PrepPilotPractise free

Core Java interview questions

Collections, generics, streams, records, exceptions, equals/hashCode

19 questions · Java 21 · updated November 2026

Practise these as flashcards. One or two lines each; the ones you miss come back first.

Start free

New this month

What happens if you attempt to modify a collection created with List.of() and why?

Collections created with List.of() are immutable, so any attempt to add, remove, or modify elements will throw an UnsupportedOperationException. This is because List.of() returns an instance of an unmodifiable list introduced in Java 9.

How do Java records handle equals() and hashCode() implementations, and what should you be aware of when using them in collections?

Java records automatically generate equals() and hashCode() methods based on all component fields, ensuring value-based equality. When using records as keys in collections like HashMap or HashSet, this behavior ensures correct lookup and uniqueness as long as the fields used are themselves properly comparable and hashable.

Core questions

What is the difference between JDK, JRE, and JVM?

The JVM runs Java bytecode, the JRE includes the JVM and standard libraries for running Java apps, and the JDK adds development tools like the compiler and debugger. You need the JDK to develop Java, but only the JRE to run it.

What is the equals/hashCode contract?

The contract states that if two objects are equal according to equals(), they must have the same hashCode. This is important for correct behavior in hash-based collections like HashMap and HashSet.

What is the String pool in Java?

The String pool is a special memory area where Java stores String literals. If you create a String literal, Java checks the pool first and reuses the object if it already exists, saving memory.

What is the difference between ArrayList and LinkedList?

ArrayList uses a dynamic array and offers fast random access (O(1)), but slow insert/delete in the middle. LinkedList uses nodes and is fast for insert/delete (O(1)), but slow for random access (O(n)).

What happens if you attempt to modify a collection created with List.of() and why?

Collections created with List.of() are immutable, so any attempt to add, remove, or modify elements will throw an UnsupportedOperationException. This is because List.of() returns an instance of an unmodifiable list introduced in Java 9.

Intermediate questions

How does HashMap work internally?

HashMap stores entries in buckets based on hashCode. Collisions are handled by chaining (linked lists), and since Java 8, by tree nodes if collisions are high. It resizes automatically and aims for O(1) access, but performance can degrade if hashCode is poorly implemented.

Explain the difference between checked and unchecked exceptions.

Checked exceptions must be declared or caught, like IOException. Unchecked exceptions, like NullPointerException, extend RuntimeException and don't need to be declared. Checked exceptions are for recoverable errors, unchecked for programming mistakes.

What is type erasure in Java generics?

Type erasure means generic type information is removed at runtime. For example, List<String> and List<Integer> are both just List at runtime. This limits certain operations, like using instanceof with generic types.

What is the purpose of Optional in Java?

Optional is a container that may or may not hold a value. It helps avoid null checks and makes it clear when a value might be absent, reducing NullPointerExceptions and improving code readability.

How do you use try-with-resources?

try (BufferedReader br = new BufferedReader(new FileReader("file.txt"))) { br.readLine(); }

What is the difference between interfaces and abstract classes?

Interfaces define contracts and can have default methods but no state. Abstract classes can have state and provide partial implementation. Use interfaces for API contracts and abstract classes for shared code and fields.

What are wildcards in Java generics?

Wildcards allow flexible APIs. ? extends T means any subtype of T, useful for reading; ? super T means any supertype, useful for writing. They help make generic methods more reusable.

How do you use ExecutorService and CompletableFuture together?

See the answer in the app, free

How do Java records handle equals() and hashCode() implementations, and what should you be aware of when using them in collections?

See the answer in the app, free

Senior questions

How would you implement a thread-safe cache in Java?

See the answer in the app, free

Explain the advantages and trade-offs between synchronized and Lock.

See the answer in the app, free

How do virtual threads in Java 21 improve concurrency?

See the answer in the app, free

How would you design a generic API that accepts both lists of numbers and lists of strings?

See the answer in the app, free

How do you debug a memory leak caused by improper use of collections?

See the answer in the app, free

More Java topics