Chapter 5: Flow control -sample exam questions
What's the output of the following code? class EJavaGuru3 { public static void main(String args[]) { byte foo = 120; switch (foo) { default: System.out.println("ejavaguru"); break; case 2: System.out.println("e"); break; case 120: System.out.println("ejava"); case 121: System.out.println("enum"); case 127: System.out.println("guru"); break; } } } a ejava enum guru b ejava c ejavaguru e d ejava enum guru ejavaguru
Answer: a Explanation: For a switch case construct, control enters the case labels when a matching case is found. The control then falls through the remaining case labels until it's terminated by a break statement. The control exits the switch construct when it encounters a break statement or it reaches the end of the switch construct.
Given the following code, which of the following lines of code can individually replace the //INSERT CODE HERE line so that the code compiles successfully? class EJavaGuru { public static int getVal() { return 100; } public static void main(String args[]) { int num = 10; final int num2 = 20; switch (num) { // INSERT CODE HERE break; default: System.out.println("default"); } } } a case 10*3: System.out.println(2); b case num: System.out.println(3); c case 10/3: System.out.println(4); d case num2: System.out.println(5);
Answer: a, c, d Explanation: Option (a) is correct. Compile-time constants, including expressions, are permissible in the case labels. Option (b) is incorrect. The case labels should be compile-time constants. A nonfinal variable isn't a compile-time constant because it can be reassigned a value during the course of a class's execution. Although the previous class doesn't assign a value to it, the compiler still treats it as a changeable variable. Option (c) is correct. The value specified in the case labels should be assignable to the variable used in the switch construct. You may think that 10/3 will return a decimal number, which can't be assigned to the variable num, but this operation discards the decimal part and compares 3 with the variable num. Option (d) is correct. The variable num2 is defined as a final variable and assigned a value on the same line of code, with its declaration. Hence, it's considered to be a compile-time constant.
What's the output of the following code? int a = 10; if (a++ > 10) { System.out.println("true"); } { System.out.println("false"); } System.out.println("ABC"); a true false ABC b false ABC c true ABC d Compilation error
Answer: b Explanation: First of all, the code has no compilation errors. This question has a trick—the following code snippet isn't part of the if construct: { System.out.println("false"); }
What's the output of the following code? class EJavaGuru4 { public static void main(String args[]) { boolean myVal = false; if (myVal=true) for (int i = 0; i < 2; i++) System.out.println(i); else System.out.println("else"); } } a else b 0 1 2 c 0 1 d Compilation error
Answer: c Explanation: First of all, the expression used in the if construct isn't comparing the value of the variable myVal with the literal value true—it's assigning the literal value true to it. The assignment operator (=) assigns the literal value. The comparison operator (==) is used to compare values. Because the resulting value is a boolean value, the compiler doesn't complain about the assignment in the if construct.
Which of the following statements is true? a The enhanced for loop can't be used within a regular for loop. b The enhanced for loop can't be used within a while loop. c The enhanced for loop can be used within a do-while loop. d The enhanced for loop can't be used within a switch construct. e All of the above statements are false.
Answer: c Explanation: The enhanced for loop can be used within all types of looping and conditional constructs. Notice the use of "can" and "can't" in the answer options. It's important to take note of these subtle differences.
What's the output of the following code? class EJavaGuru { public static void main(String args[]) { int num = 120; switch (num) { default: System.out.println("default"); case 0: System.out.println("case1"); case 10*2-20: System.out.println("case2"); break; } } } a default case1 case2 b case1 case2 c case2 d Compilation error e Runtime exception
Answer: d Explanation: The expressions used for both case labels—that is, 0 and 10*2-20—evaluate to the constant value 0. Because you can't define duplicate case labels for the switch statement, the code will fail to compile with an error message that states that the code defines a duplicate case label.
What's the output of the following code? class EJavaGuru { public static void main(String args[]) { int num = 20; final int num2; num2 = 20; switch (num) { default: System.out.println("default"); case num2: System.out.println(4); break; } } } a default b default 4 c 4 d Compilation error
Answer: d Explanation: The code will fail to compile. The case labels require compile-time constant values, and the variable num2 doesn't qualify as such. Although the variable num2 is defined as a final variable, it isn't assigned a value with its declaration.
What's the output of the following code? class Loop2 { public static void main(String[] args) { int i = 10; do while (i++ < 15) i = i + 20; while (i < 2); System.out.println(i); } } a 10 b 30 c 31 d 32
Answer: d) 32 it uses a postfix unary operator in the while condition. The condition specified in the do-while loop evaluates to false (because 10<2 evaluates to false). But the control enters the do-while loop because the do-while loop executes at least once—its condition is checked at the end of the loop. This question prints outs 32, not 30, because the condition specified in the while loop (which has an increment operator) executes twice.
What's the output of the following code? class EJavaGuru5 { public static void main(String args[]) { int i = 0; for (; i < 2; i=i+5) { if (i < 5) continue; System.out.println(i); } System.out.println(i); } } a Compilation error b 0 5 c 0 5 10 d 10 e 0 1 5 f 5
Answer: f For the first for iteration, the variable i has a value of 0. Because this value is less than 2, the following if construct evaluates to true and the continue statement executes: if (i < 5) continue; Because the continue statement ignores all of the remaining statements in a for loop iteration, the control doesn't print the value of the variable i, which leads the control to move on to the next for iteration. In the next for iteration, the value of the variable i is 5. The for loop condition evaluates to false and the control moves out of the for loop. After the for loop, the code prints out the value of the variable i, which increments once using the code i=i+5.
What's the output of the following code? class Loop2 { public static void main(String[] args) { int i = 10; do while (i < 15) i = i + 20; while (i < 2); System.out.println(i); } } a 10 b 30 c 31 d 32
b) Explanation: The condition specified in the do-while loop evaluates to false (because 10<2 evaluates to false). But the control enters the do-while loop because the dowhile loop executes at least once—its condition is checked at the end of the loop. The while evaluates to true for the first iteration and adds 20 to i, making it 30. The while loop doesn't execute for the second time. Hence, the value of the variable i at the end of the execution of the previous code is 30.