QuestionQ199

Using Operators and Decision Constructs

Given this code fragment:

Question Image

Which three code fragments can each be independently inserted at line n1 so that the code prints One?

Choose three
  • A byte x = 1;
  • B short x = 1;
  • C String x = "1";
  • D long x = 1;
  • E double x = 1;
  • F Integer x = new Integer("1");
Explanation

byte and short are valid integral selector types, and the constant case 1 is assignment-compatible with both. Integer is also a valid selector type and its value is unboxed to int, matching case 1. Each of these variables has the value 1, so execution prints One. A String selector requires compatible String case labels, and long and double are not supported selector types under the applicable Java switch rules.

Learn more

Community Discussion

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