Machine-translated by Google. Questions are authored in English.
Top

Flow Control - Finding the output Questions and Answers

1.

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

Answer: D

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.

Please log in or register to report this question.

2.

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

Answer: C

Correct answer: Compilation fails at line 11.

A case label must be a compile-time constant. x qualifies, being final static short with an initialiser, so case x and case x-1 are both legal. y does not: it is declared public static int without final, so its value could change and the compiler rejects it at line 11. Had it compiled, the fall-through would have printed 0 1 2 1 2 2 - which is the option offered beside the key.

Please log in or register to report this 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 ");
            }
        }
    }
}

Answer: D

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.

Please log in or register to report this question.

4.

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

Answer: C

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.

Please log in or register to report this question.

5.

What will be the output of the program?

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

Answer: D

Correct answer: Compilation fails.

A classic switch statement takes one constant per case label, so case 0, 1: is a syntax error - the way to match two values is to write two labels one after the other, case 0: case 1:, relying on fall-through. The comma form was introduced only with the switch expressions of Java 14 and belongs to the arrow syntax, not to this one. Nothing runs, so j is never assigned.

Please log in or register to report this question.

6.

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 1: j++; 
            case 2: j++;  
            case 3: j++; 
            case 4: j++; 
            case 5: j++; 
            default: j++; 
            } 
        return j + x;  
    } 
}

Answer: D

Correct answer: value = 8

Switch labels fall through unless a break intervenes, and here none does. With x = 4 control enters at case 4 and then runs through case 5 and default, so j is incremented three times from 1 to 4. The method returns \( j + x = 4 + 4 = 8 \). Counting only the matching label gives 2 and 6, both of which are offered.

Please log in or register to report this question.

Loading…
Tags: Flow Control - Finding the output Questions and Answers || Flow Control - Finding the output MCQ Questions and Answers || Flow Control - Finding the output GK Questions and Answers || Flow Control - Finding the output GK MCQ Questions || Flow Control - Finding the output Multiple Choice Questions and Answers || Flow Control - Finding the output GK || GK on Flow Control - Finding the output || Java Programming Questions and Answers || Java Programming MCQ Questions and Answers || Java Programming GK Questions and Answers || GK on Java Programming