1Z0-829 Controlling Program Flow Practice Question
A Java application processes a list of orders. Each order has a status: NEW, PROCESSING, SHIPPED, or DELIVERED. The code must print a message based on the status: - If NEW: "Order received" - If PROCESSING: "Order in progress" - If SHIPPED: "Order shipped" - If DELIVERED: "Order delivered" - For any other status: "Unknown status"
The developer writes the following code using a switch expression:
String message = switch (status) { case NEW -> "Order received"; case PROCESSING -> "Order in progress"; case SHIPPED -> "Order shipped"; case DELIVERED -> "Order delivered"; default -> "Unknown status";
};
System.out.println(message);
What is the correct course of action to ensure the code compiles and runs correctly?
⚠ Common exam trap
Candidates often think switch expressions cannot yield values or that enum types require if-else, but Java's enhanced switch fully supports both, and the default branch is already present, so no change is needed.
Answer choices
Why each option matters
Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.
Correct answer & explanation
✓
No change needed; the code compiles and runs as expected.
The switch expression shown is syntactically valid and complete. It uses the arrow syntax with all enum constants covered and includes a default branch to handle any other status, so it compiles and runs as expected. Switch expressions can yield values and work with enum types, making the code correct as written.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
Add a default branch to handle any other status.
Why it's wrong here
There is already a default branch.
- ✗
Use if-else if statements because switch cannot be used with enum types.
Why it's wrong here
Switch can be used with enums.
- ✓
No change needed; the code compiles and runs as expected.
Why this is correct
The code is syntactically correct and covers all cases.
- ✗
Change the switch expression to a switch statement because switch expressions cannot yield values.
Why it's wrong here
Switch expressions can yield values.
Go deeper
Related to this question
About these practice questions
Courseiva writes every 1Z0-829 question from scratch — 513 in total, each with an explanation and a wrong-answer breakdown. None are copied from real exams or dumps. Learn why practice questions differ from exam dumps →
JA
Written by Johnson Ajibi, MSc IT Security
Senior Network & Security Engineer · founder of Courseiva
This 1Z0-829 practice question is part of Courseiva's free Oracle certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the 1Z0-829 exam.