MCQs
Compilation fails because the while loop demands a boolean argument for it's looping
condition, but in the code, it's given an int argument.
while(true) { //insert code here }
The program flows as follows: I will be incremented after the while loop is entered,
then I will be incremented (by zero) when the for loop is entered. The if statement
evaluates to false, and the continue statement is never reached. The break statement
tells the JVM to break out of the outer loop, at which point I is printed and the fragment
is done.
In Java, boolean instance variables are initialized to false, so the if test on line 7 is
true and hand is incremented. Line 9 is legal syntax, a do nothing statement. The
else-if is true so hand has 7 added to it and is then incremented.
There are two different looping constructs in this problem. The first is a do-while loop
and the second is a while loop, nested inside the do-while. The body of the do-while
is only a single statement-brackets are not needed. You are assured that the while
expression will be evaluated at least once, followed by an evaluation of the do-while
expression. Both expressions are false and no output is produced.
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 < 3; z++)
{
switch (z)
{
case y: System.out.print("0 "); /* Line 11 */
case x-1: System.out.print("1 "); /* Line 12 */
case x: System.out.print("2 "); /* Line 13 */
}
}
}
}
Case expressions must be constant expressions. Since x is marked final, lines
12 and 13 are legal; however y is not a final so the compiler will fail at line 11.
This loop is a do-while loop, which always executes the code block within the block at
least once, due to the testing condition being at the end of the loop, rather than at the
beginning. This particular loop is exited prematurely if ibecomes greater than j.
The order is, test i against j, if bigger, it breaks from the loop, decrements j by one, and
then tests the loop condition, where a pre-incremented by one i is tested for being lower
than 5. The test is at the end of the loop, so ican reach the value of 5 before it fails. So it
goes, start:
1, 10
2, 9
3, 8
4, 7
5, 6 loop condition fails.
What will be the output of the program?
public class SwitchTest
{
public static void main(String[] args)
{
System.out.println("value =" + switchIt(4));
}
public static int switchIt(int x)
{
int j = 1;
switch (x)
{
case l: j++;
case 2: j++;
case 3: j++;
case 4: j++;
case 5: j++;
default: j++;
}
return j + x;
}
}
Because there are no break statements, once the desired result is found, the program
continues though each of the remaining options.
What will be the output of the program?
public class If2
{
static boolean b1, b2;
public static void main(String [] args)
{
int x = 0;
if ( !b1 ) /* Line 7 */
{
if ( !b2 ) /* Line 9 */
{
b1 = true;
x++;
if ( 5 > 6 )
{
x++;
}
if ( !b1 )
x = x + 10;
else if ( b2 = true ) /* Line 19 */
x = x + 100;
else if ( b1 | b2 ) /* Line 21 */
x = x + 1000;
}
}
System.out.println(x);
}
}
As instance variables, b1 and b2 are initialized to false. The if tests on lines 7
and 9 are successful so b1 is set to true and x is incremented. The next if test
to succeed is on line 19 (note that the code is not testing to see if b2 is true, it is
setting b2 to be true). Since line 19 was successful, subsequent else-if's (line 21)
will be skipped.
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 < 3; z++)
{
switch (z)
{
case x: System.out.print("0 ");
case x-1: System.out.print("1 ");
case x-2: System.out.print("2 ");
}
}
}
}
The case expressions are all legal because x is marked final, which means the
expressions can be evaluated at compile time. In the first iteration of the for loop
case x-2 matches, so 2 is printed. In the second iteration, x-1 is matched so 1
and 2 are printed (remember, once a match is found all remaining statements
are executed until a break statement is encountered). In the third iteration, x
is matched. So 0 1 and 2 are printed.
The case statement takes only a single argument. The case statement on line 4 is
given two arguments so the compiler complains.