nerdexam
Oracle

1Z0-803 · Question #120

Given: class Test { public static void main (String[] args) { int day = 1; switch (day) { case "7": System.out.print("Uranus"); case "6": System.out.print("Saturn"); case "1": System.out.print("Mercur

The correct answer is C. changing the string literals in each case label to integer D. changing the type of the variable day to String. The provided Java code fails to compile because the switch statement attempts to use String literals for its case labels while the switch variable day is an int.

Using Operators and Decision Constructs

Question

Given:

class Test { public static void main (String[] args) { int day = 1; switch (day) { case "7":

System.out.print("Uranus"); case "6":

System.out.print("Saturn"); case "1":

System.out.print("Mercury"); case "2":

System.out.print("Venus"); case "3":

System.out.print("Earth"); case "4":

System.out.print("Mars"); case "5":

System.out.print("Jupiter"); } } } Which two modifications, made independently, enable the code to compile and run?

Options

  • Aadding a break statement after each print statement
  • Badding a default section within the switch code-block
  • Cchanging the string literals in each case label to integer
  • Dchanging the type of the variable day to String
  • Earranging the case labels in ascending order

How the community answered

(23 responses)
  • A
    9% (2)
  • B
    4% (1)
  • C
    83% (19)
  • E
    4% (1)

Why each option

The provided Java code fails to compile because the `switch` statement attempts to use `String` literals for its `case` labels while the `switch` variable `day` is an `int`.

Aadding a break statement after each print statement

Adding `break` statements would change the runtime behavior by preventing fall-through, but it does not resolve the compile-time type mismatch error between the `int` switch variable and `String` case labels.

Badding a default section within the switch code-block

Adding a `default` section would provide a fallback if no other case matches, but it does not address the fundamental compile-time type mismatch error in the existing `case` labels.

Cchanging the string literals in each case label to integerCorrect

Changing the string literals in each case label to integer literals (e.g., `case "7"` to `case 7`) resolves the type mismatch, allowing the `switch` statement to correctly compare the `int` variable `day` with `int` case values.

Dchanging the type of the variable day to StringCorrect

Changing the type of the variable `day` to `String` (e.g., `String day = "1";`) resolves the type mismatch, allowing the `switch` statement to correctly compare the `String` variable `day` with `String` case labels.

Earranging the case labels in ascending order

Arranging the case labels in ascending order has no impact on the compilation success or failure and does not resolve the type mismatch issue.

Concept tested: Java switch statement type compatibility

Source: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html

Topics

#switch statement#control flow#type compatibility#literals

Community Discussion

No community discussion yet for this question.

Full 1Z0-803 Practice