1. Introduction
This roundup collects the Java interview questions that come up again and again across telephonic screens, onsite loops, and written tests — the kind of questions that check whether you actually understand the language rather than just having memorized syntax.
Cricket analogy: Like a national selector's trials testing a player across net sessions, practice matches, and fitness tests rather than just a single flashy shot, this roundup covers the Java questions repeated across phone screens, onsites, and written tests.
The goal here is not just to give you an answer to recite, but to explain the reasoning so you can adapt it when an interviewer changes the wording or asks a follow-up 'why' question.
Cricket analogy: A commentator who explains why a captain set a particular field, not just what the field was, can adapt when the next ball changes everything, the same reasoning-over-recitation goal drives these answers.
2. Syntax
String a = "hello";
String b = "hello";
String c = new String("hello");
System.out.println(a == b); // reference comparison
System.out.println(a == c); // reference comparison
System.out.println(a.equals(c)); // value comparisonfinal int x = 10;
// x = 20; // compile error: final cannot be reassigned
try {
System.out.println("try block");
throw new RuntimeException("boom");
} finally {
System.out.println("finally always runs");
}3. Explanation
Interviewers usually build these questions around three themes: (1) memory model and object identity — spot-checked with == vs equals() and String pooling; (2) keyword confusion — static vs instance, final vs finally vs finalize; and (3) control flow guarantees — does finally always execute, what happens with nested try-catch, checked vs unchecked exceptions.
Cricket analogy: Interviewers probe three areas like a selection panel: player identity, is this the same Virat Kohli or a replacement, mirroring == vs equals; role confusion, captain vs vice-captain duties, mirroring static vs instance; and match protocols, does the innings guarantee always hold, mirroring finally always executing.
A strong answer names the underlying rule first ('== compares references for objects, values for primitives'), then walks through the specific example line by line, and finally states the practical implication ('that's why you should never compare Strings, wrapper objects, or enums with == unless you specifically want reference identity').
Cricket analogy: A strong answer states the rule first, DRS overturns only marginal umpire's-call decisions, replays the specific ball, then states the implication, just as you should state == compares references before showing why comparing two Strings with == is risky.
Exam strategy: when asked to predict output, first identify whether each variable is a primitive or a reference type. Primitives compare by value everywhere; references compare by identity with == and by contract with .equals(). This single distinction resolves the majority of 'tricky output' questions.
4. Example
public class InterviewDemo {
static int counter = 0;
int id;
InterviewDemo() {
counter++;
id = counter;
}
public static void main(String[] args) {
Integer a = 127;
Integer b = 127;
Integer c = 128;
Integer d = 128;
System.out.println(a == b);
System.out.println(c == d);
InterviewDemo o1 = new InterviewDemo();
InterviewDemo o2 = new InterviewDemo();
System.out.println(o1.id + " " + o2.id + " " + InterviewDemo.counter);
}
}5. Output
true
false
1 2 26. Key Takeaways
- == compares object references (memory addresses) while .equals() compares logical content; always override equals() (and hashCode()) together for value comparison.
- Integer caching means autoboxed values from -128 to 127 are reused from a pool, so == happens to return true in that range but not outside it — never rely on this.
- Strings are immutable in Java; every 'modification' creates a new String object, which is why StringBuilder is preferred for heavy concatenation in loops.
- static members belong to the class and are shared across all instances, while instance members belong to each object separately.
- finally executes regardless of whether an exception was thrown, caught, or a return statement was hit in the try/catch — only System.exit() or a JVM crash can skip it.
- Checked exceptions must be declared or handled at compile time (e.g. IOException); unchecked exceptions (RuntimeException and subclasses) are not enforced by the compiler.
Practice what you learned
1. What does the following print? String a = "java"; String b = "java"; System.out.println(a == b);
2. Which keyword prevents a method from being overridden in a subclass?
3. What is the output? try { return 1; } finally { System.out.println("finally"); }
4. Which of these is a checked exception in standard Java?
5. What happens when you call a static method using an instance reference, e.g. obj.staticMethod()?
Was this page helpful?
You May Also Like
OOP Interview Questions in Java
A focused roundup of Java OOP interview questions covering the four pillars — encapsulation, inheritance, polymorphism, abstraction — plus interface vs abstract class and overloading vs overriding.
Previous Exam Questions on Java
A study-ready roundup of typical semester exam questions on Java — definitions, short programs, output-prediction, and viva questions — matching common university exam patterns.
Exceptions in Java
Understand what exceptions are in Java, how the Throwable hierarchy is organized, and why exception handling keeps programs robust.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics