Java Fundamentals: complete interview question bank
This is the page I'd want open the evening before a Java interview. It pulls together the fundamentals questions I ask most often, plus the ones that quietly separate candidates who have read the rules from those who have only used the language: JIT warm-up, class initialisation order, ternary unboxing, array covariance, static import clashes and more.
Use it the way you'd use flashcards. Read the question, say your answer out loud first, then open the model answer and compare. The "What if..." follow-ups are the next question a real interviewer asks, so answer those out loud too.
Treat this bank as the final pass, not the first one. If a group feels shaky, go back to the topic page for longer worked examples: how Java runs, data types, control flow, methods and arrays, strings and packages and access.
How Java runs
7 questionsFrom javac to bytecode, class loading, initialisation and the JIT: what actually happens when you type java Main.
Is Java compiled or interpreted?
Tests javac, bytecode, interpreter, JIT
Show answerHide answer
Model answer
Both, in two stages. javac compiles source to platform-neutral bytecode in .class files. At run time the JVM starts by interpreting that bytecode, profiles which methods and loops are hot, and hands those to the JIT compiler, which turns them into native machine code.
HotSpot uses tiered compilation: C1 compiles quickly with light optimisation, then C2 recompiles the hottest code using the profile (inlining, escape analysis, loop unrolling). So a long-running Java service ends up running mostly native code.
How I’d say it in the interview
"Both. javac produces bytecode, the JVM interprets it at first, and the JIT compiles the hot paths to native code. C1 first for speed of compilation, then C2 for peak performance using profiling data."
Signals depth
Mentioning that the JIT can make speculative optimisations (for example, inlining the only implementation of an interface it has seen) and deoptimise back to the interpreter when an assumption breaks.
Red flags
"Java is interpreted, so it's slow" or "javac produces machine code".
Follow-ups interviewers ask next
What if the JIT is that good, why keep the interpreter at all?
Start-up. Compiling everything up front would delay start and waste effort on code that runs once. The interpreter also collects the profile that makes C2's optimisations possible, and it's the safe place to fall back to after deoptimisation.
Why is main declared public static void?
Tests launcher, entry point, static
Show answerHide answer
Model answer
public so the launcher, which lives outside your class, can call it. static so the JVM can call it without creating an instance: it doesn't know which constructor to use or what arguments to pass. void because the program reports its result through the exit status (System.exit(code)), not a return value. String[] args carries the command-line arguments.
On Java 21, removing static makes the launcher refuse to start with a "main method is not static" error. Instance main methods were a preview in 21 (JEP 445) and became final in Java 25, where the launcher instantiates the class with its no-arg constructor.
How I’d say it in the interview
"Public so the launcher can see it, static so there's no object to construct first, void because the exit code goes through System.exit. On 21 a non-static main won't launch; Java 25 finally allows instance mains."
Signals depth
Knowing that the method can also be written as static public void main(String... args) and that final or synchronized modifiers are allowed.
Red flags
"Because the compiler requires it": javac compiles a class without main happily; it's the launcher that looks for it.
What does new Order(); new Order(); print?
Tests class initialisation, instance initialisers, constructor order
Model answer, how to say it, 1 follow-up: unlock with the Java Pass
When exactly does the JVM initialise a class?
Tests lazy class loading, JLS 12.4.1, holder idiom
Model answer, how to say it, 1 follow-up: unlock with the Java Pass
What is the difference between the JDK, the JRE and the JVM?
Tests JDK, JRE, JVM, jlink
Model answer, how to say it: unlock with the Java Pass
Java is "write once, run anywhere". Where does that promise break?
Tests platform independence, JNI, charsets, file paths
Model answer, how to say it: unlock with the Java Pass
What's the difference between ClassNotFoundException and NoClassDefFoundError?
Tests class loading failures, checked vs error
Model answer, how to say it, 1 follow-up: unlock with the Java Pass
Data types & variables
8 questionsPrimitives, wrappers, overflow, precision and defaults: the questions where three lines of code decide whether you know the rules.
Name Java's primitive types and their sizes. Why is char 16 bits?
Tests primitive sizes, UTF-16
Model answer, how to say it, 1 follow-up: unlock with the Java Pass
What's the difference between char and String? What do 'a' + 1, "a" + 1 and (char) ('a' + 1) print?
Tests char arithmetic, string concatenation
Model answer, how to say it: unlock with the Java Pass
What do a == b and c == d print for Integer a = 127, b = 127, c = 128, d = 128;?
Tests Integer cache, autoboxing
Model answer, how to say it: unlock with the Java Pass
What are the default values of fields and local variables?
Tests default values, definite assignment
Model answer, how to say it, 1 follow-up: unlock with the Java Pass
How do you detect integer overflow in Java?
Tests two's complement, Math.addExact
Model answer, how to say it, 1 follow-up: unlock with the Java Pass
Is int to float a widening conversion? Can it lose data?
Tests widening conversions, float precision
Model answer, how to say it: unlock with the Java Pass
Why should you use BigDecimal for money, and what's the trap in comparing two of them?
Tests BigDecimal, scale, equals vs compareTo
Model answer, how to say it, 1 follow-up: unlock with the Java Pass
Should a field be int or Integer?
Tests primitives vs wrappers, nullability, memory
Model answer, how to say it: unlock with the Java Pass
Operators & control flow
7 questionsEvaluation order, short-circuiting, the ternary's hidden type rules, modulo and every shape of switch and loop exit.
What does int i = 5; i = i++ + ++i; leave in i?
Tests post-increment, pre-increment, evaluation order
Model answer, how to say it, 1 follow-up: unlock with the Java Pass
What's the difference between && and & on booleans?
Tests short-circuit evaluation
Model answer, how to say it: unlock with the Java Pass
Why does Integer v = flag ? cached : 0; throw NullPointerException when cached is null?
Tests conditional operator typing, unboxing
Model answer, how to say it, 1 follow-up: unlock with the Java Pass
What does -7 % 3 print, and how do you get a safe bucket index from a hash?
Tests remainder sign, Math.floorMod
Model answer, how to say it: unlock with the Java Pass
How does a switch on a String work under the hood?
Tests string switch, hashCode, lookupswitch
Model answer, how to say it, 1 follow-up: unlock with the Java Pass
What's the difference between a switch statement and a switch expression?
Tests arrow labels, yield, exhaustiveness, fall-through
Model answer, how to say it: unlock with the Java Pass
How do you break out of two nested loops at once?
Tests labelled break, loop control
Model answer, how to say it, 1 follow-up: unlock with the Java Pass
Methods & arrays
7 questionsPass-by-value, overload resolution, varargs and how arrays really behave as objects.
Is Java pass-by-value or pass-by-reference?
Tests parameter passing, references
Model answer, how to say it, 1 follow-up: unlock with the Java Pass
Are arrays objects? Where does length come from?
Tests array layout, arraylength
Model answer, how to say it, 1 follow-up: unlock with the Java Pass
Does Object[] items = new String[2]; items[0] = 42; compile? What happens?
Tests array covariance, ArrayStoreException
Model answer, how to say it, 1 follow-up: unlock with the Java Pass
Which overload runs for m(5) when there are m(long), m(Integer) and m(int...)?
Tests overload resolution phases
Model answer, how to say it, 1 follow-up: unlock with the Java Pass
What do a.equals(b), Arrays.equals(a, b) and Arrays.deepEquals do for arrays?
Tests array equality, hashCode
Model answer, how to say it: unlock with the Java Pass
What are the traps in Arrays.asList?
Tests fixed-size list, write-through, primitive arrays
Model answer, how to say it: unlock with the Java Pass
When would you avoid varargs on a hot path?
Tests varargs allocation, API design
Model answer, how to say it: unlock with the Java Pass
Strings basics
8 questionsEquality, the string pool, immutability, building strings efficiently and the APIs with sharp edges.
Why does s1 == s2 sometimes return true for strings and sometimes false?
Tests string pool, compile-time constants, equals
Model answer, how to say it: unlock with the Java Pass
Why is String immutable?
Tests immutability, security, hash caching
Model answer, how to say it, 1 follow-up: unlock with the Java Pass
How is a String stored in memory since Java 9?
Tests compact strings, coder, UTF-16
Model answer, how to say it: unlock with the Java Pass
How many objects does new String("hello") create?
Tests string pool, interning
Model answer, how to say it: unlock with the Java Pass
+, StringBuilder or String.format: which do you use, and why?
Tests string concatenation, invokedynamic, formatting cost
Model answer, how to say it, 1 follow-up: unlock with the Java Pass
What does "a,b,,".split(",") return?
Tests split regex, trailing empty strings
Model answer, how to say it: unlock with the Java Pass
Why can toUpperCase() break a comparison in production?
Tests locale, case conversion
Model answer, how to say it, 1 follow-up: unlock with the Java Pass
What's the difference between String, StringBuilder and StringBuffer?
Tests mutability, synchronization
Model answer, how to say it: unlock with the Java Pass
Packages & access
7 questionsAccess modifiers, imports, the default package and what modules add on top.
What are the four access levels, and who can see each?
Tests access modifiers
Model answer, how to say it, 1 follow-up: unlock with the Java Pass
Can a subclass in another package call protected methods on any instance of the parent?
Tests protected access rule
Model answer, how to say it: unlock with the Java Pass
What happens when two static imports bring in members with the same name?
Tests static import, ambiguity, shadowing
Model answer, how to say it: unlock with the Java Pass
What's wrong with putting classes in the default package?
Tests default package, imports, tooling
Model answer, how to say it, 1 follow-up: unlock with the Java Pass
Does import java.util.*; slow down your program or bloat the class file?
Tests imports, compile-time names
Model answer, how to say it, 1 follow-up: unlock with the Java Pass
Why can a .java file have only one public top-level class?
Tests source file rules, top-level access
Model answer, how to say it: unlock with the Java Pass
In a modular application, is a public class always accessible to everyone?
Tests JPMS, exports, strong encapsulation
Model answer, how to say it, 1 follow-up: unlock with the Java Pass
Premium question set
Keep reading with the Java Pass
The rest of this question set is part of Java One-Stop Interview Prep: in-depth explanations, diagrams, model answers and how to say them in the interview.
From ₹199 · one-time payment · No auto-renewal
Topic question sets in Java Fundamentals
- How Java runs: interview questions & scenarios
- Data types & variables: interview questions & scenarios
- Operators & control flow: interview questions & scenarios
- Methods & arrays: interview questions & scenarios
- Strings basics: interview questions & scenarios
- Packages & access: interview questions & scenarios