Java Programming - Flow Control

Exercise : Flow Control - Finding the output
6.
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 */
            }
        }
    }
}
0 1 2
0 1 2 1 2 2
Compilation fails at line 11.
Compilation fails at line 12.
Answer: Option
Explanation:

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.


7.
What will be the output of the program?
public class If1 
{
    static boolean b;
    public static void main(String [] args) 
    {
        short hand = 42;
        if ( hand < 50 && !b ) /* Line 7 */
            hand++;
        if ( hand > 50 );     /* Line 9 */
        else if ( hand > 40 ) 
        {
            hand += 7;
            hand++;    
        }
        else
            --hand;
        System.out.println(hand);
    }
}
41
42
50
51
Answer: Option
Explanation:

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.


8.
What will be the output of the program?
public class Test 
{
    public static void main(String [] args) 
    {
        int I = 1;
        do while ( I < 1 )
        System.out.print("I is " + I);
        while ( I > 1 ) ;
    }
}
I is 1
I is 1 I is 1
No output is produced.
Compilation error
Answer: Option
Explanation:

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.


9.
What will be the output of the program?
int x = l, y = 6; 
while (y--) 
{
    x++; 
} 
System.out.println("x = " + x +" y = " + y);
x = 6 y = 0
x = 7 y = 0
x = 6 y = -1
Compilation fails.
Answer: Option
Explanation:

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 }


10.
What will be the output of the program?
int I = 0;
    outer:
    while (true) 
    {
        I++;
        inner:
        for (int j = 0; j < 10; j++) 
        {
            I += j;
            if (j == 3)
                continue inner;
            break outer;
        }
        continue outer;
    }
System.out.println(I);
1
2
3
4
Answer: Option
Explanation:

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.