Java Core Concepts: Questions and Answers
1. Basic Looping
Q1: What are the types of loops in Java?
A:
1. For Loop: Used when the number of iterations is known.
Example:
for (int i = 0; i < 5; i++) {
System.out.println(i);
2. While Loop: Used when the number of iterations is not known.
Example:
int i = 0;
while (i < 5) {
System.out.println(i);
i++;
3. Do-While Loop: Executes at least once regardless of the condition.
Example:
int i = 0;
do {
System.out.println(i);
i++;
} while (i < 5);
Q2: When to use a specific loop?
A:
- For Loop: When the number of iterations is fixed.
- While Loop: When you need to check a condition before execution.
- Do-While Loop: When you need to ensure the loop runs at least once.
2. OOP Concepts
Encapsulation
Q1: What is encapsulation? How do you implement it?
A:
Encapsulation is bundling data (fields) and methods (behavior) into a single unit (class) and
restricting direct access using access modifiers.
Example:
public class BankAccount {
private double balance; // Private variable
// Getter
public double getBalance() {
return balance;
// Setter
public void deposit(double amount) {
if (amount > 0) {
balance += amount;
}
Abstraction
Q2: What is abstraction? How is it achieved?
A:
Abstraction is hiding implementation details and exposing only the functionality. It is achieved using:
1. Abstract Classes:
abstract class Shape {
abstract void draw();
class Circle extends Shape {
void draw() {
System.out.println("Drawing Circle");
2. Interfaces:
interface Vehicle {
void start();
class Car implements Vehicle {
public void start() {
System.out.println("Car starting");
Inheritance
Q3: What is inheritance? Provide an example.
A:
Inheritance allows a class to inherit fields and methods from another class.
Example:
class Animal {
void eat() {
System.out.println("This animal eats food.");
class Dog extends Animal {
void bark() {
System.out.println("Dog barks.");
Polymorphism
Q4: What is polymorphism? How is it implemented?
A:
Polymorphism allows one interface to be used for different data types.
1. Compile-Time (Method Overloading):
class Calculator {
int add(int a, int b) {
return a + b;
double add(double a, double b) {
return a + b;
2. Run-Time (Method Overriding):
class Parent {
void show() {
System.out.println("Parent");
class Child extends Parent {
@Override
void show() {
System.out.println("Child");
3. Strings
Q1: Why are strings immutable in Java?
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";
System.out.println(s1 == s2); // true, same reference in string pool
4. Exception Handling
Q1: What are the keywords in exception handling, and when are they used?
1. Try-Catch: Handles exceptions.
try {
int result = 10 / 0;
} catch (ArithmeticException e) {
System.out.println("Cannot divide by zero.");
2. Finally: Executes code irrespective of exception.
finally {
System.out.println("Always executed.");
3. Throw: Used to explicitly throw an exception.
throw new IllegalArgumentException("Invalid argument");
4. Throws: Declares exceptions a method can throw.
void readFile() throws IOException {
throw new IOException("File not found");
5. Collections Framework
Q1: What are collections in Java?
A:
Collections are a framework for storing and manipulating groups of objects.
Q2: What are the main interfaces?
1. List: Ordered, allows duplicates (e.g., ArrayList, LinkedList).
2. Set: No duplicates (e.g., HashSet, TreeSet).
3. Queue: Follows FIFO (e.g., PriorityQueue).
4. Map: Key-value pairs (e.g., HashMap, TreeMap).
6. JVM, JDK, JRE, and Garbage Collection
Q1: What is the difference between JDK, JRE, and JVM?
A:
- JVM: Runs Java bytecode and provides the runtime environment.
- JRE: Includes JVM and libraries for running Java programs.
- JDK: Includes JRE, compiler, and tools for developing Java programs.
Q2: What is garbage collection in Java?
A:
Garbage Collection (GC) is an automatic process that removes unused objects from memory.
Example:
System.gc(); // Suggests GC to run, but it is not guaranteed.