Java interviews, especially for experienced developers, often include tricky questions that test deeper understanding of the language's unique behaviors, concepts, and edge cases. This knowledgebase offers challenging questions plus code examples and explanations to empower candidates in mastering their Java technical rounds.
1. Final Variable Re-assignment
Question: What happens if you try to reassign a final variable in Java?
Code Example:
java
public class FinalTest {
public static void main(String[] args) {
final int i;
i = 10;
int j = i + 5;
// i = j + 20; // Uncommenting this line causes error
System.out.println(i + " " + j);
}
}
Explanation:
A final variable in Java can only be assigned once. Attempting to reassign it causes a compile-time error: variable i might already have been initialized. This tests understanding of immutability in Java variables.
2. Abstract and Final Class Combination
Question: Can you make a class both abstract and final?
Code Example:
java
abstract final class TestClass {
public abstract void display();
}
Explanation:
This is not allowed. An abstract class must be inherited and cannot be instantiated on its own, while a final class cannot be extended. Declaring a class both abstract and final causes compilation errors due to contradictory keywords.
3. Overloading the main() Method
Question: Is it possible to overload the main() method in Java?
Code Example:
java
public class MainOverload {
public static void main(String[] args) {
System.out.println("Main with String array");
main(5); // Calls overloaded main
}
public static void main(int x) {
System.out.println("Overloaded main with int: " + x);
}
}
Explanation:
Yes, Java allows overloading the main() method with different parameter lists. However, the JVM only calls the standard main(String[] args) method to start the application. Overloaded main methods can be called explicitly within the program.
4. String Pool and Immutable Strings
Question: Why are Strings immutable in Java? How does the string pool help
Code Example:
java
String s1 = "hello";
String s2 = "hello";
System.out.println(s1 == s2); // true
Explanation:
Strings are immutable for thread-safety and performance optimization. The String pool stores unique string literals; thus, s1 and s2 point to the same object in memory. This is a subtle but crucial optimization.
5. Comparator vs Comparable for Sorting
Question: Difference between Comparable and Comparator interfaces with examples?
Code Example:
java
class Employee implements Comparable
String name;
int salary;
public int compareTo(Employee e) {
return this.name.compareTo(e.name);
}
}
// Usage of Comparator for salary-based sorting
Comparator
public int compare(Employee e1, Employee e2) {
return Integer.compare(e1.salary, e2.salary);
}
};
Explanation:
Comparable defines natural ordering inside the class, while Comparator defines external custom ordering, suitable when sorting by multiple criteria.
6. Difference between Shallow and Deep Copy
Question: Explain shallow copy and deep copy with instance scenarios.
Code Example:
java
class Address {
String city;
}
class Employee implements Cloneable {
String name;
Address addr;
protected Object clone() throws CloneNotSupportedException {
return super.clone(); // shallow copy
}
}
Employee e1 = new Employee();
e1.name = "Alice";
e1.addr = new Address();
e1.addr.city = "Mumbai";
Employee e2 = (Employee) e1.clone();
e2.addr.city = "Delhi"; // Changes reflect in e1.addr.city for shallow copy
Explanation:
A shallow copy duplicates the object but copies references to nested objects, causing shared states. Deep copy duplicates nested objects as well, preventing side effects. This tests understanding of clone() behavior.
7. How Does Garbage Collection Work? Can You Force It?
Question: Explain Java Garbage Collection and ways to influence it.
Code Example:
java
System.gc(); // Suggests JVM to start garbage collection
Explanation:
Garbage collection in Java automatically frees memory of unreferenced objects. Developers can request collection via System.gc(), but it’s not guaranteed to run immediately. Understanding GC types (serial, parallel, CMS, G1) is important.
8. Static vs Instance Method Overriding
Question: Can static methods be overridden in Java?
Code Example:
java
class Parent {
static void show() {
System.out.println("Parent static show");
}
}
class Child extends Parent {
static void show() {
System.out.println("Child static show");
}
}
Parent p = new Child();
p.show(); // Output: Parent static show
Explanation:
Static methods belong to the class, not the instance, so they cannot be overridden, only hidden. The call is resolved at compile time based on reference type, resulting in parent’s method being called regardless of the instance.
9. What Happens When You Throw an Exception in a Try Block?
Question: Can you catch multiple exceptions together in a catch block? What if exceptions are related?
Code Example:
java
try {
// code
} catch(IOException | Exception e) {
// Compilation error: Exception already catches IOException
}
Explanation:
Multiple catch parameters must not have parent-child relationships to avoid unreachable catch blocks. Exception hierarchy rules govern this.
10. Difference Between == and equals()
Question: When to use == versus .equals()?
Code Example:
java
String a = new String("test");
String b = new String("test");
System.out.println(a == b); // false
System.out.println(a.equals(b)); // true
Explanation:
== checks reference equality (same object), while .equals() checks logical equality (content). Proper overriding of .equals() is important for custom objects .
Conclusion
Mastering tricky Java interview questions requires understanding core language concepts, subtle behaviors, and design principles. Candidates should practice these questions with code snippets and learn not just the “what” but the “why” behind them. This prepares them effectively for technical interviews aimed at senior and specialized Java roles.
For further deep dives, study Java 8+ features like streams and lambdas, concurrency handling, JVM internals, and design patterns, as these often appear in advanced rounds.
This article offers a blend of theory, advanced concepts, and concrete code examples to empower job seekers in confidently answering complex Java interview questions with clarity and precision.
If further elaboration or more sample questions with code are required, please ask!
Let’s talk about the future, and make it happen!
By continuing to use and navigate this website, you are agreeing to the use of cookies.
Find out more