Java is a high-level, object-oriented programming language developed by Sun Microsystems
(now Oracle) in 1995.
Key features:
• Platform Independent: Write Once, Run Anywhere (WORA).
• Object-Oriented: Follows OOP principles like encapsulation and inheritance.
• Robust: Strong memory management and exception handling.
• Multithreaded: Supports concurrent execution of threads.
• Secure: No explicit pointers and runs in a virtual machine.
2) What are the main principles of Object-Oriented Programming (OOP)?
1. Encapsulation: Wrapping data and methods in a single unit (class).
2. Abstraction: Hiding implementation details and showing only the functionality.
3. Inheritance: Allowing a class to inherit properties and methods from another class.
4. Polymorphism: Using a single interface to represent different forms (overloading and
overriding).
3) Differentiate between JDK, JRE, and JVM.
• JDK (Java Development Kit): Provides tools for development (compiler, debugger).
• JRE (Java Runtime Environment): Includes libraries and JVM for running Java
applications.
• JVM (Java Virtual Machine): Converts bytecode into machine code and executes it.
4) Explain the concept of platform independence in Java.
Java programs are compiled into bytecode, which is platform-independent. Bytecode is
executed by the JVM, which is platform-specific, ensuring the same Java program runs on
any OS with a compatible JVM.
5) What is the significance of the main method in Java?
The main method is the entry point of a Java application. Its signature is:
Snippet
public static void main(String[] args)
• public: Accessible globally.
• static: Allows the JVM to call it without object instantiation.
• void: Returns no value.
• String[] args: Accepts command-line arguments.
6) How does Java achieve memory management?
Java uses automatic garbage collection to manage memory. Objects are allocated in the
heap memory, and when they are no longer referenced, the garbage collector deallocates
them.
7) What are constructors in Java? How are they different from methods?
• Constructors: Special methods to initialize objects.
• Name matches the class.
• No return type.
• Difference from methods: Methods perform actions; constructors initialize objects.
8) Explain method overloading and method overriding with examples.
• Overloading: Same method name, different parameters (compile-time polymorphism).
Snippet — Overloading
class Example {
void display(int a) { }
void display(String b) { }
}
• Overriding: Subclass provides a new implementation for a method in the superclass (runtime polymorphism).
Snippet — Overriding
class Parent {
void display() { }
}
class Child extends Parent {
@Override
void display() { }
}
9) What is inheritance in Java? Discuss its types.
Inheritance allows a class to acquire the properties and methods of another class using the
extends keyword. Types:
1. Single: One class inherits from another.
2. Multilevel: A chain of inheritance.
3. Hierarchical: Multiple classes inherit from one superclass.
4. Multiple (via interfaces): A class implements multiple interfaces.
10) Define polymorphism and its types in Java.
Polymorphism allows methods to perform different tasks based on the object. Types:
1. Compile-time (Method Overloading).
2. Runtime (Method Overriding).
11) What is an interface in Java, and how does it differ from an abstract class?
• Interface: A collection of abstract methods and static constants.
• Can have default and static methods (since Java 8).
• A class can implement multiple interfaces.
Difference:
• Abstract class can have both abstract and concrete methods; an interface has abstract
methods by default (Java 7 and below).
• A class extends one abstract class but can implement multiple interfaces.
12) Describe the access modifiers in Java.
• Public: Accessible everywhere.
• Protected: Accessible within the same package and subclasses.
• Default: Accessible within the same package only.
• Private: Accessible within the same class only.
13) What is encapsulation? How is it implemented in Java?
Encapsulation is bundling data (variables) and methods into a single unit (class). It's
implemented using:
1. Private access modifiers for fields.
2. Public getter and setter methods for access.
14) Explain the concept of packages in Java.
Packages are namespaces used to group related classes and interfaces. They help avoid name
conflicts and improve organization.
15) What are static variables and methods? Provide examples.
• Static Variable: Belongs to the class, shared by all objects.
• Static Method: Can be called without creating an object of the class.
Snippet
class Example {
static int count = 0; // Static variable
static void display() { // Static method
System.out.println("Count: " + count);
}
}
16) Discuss the lifecycle of a thread in Java.
1. New: Thread is created.
2. Runnable: Thread is ready to run.
3. Running: Thread is executing.
4. Blocked/Waiting: Thread is waiting for a resource.
5. Terminated: Thread execution is complete.
17) What is exception handling? How is it implemented in Java?
Exception handling manages runtime errors using try, catch, throw, throws, and
finally.
18) Differentiate between throw and throws keywords.
• throw: Used to explicitly throw an exception.
• throws: Declares exceptions a method might throw.
19) What are checked and unchecked exceptions? Give examples.
��� Checked: Checked at compile-time (e.g., IOException).
• Unchecked: Occur at runtime (e.g., NullPointerException).
20) Explain the concept of synchronization in Java.
Synchronization prevents thread interference by allowing only one thread to access a critical
section at a time, using the synchronized keyword.
21) What is the Java Collections Framework? Name its main interfaces.
A unified architecture for storing and manipulating groups of objects, including interfaces
like List, Set, and Map.
22) Differentiate between ArrayList and LinkedList.
• ArrayList: Backed by a dynamic array, faster for indexing.
• LinkedList: Backed by a doubly-linked list, better for insertions/deletions.
23) What is a HashMap? How does it work internally?
HashMap stores key-value pairs using a hash table. Keys are hashed to determine the index,
and collisions are handled using linked lists or trees.
24) Explain the significance of the equals() and hashCode() methods.
• equals(): Checks logical equality.
• hashCode(): Provides a unique hash for an object, used in hash-based collections
like HashMap.
25) What is the difference between Comparable and Comparator interfaces?
• Comparable: Used to define natural ordering.
• Comparator: Defines custom ordering.
26) Describe the Java Memory Model (JMM).
Defines how threads interact through memory, ensuring visibility and ordering of variable
accesses.
27) What is garbage collection in Java? How does it work?
Garbage collection automatically deallocates memory for objects no longer in use, reclaiming
memory in the heap.
28) Explain the concept of Java annotations.
Annotations provide metadata about code, such as @Override, @Deprecated, and
custom annotations.
29) What are lambda expressions? Provide a use case.
Lambda expressions provide a concise way to implement functional interfaces.
Example:
Snippet
List<Integer> list = Arrays.asList(1, 2, 3);
list.forEach(n -> System.out.println(n));
30) Discuss the Stream API in Java.
The Stream API processes collections of objects in a functional style, supporting operations
like filter, map, and reduce.
31) What is the purpose of the Optional class?
Optional prevents NullPointerException by representing optional values.
32) Explain the try-with-resources statement.
Manages resources (like files) automatically, ensuring they are closed after use.
Example:
33) What is the difference between final, finally, and finalize()?
• final: Prevents modification of variables, methods, or classes.
• finally: Ensures execution of code after a try-catch.
• finalize(): Called by the garbage collector before destroying an object.
34) How does the volatile keyword affect thread behavior?
Ensures visibility of changes to a variable across threads, preventing caching.
35) What are design patterns? Name a few commonly used ones in Java.
Design patterns are reusable solutions to common software design problems. Examples:
Singleton, Factory, Observer.
36) Explain the Singleton design pattern and its implementation.
Restricts a class to one instance and provides a global access point to it.
Snippet
class Singleton {
private static Singleton instance;
private Singleton() { }
public static Singleton getInstance() {
if (instance == null) {
instance = new Singleton();
}
return instance;
}
}
37) What is JDBC? How is it used in Java applications?
JDBC (Java Database Connectivity) is an API for connecting to databases.
Steps:
1. Load driver.
2. Establish connection.
3. Execute SQL queries.
4. Close connection.
38) Discuss the differences between Statement and PreparedStatement.
• Statement: Used for static queries.
• PreparedStatement: Precompiled and supports dynamic queries.
39) What is the purpose of the transient keyword?
Excludes fields from serialization.
40) Explain serialization and deserialization in Java.
• Serialization: Converts an object to a byte stream.
• Deserialization: Converts a byte stream back to an object.
41) What are inner classes? Differentiate between static and non-static inner classes.
Classes defined within another class. Types: static, non-static, local, and anonymous.
42) Describe the use of the synchronized keyword.
Locks a block/method to allow only one thread access at a time.
43) What is the difference between String, StringBuilder, and StringBuffer?