Java Interview Questions and Answers

JVM fundamentals, OOP, collections, concurrency and Spring concepts.

Practise 10 random 2 peer-reviewed questions
Java Interview Syllabus & Preparation Strategy

Whether you are preparing for entry-level Java interview questions for freshers or senior software engineer interview questions addressing concurrency, scalability, and system architecture, this track provides peer-reviewed model answers with syntax walkthroughs, edge cases, and practical interview tips.

1 How does the Java memory model and garbage collection work? Hard

Memory is divided into heap (shared objects) and per-thread stacks (frames, locals). The heap has a young generation (Eden plus two survivor spaces) and an old generation.

GC roots (stack references, statics, JNI references) mark reachable objects; everything else is collectible. Minor GC copies surviving young objects between survivor spaces and promotes long-lived ones to old gen. Major/full GC handles old gen and can pause the application.

Collectors: Serial, Parallel, G1 (default in recent JDKs, region-based with pause targets), ZGC and Shenandoah for very low pause times. Tuning knobs include heap size (-Xmx), pause goals (-XX:MaxGCPauseMillis) and region size. Diagnose with jstat, GC logs, JFR and heap dumps.

2 What causes a deadlock and how do you prevent it? Hard

A deadlock needs four conditions: mutual exclusion, hold-and-wait, no preemption and circular wait.

Classic case: thread A locks X then waits for Y while thread B locks Y then waits for X.

Prevention:

  • Establish a global lock ordering and always acquire in that order.
  • Use tryLock with a timeout and back off, or lock-free structures.
  • Keep critical sections small and avoid calling external code while holding a lock.
  • Prefer higher-level concurrency utilities (java.util.concurrent) over manual synchronized.
  • Use immutable objects and message passing where possible.

Diagnose with thread dumps (jstack) and JConsole, which detect deadlock cycles.

Frequently Asked Questions About Java Interviews

What do hiring managers evaluate in Java technical rounds?

Technical interviewers look for foundational fluency, idiomatic syntax, clarity when communicating complex logic, and awareness of performance trade-offs (e.g. memory footprint, render performance, and network latency) in production environments.

What are the best interview tips for practicing Java questions?

Use active recall: summarize each answer in your own words before revealing the model solution. Focus on explaining why a certain approach is chosen rather than just memorizing code syntax.