The answer is a switch selector type mismatch compilation error, caused by using a String selector with int case labels. In Java, the switch selector and its case labels must be type-compatible; a String selector requires case labels that are String constants or constant expressions assignable to String, not int literals like 1 or 2. This rule enforces type safety at compile time, preventing implicit type conversions between unrelated types. On the Oracle Certified Professional Java SE 17 Developer 1Z0-829 exam, this concept frequently appears in questions testing your understanding of switch expressions and pattern matching, often as a trap where you might assume automatic widening or boxing applies. A common memory tip: think of the switch selector as the “key” and case labels as the “locks”—they must be made of the same material to fit.
1Z0-829 Controlling Program Flow Practice Question
This 1Z0-829 practice question tests your understanding of controlling program flow. Read the scenario carefully and evaluate each option against the stated constraints before committing to an answer. After answering, compare your reasoning against the explanation and wrong-answer breakdown below. Once you have made your selection, read the full explanation to reinforce the concept and understand why each distractor is designed to mislead on exam day.
Exhibit
Refer to the exhibit.
```
Error: incompatible types: bad type in switch expression
return switch(day) {
^
required: int
found: String
```
What is the likely cause of this compilation error?
Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.
Correct answer & explanation
✓
The switch selector is a String but case labels are int constants.
Option D is correct because in a Java switch expression, the type of the selector (String) must be compatible with the case label types. Here, the case labels are int constants (e.g., 1, 2), which are not assignable from a String selector. Java requires that case labels be of the same type as the selector or be constant expressions that are assignment-compatible (e.g., String constants for a String selector). This mismatch causes a compilation error.
Key principle: Answer the scenario, not the keyword: identify the specific constraint before choosing the most familiar-sounding option.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
✗
The switch expression uses arrow syntax but the keys are not unique.
Why it's wrong here
Duplicate case labels would be a different error.
✗
The switch expression is missing a default case.
Why it's wrong here
Missing default gives a different error about not all values covered.
✗
The switch expression is not assigned to a variable.
Why it's wrong here
Switch expression must be assigned or used; but missing assignment gives a different error.
✓
The switch selector is a String but case labels are int constants.
Why this is correct
Switch expression requires consistent types; likely String selector with int cases.
Related concept
Read the scenario before looking for a memorised answer.
Common exam traps
Common exam trap: answer the scenario, not the keyword
The trap here is that candidates often overlook the type compatibility requirement and focus on syntax details like arrow syntax or missing default, assuming the error is about completeness or assignment rather than a fundamental type mismatch.
Detailed technical explanation
How to think about this question
Under the hood, Java switch expressions enforce strict type compatibility: the selector and case labels must be of the same type (or convertible via constant expressions). For String selectors, case labels must be String literals or constants (e.g., final String variables). This is because the compiler generates a hash-based or lookup table for String switches, which requires all case labels to be compile-time constants of the same type. A subtle behavior is that enum selectors also require case labels to be enum constants, not ordinal integers.
KKey Concepts to Remember
Read the scenario before looking for a memorised answer.
Find the constraint that changes the correct option.
Eliminate answers that are true in general but not in this case.
TExam Day Tips
→Watch for words such as best, first, most likely and least administrative effort.
→Review why wrong options are wrong, not only why the correct option is correct.
Key takeaway
Answer the scenario, not the keyword: identify the specific constraint before choosing the most familiar-sounding option.
Real-world example
How this comes up in practice
A small business has 20 workstations on the 192.168.1.0/24 network and one public IP from its ISP. The router uses PAT (NAT overload) so all 20 devices share one public address using different source ports. NAT questions test whether you understand the four address terms and which direction each translation applies.
What to study next
Got this wrong? Here's your next step.
Identify which exam domain this question belongs to, review the core concept, then practise similar questions from the same domain.
Controlling Program Flow — This question tests Controlling Program Flow — Read the scenario before looking for a memorised answer..
What is the correct answer to this question?
The correct answer is: The switch selector is a String but case labels are int constants. — Option D is correct because in a Java switch expression, the type of the selector (String) must be compatible with the case label types. Here, the case labels are int constants (e.g., 1, 2), which are not assignable from a String selector. Java requires that case labels be of the same type as the selector or be constant expressions that are assignment-compatible (e.g., String constants for a String selector). This mismatch causes a compilation error.
What should I do if I get this 1Z0-829 question wrong?
Identify which exam domain this question belongs to, review the core concept, then practise similar questions from the same domain.
What is the key concept behind this question?
Read the scenario before looking for a memorised answer.
About these practice questions
Courseiva creates original exam-style practice questions with explanations and wrong-answer analysis. It does not publish real exam questions, exam dumps, or protected exam content. Learn why practice questions differ from exam dumps →
Share a tip, memory trick, or ask about the reasoning behind this question. Do not post real exam questions, leaked content, braindumps, or copyrighted exam material. Comments are moderated and may be removed without notice.
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.
Question Discussion
Share a tip, memory trick, or ask about the reasoning behind this question. Do not post real exam questions, leaked content, braindumps, or copyrighted exam material. Comments are moderated and may be removed without notice.
Sign in to join the discussion.