Flow Control

Q1: 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 1 2 2

B Compilation fails at line 12.

C Compilation fails at line 11.

D 0 1 2

ANS:A - 0 1 2

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.



img not found
img

For help Students Orientation
Mcqs Questions

One stop destination for examination, preparation, recruitment, and more. Specially designed online test to solve all your preparation worries. Go wherever you want to and practice whenever you want, using the online test platform.