Skip to content
Sharp

Modern Java · interview question

Given the following code, what will be printed if the 'status' is 'PENDING'? enum OrderStatus { PENDING, SHIPPED, DELIVERED } OrderStatus status = OrderStatus.PENDING; String message = switch (status) { case SHIPPED -> "Order shipped."; case DELIVERED -> "Order delivered."; }; System.out.println(message);?

Multiple choiceHardchapter-12-curatedpremiummodern-javaswitch-expressions
java
enum OrderStatus { PENDING, SHIPPED, DELIVERED }
OrderStatus status = OrderStatus.PENDING;
String message = switch (status) {
  case SHIPPED -> "Order shipped.";
  case DELIVERED -> "Order delivered.";
};
System.out.println(message);
Choose one answer
Answer and explanation

Correct answer

A. Compilation error

Why

The switch expression lacks a case for 'PENDING' and a default case, resulting in a compilation error due to non-exhaustiveness in handling all enum constants.

Test yourself on Modern Java

A free test with instant, explained results. No account needed.

Start free test
All Modern Java questions →