Top Interview Questions on Core Java
Preparing for a Java programming interview involves understanding fundamental concepts and being able to apply them effectively. Here’s a curated list of common interview questions covering core Java topics, along with brief explanations and example code where applicable.
1. What are the main principles of Object-Oriented Programming (OOP)?
- Encapsulation: Bundling data (fields) and methods (functions) that operate on the data within a single unit (class).
- Inheritance: Acquiring properties and behaviors from a parent class (superclass) to a child class (subclass).
- Polymorphism: The ability of objects of different classes to be treated as objects of a common superclass, achieved through method overriding and method overloading.
2. What is the difference between ==
and .equals()
method in Java?
==
operator checks for reference equality (whether two references point to the same memory location)..equals()
method (from theObject
class) is overridden in specific classes (likeString
,Integer
) to check for object equality based on their contents.
String s1 = new String("Hello");
String s2 = new String("Hello");
System.out.println(s1 == s2); // false (different memory locations)
System.out.println(s1.equals(s2)); // true (same content)
3. Explain the concept of static keyword in Java.
static
keyword is used to create variables and methods that belong to the class rather than instances of the class.static
variables are shared among all instances of a class.static
methods can be called without creating an instance of the class.
public class Example {
static int count = 0; // static variable
static void increment() { // static method
count++;
}
}
4. What is the difference between abstract class
and interface
in Java?
- Abstract class: Can have abstract methods (methods without a body) and concrete methods. Can have instance variables. Cannot be instantiated.
- Interface: Only contains abstract methods and constants (variables are implicitly
final
andstatic
). Used to achieve multiple inheritance and provide a contract for classes to implement.
// Abstract class
abstract class Animal {
abstract void makeSound();
void eat() {
System.out.println("Animal is eating");
}
}
// Interface
interface Drawable {
void draw();
}
5. What is method overloading and method overriding? Provide examples.
- Method overloading: Defining multiple methods with the same name but different parameters in the same class.
- Method overriding: Providing a specific implementation of a method in a subclass that is already provided by its superclass.
// Method overloading
class Calculator {
int add(int a, int b) {
return a + b;
}
double add(double a, double b) {
return a + b;
}
}
// Method overriding
class Animal {
void makeSound() {
System.out.println("Animal makes a sound");
}
}
class Dog extends Animal {
@Override
void makeSound() {
System.out.println("Dog barks");
}
}
6. Explain the concept of Exception Handling in Java.
- Exception Handling is used to handle runtime errors (exceptions) that occur during program execution.
- Key components include
try
,catch
,finally
, andthrow
keywords. try
block: Encloses the code that may throw an exception.catch
block: Handles specific exceptions caught bytry
block.finally
block: Executes code whether an exception is thrown or not.throw
keyword: Throws a user-defined exception.
try {
int result = 10 / 0; // Division by zero exception
} catch (ArithmeticException e) {
System.out.println("Cannot divide by zero");
} finally {
System.out.println("Finally block executed");
}
7. What is the difference between final
, finally
, and finalize
in Java?
final
: Keyword used to declare constants, prevent method overriding, and inheritance of classes.finally
: Block used in exception handling to execute code irrespective of whether an exception is thrown or not.finalize
: Method called by the garbage collector before reclaiming the memory occupied by the object.
8. Explain the concept of Java Collections Framework.
- Java Collections Framework provides a set of interfaces and classes to store, manipulate, and manage groups of objects.
- Key interfaces:
List
,Set
,Map
. - Implementations:
ArrayList
,LinkedList
,HashSet
,HashMap
, etc.
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("Java");
list.add("Python");
System.out.println(list); // Output: [Java, Python]
}
}
9. What are the access modifiers in Java?
public
: Accessible from any other class.protected
: Accessible within the same package and subclasses (even if they are in different packages).default
(no modifier): Accessible within the same package.private
: Accessible only within the same class.
10. Explain multithreading and synchronization in Java.
- Multithreading: Executing multiple threads concurrently to improve the performance of the program.
- Synchronization: Mechanism to control access to resources shared among multiple threads to prevent data inconsistency and thread interference.
class PrintThread extends Thread {
@Override
public void run() {
for (int i = 0; i < 5; i++) {
System.out.println("Printing " + i);
try {
Thread.sleep(1000); // Pause for 1 second
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public class Main {
public static void main(String[] args) {
PrintThread thread1 = new PrintThread();
PrintThread thread2 = new PrintThread();
thread1.start();
thread2.start();
}
}