Finding The Output

Question 1 :

What will be the output of the program?

int i = l, j = -1; 
switch (i) 
{
    case 0, 1: j = 1; /* Line 4 */
    case 2: j = 2; 
    default: j = 0; 
} 
System.out.println("j = " + j);


A). j = -1
B). j = 0
C). j = 1
D). Compilation fails.
Answer : Option D

Explanation :

The case statement takes only a single argument. The case statement on line 4 is given two arguments so the compiler complains.


Question 2 :

What will be the output of the program?

int i = 1, j = 10; 
do 
{
    if(i > j) 
    {
        break; 
    } 
    j--; 
} while (++i < 5); 
System.out.println("i = " + i + " and j = " + j);


A). i = 6 and j = 5
B). i = 5 and j = 5
C). i = 6 and j = 4
D). i = 5 and j = 6
Answer : Option D

Explanation :

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 i becomes 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 i can 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.


Question 3 :

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 ");
            }
        }
    }
}


A). 0 1 2
B). 0 1 2 1 2 2
C). 2 1 0 1 0 0
D). 2 1 2 0 1 2
Answer : Option D

Explanation :

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.


Question 4 :

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;  
    } 
}


A). value = 2
B). value = 4
C). value = 6
D). value = 8
Answer : Option D

Explanation :

Because there are no break statements, once the desired result is found, the program continues though each of the remaining options.


Question 5 :

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);
    }
}


A). 0
B). 1
C). 101
D). 111
Answer : Option C

Explanation :

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.


Question 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 */
            }
        }
    }
}


A). 0 1 2
B). 0 1 2 1 2 2
C). Compilation fails at line 11.
D). Compilation fails at line 12.
Answer : Option C

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.


Question 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);
    }
}


A). 41
B). 42
C). 50
D). 51
Answer : Option D

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.


Question 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 ) ;
    }
}


A). I is 1
B). I is 1 I is 1
C). No output is produced.
D). Compilation error
Answer : Option C

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.


Question 9 :

What will be the output of the program?

int x = l, y = 6; 
while (y--) 
{
    x++; 
} 
System.out.println("x = " + x +" y = " + y);


A). x = 6 y = 0
B). x = 7 y = 0
C). x = 6 y = -1
D). Compilation fails.
Answer : Option D

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 }


Question 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);


A). 1
B). 2
C). 3
D). 4
Answer : Option A

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.


Question 11 :

What will be the output of the program?

for (int i = 0; i < 4; i += 2) 
{ 
    System.out.print(i + " "); 
} 
System.out.println(i); /* Line 5 */


A). 0 2 4
B). 0 2 4 5
C). 0 1 2 3 4
D). Compilation fails.
Answer : Option D

Explanation :

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.


Question 12 :

What will be the output of the program?

int x = 3; 
int y = 1; 
if (x = y) /* Line 3 */
{
    System.out.println("x =" + x); 
}


A). x = 1
B). x = 3
C). Compilation fails.
D). The code runs with no output.
Answer : Option C

Explanation :

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.


Question 13 :

What will be the output of the program?

Float f = new Float("12"); 
switch (f) 
{
    case 12: System.out.println("Twelve"); 
    case 0: System.out.println("Zero"); 
    default: System.out.println("Default"); 
}


A). Zero
B). Twelve
C). Default
D). Compilation fails
Answer : Option D

Explanation :

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.


Question 14 :

What will be the output of the program?

int i = 0; 
while(1) 
{
    if(i == 4) 
    {
        break;
    } 
    ++i; 
} 
System.out.println("i = " + i);


A). i = 0
B). i = 3
C). i = 4
D). Compilation fails.
Answer : Option D

Explanation :

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.


Question 15 :

What will be the output of the program?

public class Delta 
{ 
    static boolean foo(char c) 
    {
        System.out.print(c); 
        return true; 
    } 
    public static void main( String[] argv ) 
    {
        int i = 0; 
        for (foo('A'); foo('B') && (i < 2); foo('C')) 
        {
            i++; 
            foo('D'); 
        } 
    } 
}


A). ABDCBDCB
B). ABCDABCD
C). Compilation fails.
D). An exception is thrown at runtime.
Answer : Option A

Explanation :

'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


Question 16 :

What will be the output of the program?

for(int i = 0; i < 3; i++) 
{ 
    switch(i) 
    { 
        case 0: break; 
        case 1: System.out.print("one "); 
        case 2: System.out.print("two "); 
        case 3: System.out.print("three "); 
    } 
} 
System.out.println("done");


A). done
B). one two done
C). one two three done
D). one two three two three done
Answer : Option D

Explanation :

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.


Question 17 :

What will be the output of the program?

public class Test 
{  
    public static void main(String args[]) 
    { 
        int i = 1, j = 0; 
        switch(i) 
        { 
            case 2: j += 6; 
            case 4: j += 1; 
            default: j += 2; 
            case 0: j += 4; 
        } 
        System.out.println("j = " + j); 
    } 
}


A). j = 0
B). j = 2
C). j = 4
D). j = 6
Answer : Option D

Explanation :

Because there are no break statements, the program gets to the default case and adds 2 to j, then goes to case 0 and adds 4 to the new j. The result is j = 6.


Question 18 :

What will be the output of the program?

boolean bool = true; 
if(bool = false) /* Line 2 */
{
    System.out.println("a"); 
} 
else if(bool) /* Line 6 */
{
    System.out.println("b"); 
} 
else if(!bool) /* Line 10 */
{
    System.out.println("c"); /* Line 12 */
} 
else 
{
    System.out.println("d"); 
}


A). a
B). b
C). c
D). d
Answer : Option C

Explanation :

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.


Question 19 :

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 ");
            }
        }
    }
}


A). 0 def 1
B). 2 1 0 def 1
C). 2 1 0 def def
D). 2 1 0 def 1 def 1
Answer : Option D

Explanation :

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).


Question 20 :

What will be the output of the program?

int i = 0, j = 5; 
tp: for (;;) 
    {
        i++;  
        for (;;) 
        {
            if(i > --j) 
            {
                break tp; 
            } 
        } 
        System.out.println("i =" + i + ", j = " + j);


A). i = 1, j = 0
B). i = 1, j = 4
C). i = 3, j = 4
D). Compilation fails.
Answer : Option D

Explanation :

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.


Question 21 :

What will be the output of the program?

int I = 0;
label:
    if (I < 2) {
    System.out.print("I is " + I);
    I++;
    continue label;
}


A). I is 0
B). I is 0 I is 1
C). Compilation fails.
D). None of the above
Answer : Option C

Explanation :

The code will not compile because a continue statement can only occur in a looping construct. If this syntax were legal, the combination of the continue and the if statements would create a kludgey kind of loop, but the compiler will force you to write cleaner code than this.