Java Coding Interview Questions + Answers (With Code Examples) _ Zero to Mastery
Java Coding Interview Questions + Answers (With Code Examples) _ Zero to Mastery
Java Coding
Interview Questions
+ Answers (With
Code Examples)
In This Guide:
https://zerotomastery.io/blog/java-coding-interview-questions/ 1/71
30/05/2025, 23:09 Java Coding Interview Questions + Answers (With Code Examples) | Zero To Mastery
https://zerotomastery.io/blog/java-coding-interview-questions/ 2/71
30/05/2025, 23:09 Java Coding Interview Questions + Answers (With Code Examples) | Zero To Mastery
With that out of the way, let’s get into these interview
questions!
https://zerotomastery.io/blog/java-coding-interview-questions/ 3/71
30/05/2025, 23:09 Java Coding Interview Questions + Answers (With Code Examples) | Zero To Mastery
https://zerotomastery.io/blog/java-coding-interview-questions/ 4/71
30/05/2025, 23:09 Java Coding Interview Questions + Answers (With Code Examples) | Zero To Mastery
https://zerotomastery.io/blog/java-coding-interview-questions/ 5/71
30/05/2025, 23:09 Java Coding Interview Questions + Answers (With Code Examples) | Zero To Mastery
For example
int a = 5;
int b = 5;
System.out.println(a == b); // t
For example
System.out.println(str1.equals(s
https://zerotomastery.io/blog/java-coding-interview-questions/ 6/71
30/05/2025, 23:09 Java Coding Interview Questions + Answers (With Code Examples) | Zero To Mastery
For example
package accessmodifiers;
https://zerotomastery.io/blog/java-coding-interview-questions/ 7/71
30/05/2025, 23:09 Java Coding Interview Questions + Answers (With Code Examples) | Zero To Mastery
Final
For example
Finally
For example
try {
int result = 10 / 0;
} catch (ArithmeticException e)
System.out.println("Caught a
https://zerotomastery.io/blog/java-coding-interview-questions/ 8/71
30/05/2025, 23:09 Java Coding Interview Questions + Answers (With Code Examples) | Zero To Mastery
} finally {
System.out.println("This wil
}
Finalize
Abstract Class
https://zerotomastery.io/blog/java-coding-interview-questions/ 9/71
30/05/2025, 23:09 Java Coding Interview Questions + Answers (With Code Examples) | Zero To Mastery
For example
Interface
For example
interface Pet {
void play();
https://zerotomastery.io/blog/java-coding-interview-questions/ 10/71
30/05/2025, 23:09 Java Coding Interview Questions + Answers (With Code Examples) | Zero To Mastery
Understanding
when to use abstract classes versus
interfaces is key to designing scalable and
maintainable object-oriented systems. With Java’s
evolving capabilities, such as default methods in
interfaces (Java 8+) and sealed classes (Java 17+),
interviewers may explore how you would approach
real-world scenarios requiring abstraction.
Constructor
For example
class Person {
String name;
https://zerotomastery.io/blog/java-coding-interview-questions/ 11/71
30/05/2025, 23:09 Java Coding Interview Questions + Answers (With Code Examples) | Zero To Mastery
// Constructor
Person(String name) {
this.name = name;
}
}
Method
For example
class Person {
String name;
Person(String name) {
this.name = name;
Academy }
Testimonials Resources Community SIGN IN JOIN ZERO TO MASTERY
// Method
void greet() {
System.out.println("Hell
}
}
Key differences:
https://zerotomastery.io/blog/java-coding-interview-questions/ 12/71
30/05/2025, 23:09 Java Coding Interview Questions + Answers (With Code Examples) | Zero To Mastery
String
For example
StringBuilder
https://zerotomastery.io/blog/java-coding-interview-questions/ 13/71
30/05/2025, 23:09 Java Coding Interview Questions + Answers (With Code Examples) | Zero To Mastery
For example
StringBuffer
For example
Key differences:
https://zerotomastery.io/blog/java-coding-interview-questions/ 14/71
30/05/2025, 23:09 Java Coding Interview Questions + Answers (With Code Examples) | Zero To Mastery
For example
class MathUtils {
public static int add(int a,
return a + b;
}
}
https://zerotomastery.io/blog/java-coding-interview-questions/ 15/71
30/05/2025, 23:09 Java Coding Interview Questions + Answers (With Code Examples) | Zero To Mastery
For example
class Calculator {
public int add(int a, int b)
return a + b;
}
For example
https://zerotomastery.io/blog/java-coding-interview-questions/ 16/71
30/05/2025, 23:09 Java Coding Interview Questions + Answers (With Code Examples) | Zero To Mastery
class Animal {
void sound() {
System.out.println("Anim
}
}
Key differences:
https://zerotomastery.io/blog/java-coding-interview-questions/ 17/71
30/05/2025, 23:09 Java Coding Interview Questions + Answers (With Code Examples) | Zero To Mastery
Array
For example
ArrayList
For example
https://zerotomastery.io/blog/java-coding-interview-questions/ 18/71
30/05/2025, 23:09 Java Coding Interview Questions + Answers (With Code Examples) | Zero To Mastery
import java.util.ArrayList;
Key differences:
https://zerotomastery.io/blog/java-coding-interview-questions/ 19/71
30/05/2025, 23:09 Java Coding Interview Questions + Answers (With Code Examples) | Zero To Mastery
List
For example
Set
For example
Map
For example
https://zerotomastery.io/blog/java-coding-interview-questions/ 20/71
30/05/2025, 23:09 Java Coding Interview Questions + Answers (With Code Examples) | Zero To Mastery
HashMap
https://zerotomastery.io/blog/java-coding-interview-questions/ 21/71
30/05/2025, 23:09 Java Coding Interview Questions + Answers (With Code Examples) | Zero To Mastery
For example
LinkedHashMap
For example
TreeMap
TreeMap implements the NavigableMap interface
and stores key-value pairs in a sorted order based on
the natural ordering of keys or a custom comparator.
It does not allow null keys.
For example
https://zerotomastery.io/blog/java-coding-interview-questions/ 22/71
30/05/2025, 23:09 Java Coding Interview Questions + Answers (With Code Examples) | Zero To Mastery
treeMap.put("Bob", 25);
System.out.println(treeMap); //
Key differences
Order:
HashMap : No order guaranteed
Performance:
HashMap : O(1) for most operations
Null keys:
HashMap and LinkedHashMap : Allow one
null key
For example
import java.io.*;
class Example {
void readFile(String fileNam
BufferedReader reader =
System.out.println(reade
reader.close();
}
}
For example
try {
int result = 10 / 0;
} catch (ArithmeticException e)
https://zerotomastery.io/blog/java-coding-interview-questions/ 24/71
30/05/2025, 23:09 Java Coding Interview Questions + Answers (With Code Examples) | Zero To Mastery
System.out.println("Cannot d
}
Finally block
For example
try {
BufferedReader reader = new
} catch (IOException e) {
System.out.println("File not
} finally {
System.out.println("Cleanup
}
For example
https://zerotomastery.io/blog/java-coding-interview-questions/ 25/71
30/05/2025, 23:09 Java Coding Interview Questions + Answers (With Code Examples) | Zero To Mastery
For example
import java.util.ArrayList;
class Example {
public static void main(Stri
// Using generics with A
ArrayList<String> list =
list.add("Java");
list.add("Generics");
Benefits of generics
https://zerotomastery.io/blog/java-coding-interview-questions/ 26/71
30/05/2025, 23:09 Java Coding Interview Questions + Answers (With Code Examples) | Zero To Mastery
For example
For example
https://zerotomastery.io/blog/java-coding-interview-questions/ 27/71
30/05/2025, 23:09 Java Coding Interview Questions + Answers (With Code Examples) | Zero To Mastery
Creating threads
https://zerotomastery.io/blog/java-coding-interview-questions/ 28/71
30/05/2025, 23:09 Java Coding Interview Questions + Answers (With Code Examples) | Zero To Mastery
Concurrency utilities
import java.util.concurrent.Exec
import java.util.concurrent.Exec
executor.execute(() -> S
executor.execute(() -> S
https://zerotomastery.io/blog/java-coding-interview-questions/ 29/71
30/05/2025, 23:09 Java Coding Interview Questions + Answers (With Code Examples) | Zero To Mastery
executor.shutdown(); //
}
}
Synchronized methods
For example
class Counter {
private int count = 0;
return count;
}
}
Synchronized blocks
class Counter {
private int count = 0;
Key differences
Scope:
Synchronized methods lock the entire method
Performance:
Synchronized methods can be less efficient
because they lock the entire method, including
unnecessary code
https://zerotomastery.io/blog/java-coding-interview-questions/ 31/71
30/05/2025, 23:09 Java Coding Interview Questions + Answers (With Code Examples) | Zero To Mastery
Flexibility:
Synchronized blocks allow locking on a specific
object ( this , a custom lock object, etc.)
class Counter {
private int count = 0;
private final Object lock =
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
} catch (IOException e)
e.printStackTrace();
}
}
}
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
https://zerotomastery.io/blog/java-coding-interview-questions/ 34/71
30/05/2025, 23:09 Java Coding Interview Questions + Answers (With Code Examples) | Zero To Mastery
Files.write(Paths.ge
} catch (IOException e)
e.printStackTrace();
}
}
}
class Counter {
private boolean running = tr
running = false;
}
class Counter {
private volatile boolean run
https://zerotomastery.io/blog/java-coding-interview-questions/ 36/71
30/05/2025, 23:09 Java Coding Interview Questions + Answers (With Code Examples) | Zero To Mastery
import java.util.concurrent.atom
class Counter {
private AtomicInteger count
https://zerotomastery.io/blog/java-coding-interview-questions/ 37/71
30/05/2025, 23:09 Java Coding Interview Questions + Answers (With Code Examples) | Zero To Mastery
For example
class Outer {
static class StaticNested {
void display() {
System.out.println("
}
}
}
For example
class Outer {
class Inner {
void display() {
System.out.println("
}
}
}
https://zerotomastery.io/blog/java-coding-interview-questions/ 38/71
30/05/2025, 23:09 Java Coding Interview Questions + Answers (With Code Examples) | Zero To Mastery
}
}
For example
class Outer {
void method() {
class LocalInner {
void display() {
System.out.print
}
}
LocalInner local = new L
local.display();
}
}
For example
interface Greeting {
void sayHello();
}
https://zerotomastery.io/blog/java-coding-interview-questions/ 39/71
30/05/2025, 23:09 Java Coding Interview Questions + Answers (With Code Examples) | Zero To Mastery
System.out.print
}
};
greeting.sayHello();
}
}
For example
For example
For example
javac MyProgram.java
Key differences
https://zerotomastery.io/blog/java-coding-interview-questions/ 41/71
30/05/2025, 23:09 Java Coding Interview Questions + Answers (With Code Examples) | Zero To Mastery
https://zerotomastery.io/blog/java-coding-interview-questions/ 42/71
30/05/2025, 23:09 Java Coding Interview Questions + Answers (With Code Examples) | Zero To Mastery
For example
https://zerotomastery.io/blog/java-coding-interview-questions/ 43/71
30/05/2025, 23:09 Java Coding Interview Questions + Answers (With Code Examples) | Zero To Mastery
https://zerotomastery.io/blog/java-coding-interview-questions/ 44/71
30/05/2025, 23:09 Java Coding Interview Questions + Answers (With Code Examples) | Zero To Mastery
https://zerotomastery.io/blog/java-coding-interview-questions/ 45/71
30/05/2025, 23:09 Java Coding Interview Questions + Answers (With Code Examples) | Zero To Mastery
High-
Parallel GC Yes Throughput throughput
applications
General-
Balanced purpose,
G1 GC Yes
performance low-latency
needs
Large
Ultra-low heaps, real-
ZGC Yes
latency time
systems
Medium-
sized
Shenandoah Yes Low latency heaps, low-
pause
needs
https://zerotomastery.io/blog/java-coding-interview-questions/ 46/71
30/05/2025, 23:09 Java Coding Interview Questions + Answers (With Code Examples) | Zero To Mastery
Concurrency
For example
Parallelism
For example
https://zerotomastery.io/blog/java-coding-interview-questions/ 47/71
30/05/2025, 23:09 Java Coding Interview Questions + Answers (With Code Examples) | Zero To Mastery
For example
Happens-before relationship
https://zerotomastery.io/blog/java-coding-interview-questions/ 48/71
30/05/2025, 23:09 Java Coding Interview Questions + Answers (With Code Examples) | Zero To Mastery
class Counter {
private int count = 0;
https://zerotomastery.io/blog/java-coding-interview-questions/ 49/71
30/05/2025, 23:09 Java Coding Interview Questions + Answers (With Code Examples) | Zero To Mastery
Creational patterns
Singleton
For example
class Singleton {
private static Singleton ins
private Singleton() {}
Factory Method
https://zerotomastery.io/blog/java-coding-interview-questions/ 50/71
30/05/2025, 23:09 Java Coding Interview Questions + Answers (With Code Examples) | Zero To Mastery
Builder
Structural patterns
Adapter
Decorator
interface DataSource {
void writeData(String data);
String readData();
}
public EncryptionDecorator(D
this.wrapped = source;
}
https://zerotomastery.io/blog/java-coding-interview-questions/ 51/71
30/05/2025, 23:09 Java Coding Interview Questions + Answers (With Code Examples) | Zero To Mastery
Proxy
Behavioral patterns
Observer
import java.util.ArrayList;
import java.util.List;
class Subject {
private List<Observer> obser
interface Observer {
void update();
}
Strategy
Command
https://zerotomastery.io/blog/java-coding-interview-questions/ 53/71
30/05/2025, 23:09 Java Coding Interview Questions + Answers (With Code Examples) | Zero To Mastery
https://zerotomastery.io/blog/java-coding-interview-questions/ 54/71
30/05/2025, 23:09 Java Coding Interview Questions + Answers (With Code Examples) | Zero To Mastery
For example
executor.shutdown();
https://zerotomastery.io/blog/java-coding-interview-questions/ 55/71
30/05/2025, 23:09 Java Coding Interview Questions + Answers (With Code Examples) | Zero To Mastery
https://zerotomastery.io/blog/java-coding-interview-questions/ 56/71
30/05/2025, 23:09 Java Coding Interview Questions + Answers (With Code Examples) | Zero To Mastery
For example
CompletableFuture.supplyAsync(()
.thenApply(greet
.thenAccept(Syst
.exceptionally(e
System.out.p
return null;
});
https://zerotomastery.io/blog/java-coding-interview-questions/ 57/71
30/05/2025, 23:09 Java Coding Interview Questions + Answers (With Code Examples) | Zero To Mastery
Language features
https://zerotomastery.io/blog/java-coding-interview-questions/ 58/71
30/05/2025, 23:09 Java Coding Interview Questions + Answers (With Code Examples) | Zero To Mastery
Developer tooling
For example
module com.example.myapp {
requires java.sql;
exports com.example.myapp.se
}
https://zerotomastery.io/blog/java-coding-interview-questions/ 60/71
30/05/2025, 23:09 Java Coding Interview Questions + Answers (With Code Examples) | Zero To Mastery
https://zerotomastery.io/blog/java-coding-interview-questions/ 61/71
30/05/2025, 23:09 Java Coding Interview Questions + Answers (With Code Examples) | Zero To Mastery
For example
For example
https://zerotomastery.io/blog/java-coding-interview-questions/ 62/71
30/05/2025, 23:09 Java Coding Interview Questions + Answers (With Code Examples) | Zero To Mastery
For example
System.out.println(filteredNames
For example
System.out.println(grouped);
// Output: {false=[Bob], true=[A
Comparable
For example
https://zerotomastery.io/blog/java-coding-interview-questions/ 65/71
30/05/2025, 23:09 Java Coding Interview Questions + Answers (With Code Examples) | Zero To Mastery
Comparator
Comparator<Student> nameCompa
Collections.sort(studentList, na
https://zerotomastery.io/blog/java-coding-interview-questions/ 66/71
30/05/2025, 23:09 Java Coding Interview Questions + Answers (With Code Examples) | Zero To Mastery
Key differences
Use case:
Implementation:
https://zerotomastery.io/blog/java-coding-interview-questions/ 67/71
30/05/2025, 23:09 Java Coding Interview Questions + Answers (With Code Examples) | Zero To Mastery
https://zerotomastery.io/blog/java-coding-interview-questions/ 68/71
30/05/2025, 23:09 Java Coding Interview Questions + Answers (With Code Examples) | Zero To Mastery
Email SUBSCRIBE
https://zerotomastery.io/blog/java-coding-interview-questions/ 69/71
30/05/2025, 23:09 Java Coding Interview Questions + Answers (With Code Examples) | Zero To Mastery
Java Programming
Bootcamp: Zero to
Mastery
Learn Java from scratch. The
How To Ace The Coding Interview How To Get A Job In Tech & How to Get More Interviews, More
Succeed When You’re There! Job Offers, and Even a Raise
34 min read
35 min read 20 min read
Are you ready to apply for & land a coding
job but not sure how? This coding Are you looking to get a job in tech? This is Part 3 of a 3 part series on how I
interview guide will show you how it These are the steps (+ tips, tricks, and taught myself programming, and ended
works, how to study, what to learn & resources) from a Senior Developer to up with multiple job offers in less than 6
more! get hired in tech with zero experience! months!
Must Read Web Development Career Advice Web Development Must Read Career Advice
https://zerotomastery.io/blog/java-coding-interview-questions/ 70/71
30/05/2025, 23:09 Java Coding Interview Questions + Answers (With Code Examples) | Zero To Mastery
Design
Cyber Security
Lifetime
Challenge
Learning
Passport
Excellent
4.9 out of 5
https://zerotomastery.io/blog/java-coding-interview-questions/ 71/71