MCQs
If you examine the code carefully you will notice a missing curly bracket at the end of the
code, this would cause the code to fail.
What will be the output of the program?
public class Switch2
{
final static SHORT x = 2;
public static int y = 0;
public static void main(String [] args)
{
for (int z=0; z < 4; z++)
{
switch (z)
{
case x: System.out.print("0 ");
default: System.out.print("def ");
case x-1: System.out.print("1 ");
break;
case x-2: System.out.print("2 ");
}
}
}
}
When z == 0 , case x-2 is matched. When z == 1, case x-1 is matched and then the
break occurs. When z == 2, case x, then default, then x-1 are all matched. When z == 3,
default, then x-1 are matched. The rules for default are that it will fall through from above
like any other case (for instance when z == 2), and that it will match when no other cases
match (for instance when z==3).
Look closely at line 2, is this an equality check (==) or an assignment (=). The condition
at line 2 evaluates to false and also assigns false to bool. bool is now false so the condition
at line 6 is not true. The condition at line 10 checks to see if bool is not true ( if !(bool == true) ),
it isn't so line 12 is executed.
Because there are no break statements, the program gets to the default case and adds
2 to j, then goes to case 0and adds 4 to the new j. The result is j = 6.
The variable i will have the values 0, 1 and 2.
When i is 0, nothing will be printed because of the break in case 0.
When i is 1, "one two three" will be output because case 1, case 2 and case 3 will be
executed (they don't have break statements).
When i is 2, "two three" will be output because case 2 and case 3 will be executed (again
no break statements).
Finally, when the for loop finishes "done" will be output.
Compilation fails because the argument of the while loop, the condition, must be of
primitive type boolean. In Java, 1 does not represent the true state of a boolean,
rather it is seen as an integer.
'A' is only printed once at the very start as it is in the initialisation SECTION of the for loop.
The loop will only initialise that once.
'B' is printed as it is part of the test carried out in order to run the loop.
'D' is printed as it is in the loop.
'C' is printed as it is in the increment section of the loop and will 'increment' only at the end
of each loop. Here ends the first loop. Again 'B' is printed as part of the loop test.
'D' is printed as it is in the loop.
'C' is printed as it 'increments' at the end of each loop.
Again 'B' is printed as part of the loop test. At this point the test fails because the other part of the
test (i < 2) is no longer true. i has been increased in value by 1 for each loop with the line: i++;
This results in a printout of ABDCBDCB
The switch statement can only be supported by integers or variables more "narrow"
than an integer i.e. byte, char, short. Here a Float wrapper object is used and so the
compilation fails.
Line 3 uses an assignment as opposed to comparison. Because of this, the if statement
receives an integer value instead of a boolean. And so the compilation fails.
Compilation fails on the line 5 - System.out.println(i); as the variable i has only been
declared within the for loop. It is not a recognised variable outside the code block of loop.