R
Rishtaara
All Interview Sets
Java

Java Interview Questions

10 questions with detailed answers

Q1. What is the difference between JDK, JRE, and JVM?

Answer: JDK is the development kit (compiler + tools), JRE is the runtime environment, JVM executes bytecode.

Q2. What is OOP? Explain its pillars.

Answer: Object-Oriented Programming: Encapsulation, Inheritance, Polymorphism, and Abstraction.

Q3. Difference between == and .equals()?

Answer: == compares references (memory addresses), .equals() compares object content (when properly overridden).

Q4. What is the difference between abstract class and interface?

Answer: Abstract classes can have constructors and non-abstract methods; interfaces define contracts. Java 8+ interfaces can have default methods.

Q5. Explain Java exception handling.

Answer: try-catch-finally blocks handle exceptions. Checked exceptions must be declared; unchecked extend RuntimeException.

Q6. Why is String immutable in Java?

Answer: Immutability makes strings thread-safe, cacheable in the string pool, and safe for security-sensitive values such as class names and URLs. Operations that appear to modify a String create a new object.

Q7. What is the difference between ArrayList and LinkedList?

Answer: ArrayList provides fast indexed access and usually better cache performance. LinkedList offers constant-time insertion or removal when a node is already known, but traversal is linear and memory overhead is higher.

Q8. What are generics in Java?

Answer: Generics provide compile-time type safety for classes, interfaces, and methods. They reduce casts and let reusable code work with different reference types, such as List<String>.

Q9. What does the synchronized keyword do?

Answer: synchronized allows only one thread at a time to execute a protected block or method for the same monitor, providing mutual exclusion and memory visibility between threads.

Q10. What is the Stream API?

Answer: The Stream API processes collections declaratively through operations such as filter, map, sorted, and reduce. Intermediate operations are lazy, and a terminal operation triggers execution.