PrepAhead
Start free
← All posts

Java backend interview quiz (10 questions)

May 28, 2026

  • Java
  • Mid-level
  • Backend

These questions are intentionally phrased like live interview prompts: short scenario, concrete trade-off, and one best answer you can defend out loud. Use them to warm up before a Java backend screen or to spot areas where your explanations are still too vague.

When you are ready for practice grounded in an actual job posting, generate a tailored set in PrepAhead instead of relying only on generic interview drills.

Question 1

You need an ordered collection that allows duplicates and is usually read by index. Which type is the best default choice?

Pick an answer to see if you are correct
Reveal answer

Answer

ArrayList - ArrayList is the best default choice when order matters, duplicates are allowed, and indexed reads are common.

Explanation

ArrayList preserves insertion order, allows duplicates, and gives O(1) indexed access. HashSet removes duplicates. TreeMap is a key-value structure, not a List. PriorityQueue orders by priority rather than preserving insertion order.

Common interview notes

Interviewers often follow up by asking when LinkedList is preferable. A strong answer mentions queue-like operations or frequent inserts/removes at known positions, not random access.

Question 2

A boolean stop flag is written by one thread and read by another, and you do not need compound atomic updates. What does volatile give you here?

Pick an answer to see if you are correct
Reveal answer

Answer

Visibility of writes across threads - volatile gives you visibility of writes across threads.

Explanation

A volatile write becomes visible to other threads that read the same field. It does not make compound operations like count++ atomic, and it does not replace locking when multiple operations must happen together safely.

Common interview notes

A common follow-up is how volatile differs from synchronized or AtomicInteger.

Question 3

Which statement about String in Java is correct?

Pick an answer to see if you are correct
Reveal answer

Answer

String literals may be interned in the string pool - String literals may be interned in the string pool.

Explanation

String is immutable regardless of how it is constructed. equals compares content, while == compares references. In loops, StringBuilder is usually preferred over repeated + because it avoids many intermediate String allocations.

Common interview notes

Strong candidates can explain when intern() is useful and why overusing it is rarely necessary in application code.

Question 4

In Java 17+, you need a compact type for an immutable response object that is mostly data. Which feature is the best fit?

Pick an answer to see if you are correct
Reveal answer

Answer

record - record is the best fit for a compact immutable data carrier.

Explanation

Records are ideal for simple data-focused models because they generate accessors, constructor, equals, hashCode, and toString automatically. enum models a fixed set of constants. interface and abstract class solve different design problems.

Common interview notes

A good follow-up is when not to use a record, for example when the type has substantial mutable state or complicated lifecycle rules.

Question 5

What happens when an unchecked exception is thrown in a method and no matching catch handles it there?

Pick an answer to see if you are correct
Reveal answer

Answer

It propagates to the caller - The exception propagates to the caller until some frame handles it or the thread terminates.

Explanation

Runtime exceptions do not need to be declared, but they still follow normal stack unwinding. finally blocks run during that unwinding, yet they do not magically swallow the exception unless code explicitly does so.

Common interview notes

Interviewers may ask you to compare checked exceptions, runtime exceptions, and Error.

Question 6

A service needs to run tasks asynchronously using a bounded thread pool instead of creating raw threads by hand. Which API is the best starting point?

Pick an answer to see if you are correct
Reveal answer

Answer

ExecutorService - ExecutorService is the standard starting point for managing a pool of worker threads and submitted tasks.

Explanation

ExecutorService separates task submission from thread management and supports fixed pools, futures, graceful shutdown, and queueing. synchronized controls access to shared state, not task execution. The other choices are unrelated.

Common interview notes

Good answers often mention shutdown(), Future, and the difference between CPU-bound and IO-bound workloads.

Question 7

Which access level gives package-private visibility in Java?

Pick an answer to see if you are correct
Reveal answer

Answer

(no modifier / default) - Package-private visibility is the default when you do not write an access modifier.

Explanation

public is visible everywhere, private only inside the declaring class, and protected is visible to the package plus subclasses. The default visibility sits between private and protected for package-level API design.

Common interview notes

A useful follow-up is why reducing visibility can make APIs safer and easier to evolve.

Question 8

You are iterating over Map<String, Integer> counts and need both the key and the value in the loop. Which style is the clearest and most idiomatic?

Pick an answer to see if you are correct
Reveal answer

Answer

Use entrySet() iteration or forEach((k, v) -> ...) - entrySet() iteration or Map.forEach is the clearest way to access both keys and values together.

Explanation

entrySet() gives you direct access to both pieces of data in one pass. values() loses the key. Converting to arrays adds unnecessary work. Stream.of(map) does not flatten the map into entries.

Common interview notes

This kind of question often checks whether you reach for the simplest readable collection view first.

Question 9

You are building a CSV line inside a loop. Which class is the best default choice for repeated string concatenation in single-threaded code?

Pick an answer to see if you are correct
Reveal answer

Answer

StringBuilder - StringBuilder is the best default choice for repeated concatenation in single-threaded code.

Explanation

StringBuilder is mutable, so it avoids creating many intermediate String objects. StringBuffer is synchronized and usually unnecessary unless multiple threads truly share the same builder. Raw char arrays are too low-level for most application code.

Common interview notes

Interviewers may ask when StringBuffer is still relevant or how the compiler handles simple constant concatenation.

Question 10

