Java Core Concepts QA
Java Core Concepts QA
1. Basic Looping
A:
Example:
System.out.println(i);
Example:
int i = 0;
while (i < 5) {
System.out.println(i);
i++;
Example:
int i = 0;
do {
System.out.println(i);
i++;
- Do-While Loop: When you need to ensure the loop runs at least once.
2. OOP Concepts
Encapsulation
A:
Encapsulation is bundling data (fields) and methods (behavior) into a single unit (class) and
Example:
// Getter
return balance;
// Setter
if (amount > 0) {
balance += amount;
}
Abstraction
A:
Abstraction is hiding implementation details and exposing only the functionality. It is achieved using:
1. Abstract Classes:
void draw() {
System.out.println("Drawing Circle");
2. Interfaces:
interface Vehicle {
void start();
System.out.println("Car starting");
Inheritance
A:
Inheritance allows a class to inherit fields and methods from another class.
Example:
class Animal {
void eat() {
void bark() {
System.out.println("Dog barks.");
Polymorphism
A:
class Calculator {
return a + b;
return a + b;
void show() {
System.out.println("Parent");
@Override
void show() {
System.out.println("Child");
3. Strings
A:
1. Thread-Safety: Immutability ensures that strings can be shared across threads without
synchronization issues.
2. Security: Immutable strings are used in sensitive operations like passwords and class loading.
3. String Pooling: Saves memory as the same string instance can be reused.
Example:
String s1 = "Hello";
String s2 = "Hello";
4. Exception Handling
Q1: What are the keywords in exception handling, and when are they used?
int result = 10 / 0;
} catch (ArithmeticException e) {
finally {
System.out.println("Always executed.");
5. Collections Framework
A:
A:
- JDK: Includes JRE, compiler, and tools for developing Java programs.
A:
Garbage Collection (GC) is an automatic process that removes unused objects from memory.
Example: