QuestionQ205

Using Operators and Decision Constructs

Given the following code fragment:

Question Image

Which change allows the code fragment to print TrueDone?

  • A Replace line 5 With String opt = "true"; Replace line 7 with case "true":
  • B Replace line 5 with boolean opt = l; Replace line 7 with case 1:
  • C At line 9, remove the break statement.
  • D Remove the default section.
Explanation

A switch statement cannot use a boolean selector in Java. Using a String selector with the matching case "true": label is valid; it prints True, exits the switch with break, and then prints Done.

Community Discussion

No comments yet. Be the first to start the discussion!