nerdexam
Oracle

1Z0-803 · Question #245

Given: public 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"…

The correct answer is A. Adding a break statement after each print statement C. Changing the string literals in each case label to integer. The following will work fine: public classTest{ public static void main(String[] args) { case 7: System.out.print("Uranus"); break; case 6: System.out.print("Saturn"); break; case 1: System.out.print("Mercury"); break; case 2: System.out.print("Venus"); break; case 3…

Using Operators and Decision Constructs

Question

Given:

public 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

(26 responses)
  • A
    81% (21)
  • B
    4% (1)
  • D
    4% (1)
  • E
    12% (3)

Explanation

The following will work fine: public classTest{ public static void main(String[] args) { case 7: System.out.print("Uranus"); break; case 6: System.out.print("Saturn"); break; case 1: System.out.print("Mercury"); break; case 2: System.out.print("Venus"); break; case 3: System.out.print("Earth"); break; case 4: System.out.print("Mars"); break; case 5: System.out.print("Jupiter"); break;

Topics

#switch statement#control flow#type compatibility#break statement

Community Discussion

No community discussion yet for this question.

Full 1Z0-803 Practice