Tricky Java Interview Questions
This document compiles a selection of Java interview questions that often trip up
candidates, covering core concepts, object-oriented programming, and common pitfalls.
Core Java and Data Types
1. The 'String' Constant Pool and Immutability
Explain the difference between == and equals() when comparing String objects. Also,
describe what happens in memory (specifically the String Constant Pool) when the
following code is executed:String s1 = "Hello";
String s2 = "Hello";
String s3 = new String("Hello");
String s4 = [Link]();
2. Autoboxing and Caching
What is the output of the following code and why?Integer a = 100;
Integer b = 100;
Integer c = 200;
Integer d = 200;
[Link](a == b);
[Link](c == d);
3. Final, Finally, and Finalize
Explain the distinct purposes of the keywords and method: final, finally, and
finalize(). Can a finally block be executed if a program exits via [Link](0)
inside a try block?
Object-Oriented Programming (OOP)
4. Method Overloading vs. Overriding
Feature Method Overloading Method Overriding
Definition Same method name, different Same method name and same
signature (parameters) signature (parameters)
Inheritance Not required (can happen Required (must happen in a
within the same class) subclass)
Example Question Can you overload a static Can you override a private
method? method?
5. Abstract Classes vs. Interfaces
Explain a scenario where you would choose an abstract class over an interface, and
vice-versa, in modern Java (post-Java 8).
6. The Private Constructor
When and why would you define a class with a private constructor? Provide a use case
example.
Exception Handling and Memory Management
7. Exception Hierarchy and Checked vs. Unchecked
Explain the difference between a Checked Exception and an Unchecked Exception.
Provide an example of each. Where does Error fit into the hierarchy?
8. try-with-resources
What problem does the try-with-resources statement solve, and which interface must
a resource implement to be used in this construct?
9. Garbage Collection and Object Eligibility
Under what conditions does an object become eligible for Garbage Collection? Can you
force the JVM to run the Garbage Collector?
Multithreading and Concurrency
10. synchronized Keyword
What are the two ways the synchronized keyword can be used in Java? What is the lock
acquired in each case?
11. volatile Keyword
What is the primary purpose of the volatile keyword, and how is it different from the
synchronized keyword?
12. Deadlock
Define "Deadlock" in the context of multithreading. How can it be prevented or detected?
Java Collections Framework
13. HashMap and ConcurrentHashMap
Explain the internal working of a HashMap (e.g., buckets, hashing, collision resolution).
How does ConcurrentHashMap achieve thread-safety and better performance compared
to a synchronized HashMap or Hashtable?
14. Performance of ArrayList vs. LinkedList
Describe the time complexity (Big O notation) for the following operations in ArrayList
and LinkedList:
Operation ArrayList Time Complexity LinkedList Time Complexity
Access/Search (by index) O(1) O(n)
Insertion/Deletion (at end) O(1) Amortized O(1)
Insertion/Deletion (in middle) O(n) O(1) (if iterator is already at the
location) / O(n) (otherwise)
Java 8 and Beyond
15. Functional Interfaces
What is a Functional Interface, and how is it related to Lambda Expressions? Provide an
example.
16. The Optional Class
What is the main goal of the Optional class? Write a brief snippet showing how it can be
used to avoid a NullPointerException.// Example snippet to be placed here
17. Stream API Intermediate vs. Terminal Operations
Briefly explain the difference between Intermediate and Terminal operations in the Stream
API. Can a stream be reused?