Course contents

Java Fundamentals: complete interview question bank

Premium44 questions · 23 what-if follow-ups

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 questions

From javac to bytecode, class loading, initialisation and the JIT: what actually happens when you type java Main.

Question 1. ConceptWarm-up

Is Java compiled or interpreted?

Tests javac, bytecode, interpreter, JIT

Show answerHide answer1 follow-up

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

  1. 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.

Question 2. ConceptWarm-up

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.

Question 3. What does it print?Core

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

Question 4. ConceptSenior

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

Question 5. ConceptWarm-up

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

Question 6. ScenarioCore

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

Question 7. DebuggingCore

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 questions

Primitives, wrappers, overflow, precision and defaults: the questions where three lines of code decide whether you know the rules.

Question 8. ConceptWarm-up

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

Question 9. What does it print?Warm-up

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

Question 10. What does it print?Warm-up

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

Question 11. ConceptWarm-up

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

Question 12. CodingCore

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

Question 13. ConceptCore

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

Question 14. ScenarioCore

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

Question 15. DesignCore

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 questions

Evaluation order, short-circuiting, the ternary's hidden type rules, modulo and every shape of switch and loop exit.

Question 16. What does it print?Warm-up

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

Question 17. ConceptWarm-up

What's the difference between && and & on booleans?

Tests short-circuit evaluation

Model answer, how to say it: unlock with the Java Pass

Question 18. DebuggingCore

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

Question 19. What does it print?Core

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

Question 20. ConceptSenior

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

Question 21. ConceptCore

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

Question 22. CodingWarm-up

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 questions

Pass-by-value, overload resolution, varargs and how arrays really behave as objects.

Question 23. ConceptWarm-up

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

Question 24. ConceptWarm-up

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

Question 25. DebuggingCore

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

Question 26. What does it print?Core

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

Question 27. What does it print?Core

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

Question 28. DebuggingCore

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

Question 29. DesignSenior

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 questions

Equality, the string pool, immutability, building strings efficiently and the APIs with sharp edges.

Question 30. What does it print?Warm-up

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

Question 31. ConceptCore

Why is String immutable?

Tests immutability, security, hash caching

Model answer, how to say it, 1 follow-up: unlock with the Java Pass

Question 32. ConceptSenior

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

Question 33. ConceptCore

How many objects does new String("hello") create?

Tests string pool, interning

Model answer, how to say it: unlock with the Java Pass

Question 34. DesignCore

+, 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

Question 35. What does it print?Core

What does "a,b,,".split(",") return?

Tests split regex, trailing empty strings

Model answer, how to say it: unlock with the Java Pass

Question 36. ScenarioSenior

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

Question 37. ConceptWarm-up

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 questions

Access modifiers, imports, the default package and what modules add on top.

Question 38. ConceptWarm-up

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

Question 39. DebuggingCore

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

Question 40. DebuggingCore

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

Question 41. ConceptCore

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

Question 42. ConceptWarm-up

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

Question 43. ConceptWarm-up

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

Question 44. ConceptSenior

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