What is the safest assumption to make about finalize(), which is deprecated?

Pick an answer to see if you are correct
Reveal answer

Answer

It may never run, so you should not rely on it for cleanup - You should assume finalize may never run and must not be relied on for resource cleanup.

Explanation

finalize is deprecated because it is unpredictable and harmful for correctness and performance. Use try-with-resources, explicit close methods, and well-defined ownership of resources instead.

Common interview notes

A strong answer usually mentions AutoCloseable and try-with-resources immediately.

Full answer key

Review every answer in one place, with explanations and interview tips below.

1. You need an ordered collection that allows duplicates and is usually read by index. Which type is the best default choice?

Answer summary

ArrayList — ArrayList is the best default choice when order matters, duplicates are allowed, and indexed reads are common.

ArrayList preserves insertion order, allows duplicates, and gives O(1) indexed access. HashSet removes duplicates. TreeMap is a key-value structure, not a List. PriorityQueue orders by priority rather than preserving insertion order.

Interview tip: Interviewers often follow up by asking when LinkedList is preferable. A strong answer mentions queue-like operations or frequent inserts/removes at known positions, not random access.

2. A boolean stop flag is written by one thread and read by another, and you do not need compound atomic updates. What does volatile give you here?

Answer summary

Visibility of writes across threads — volatile gives you visibility of writes across threads.

A volatile write becomes visible to other threads that read the same field. It does not make compound operations like count++ atomic, and it does not replace locking when multiple operations must happen together safely.

Interview tip: A common follow-up is how volatile differs from synchronized or AtomicInteger.

3. Which statement about String in Java is correct?

Answer summary

String literals may be interned in the string pool — String literals may be interned in the string pool.

String is immutable regardless of how it is constructed. equals compares content, while == compares references. In loops, StringBuilder is usually preferred over repeated + because it avoids many intermediate String allocations.

Interview tip: Strong candidates can explain when intern() is useful and why overusing it is rarely necessary in application code.

4. In Java 17+, you need a compact type for an immutable response object that is mostly data. Which feature is the best fit?

Answer summary

record — record is the best fit for a compact immutable data carrier.

Records are ideal for simple data-focused models because they generate accessors, constructor, equals, hashCode, and toString automatically. enum models a fixed set of constants. interface and abstract class solve different design problems.

Interview tip: A good follow-up is when not to use a record, for example when the type has substantial mutable state or complicated lifecycle rules.

5. What happens when an unchecked exception is thrown in a method and no matching catch handles it there?

Answer summary

It propagates to the caller — The exception propagates to the caller until some frame handles it or the thread terminates.

Runtime exceptions do not need to be declared, but they still follow normal stack unwinding. finally blocks run during that unwinding, yet they do not magically swallow the exception unless code explicitly does so.

Interview tip: Interviewers may ask you to compare checked exceptions, runtime exceptions, and Error.

6. A service needs to run tasks asynchronously using a bounded thread pool instead of creating raw threads by hand. Which API is the best starting point?

Answer summary

ExecutorService — ExecutorService is the standard starting point for managing a pool of worker threads and submitted tasks.

ExecutorService separates task submission from thread management and supports fixed pools, futures, graceful shutdown, and queueing. synchronized controls access to shared state, not task execution. The other choices are unrelated.

Interview tip: Good answers often mention shutdown(), Future, and the difference between CPU-bound and IO-bound workloads.

7. Which access level gives package-private visibility in Java?

Answer summary

(no modifier / default) — Package-private visibility is the default when you do not write an access modifier.

public is visible everywhere, private only inside the declaring class, and protected is visible to the package plus subclasses. The default visibility sits between private and protected for package-level API design.

Interview tip: A useful follow-up is why reducing visibility can make APIs safer and easier to evolve.

8. You are iterating over Map<String, Integer> counts and need both the key and the value in the loop. Which style is the clearest and most idiomatic?

Answer summary

Use entrySet() iteration or forEach((k, v) -> ...) — entrySet() iteration or Map.forEach is the clearest way to access both keys and values together.

entrySet() gives you direct access to both pieces of data in one pass. values() loses the key. Converting to arrays adds unnecessary work. Stream.of(map) does not flatten the map into entries.

Interview tip: This kind of question often checks whether you reach for the simplest readable collection view first.

9. You are building a CSV line inside a loop. Which class is the best default choice for repeated string concatenation in single-threaded code?

Answer summary

StringBuilder — StringBuilder is the best default choice for repeated concatenation in single-threaded code.

StringBuilder is mutable, so it avoids creating many intermediate String objects. StringBuffer is synchronized and usually unnecessary unless multiple threads truly share the same builder. Raw char arrays are too low-level for most application code.

Interview tip: Interviewers may ask when StringBuffer is still relevant or how the compiler handles simple constant concatenation.

10. What is the safest assumption to make about finalize(), which is deprecated?

Answer summary

It may never run, so you should not rely on it for cleanup — You should assume finalize may never run and must not be relied on for resource cleanup.

finalize is deprecated because it is unpredictable and harmful for correctness and performance. Use try-with-resources, explicit close methods, and well-defined ownership of resources instead.

Interview tip: A strong answer usually mentions AutoCloseable and try-with-resources immediately.

Try each question on your own, then open "Reveal answer" for the full write-up. For tailored practice and Check feedback on open-ended responses, use PrepAhead in the app - not on blog pages.