Advanced Java interview questions

11 questions from the Advanced Java: Streams, Concurrency, JVM and Spring course, each with the correct answer and an explanation. Try to answer before you open the answer.

Module 2: Concurrency

1. What are virtual threads best suited for?

  1. Many concurrent blocking I/O tasks
  2. CPU-bound number crunching only
  3. Replacing the garbage collector
  4. Compiling code
Show answer

Answer: Many concurrent blocking I/O tasks

They make blocking I/O cheap so you can scale thread-per-task code.

Lesson: Virtual Threads (Java 21)

Module 5: Enterprise Java, Spring & REST

2. A client sends valid credentials but tries to delete another user's order. What status code fits best?

  1. 401 Unauthorized
  2. 404 Not Found
  3. 403 Forbidden
  4. 500 Internal Server Error
Show answer

Answer: 403 Forbidden

401 means missing/invalid credentials; here the client is authenticated but not allowed to act on this resource — that's 403.

Lesson: API Status Codes & Error Handling

Module 7: Design Patterns

3. What problem does the Observer pattern primarily solve?

  1. Guaranteeing only one instance of a class exists
  2. Letting an object notify many interested parties about a change without depending on their concrete types
  3. Building complex objects step by step
  4. Choosing which concrete class to instantiate at runtime
Show answer

Answer: Letting an object notify many interested parties about a change without depending on their concrete types

That's Singleton (0), Builder (2), and Factory (3) respectively — Observer is about one-to-many change notification.

Lesson: Observer Pattern

Module 10: Cloud-Native Java

4. Which feature lets you pass behaviour as an argument?

  1. Static imports
  2. Annotations
  3. Lambda expressions
  4. Packages
Show answer

Answer: Lambda expressions

Lambdas implement functional interfaces.

Lesson: Reactive Programming Intro

5. What does Stream.map do?

  1. Filters elements
  2. Terminates the stream
  3. Sorts keys
  4. Transforms each element
Show answer

Answer: Transforms each element

map applies a function to each element.

Lesson: Reactive Programming Intro

6. Which class safely handles absent values?

  1. Optional
  2. Object
  3. Void
  4. Number
Show answer

Answer: Optional

Optional makes missing values explicit.

Lesson: Reactive Programming Intro

7. What does happens-before guarantee?

  1. Faster code
  2. One thread's writes are visible to another
  3. No exceptions
  4. Automatic locks
Show answer

Answer: One thread's writes are visible to another

It defines memory visibility between threads.

Lesson: Reactive Programming Intro

8. Which tool manages thread pools?

  1. Scanner
  2. StringBuilder
  3. ExecutorService
  4. Properties
Show answer

Answer: ExecutorService

Executors decouple tasks from threads.

Lesson: Reactive Programming Intro

9. Which Spring concept supplies dependencies to your classes?

  1. Reflection only
  2. JSP
  3. Servlets
  4. Dependency injection
Show answer

Answer: Dependency injection

The IoC container wires beans together.

Lesson: Reactive Programming Intro

10. Why hash passwords with bcrypt?

  1. So plain text is never stored
  2. To make them shorter
  3. To speed up login
  4. To allow decryption
Show answer

Answer: So plain text is never stored

Hashing protects users if data leaks.

Lesson: Reactive Programming Intro

11. What do virtual threads make cheap?

  1. CPU-bound loops
  2. Blocking I/O concurrency
  3. Memory leaks
  4. Compilation
Show answer

Answer: Blocking I/O concurrency

The JVM parks blocked virtual threads.

Lesson: Reactive Programming Intro

← Back to the course