Topics Index

Final keyword935

If you need explanation Read this topic

If you need Answer Take test on this topic

Final keyword935
What will be the output of the following program?
public class Final
    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 M

                case x - 1: System.out.print("1 "); // LINE N

                case x: System.out.print("2 ");    // LINE O

A. 0 1 2
B. 0 1 2 1 2 2
C. Compilation fails at LINE M
D. Compilation fails at LINE N
E. Compilation Error at some other place
Topic: final Keyword In Java

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.


Posted by Shubham Bansal    2015-02-06 14:48:34

Ans is C.
It gives compilation error as we cannot use non-final variable as case label... so when we are using 'y' variable in switch case label it gives compilation error...

Posted by Gopinath Manchikanti    2015-02-06 18:17:33


--Here,Concept of "SWITCH CASE" is used.

--Unlike if-then and if-then-else statements, the switch statement can have a number of possible execution paths. A switch works with the byte, short, char, and int primitive data types. It also works with enumerated types (discussed in Enum Types), the String class, and a few special classes that wrap certain primitive types: Character, Byte, Short, and Integer (discussed in Numbers and Strings).

--Here,final variable "y" is used as label which is not first compilation error comeas at line M and then at N and after that at line o.

Posted by Mânïshå Mùlchåndânï    2015-02-06 19:17:27

Ans is we have the switch concept which is used to select one statement out of multiple switch we select the statements using the case labels..and we use the break keyword to stop the flow from the one case to another if-else we can have the nested switches also..and we can not have the duplicate case labels directly or indirectly..
here we have the case labels with the variables which are not allowed so here compiler will raise error th case y which is at Line M..

Posted by Uday Kumar    2015-02-09 03:56:13

This dose is now closed and the winners are Gopinath Manchikanti,  for 'First Correct Comment', Mânïshå Mùlchåndânï, Uday Kumar,  for 'Best Comment' and Mânïshå Mùlchåndânï for the 'Popular Comment'. The 'lucky liker' is Mânïshå Mùlchåndânï. Please login into Merit Campus using facebook, to claim your recharge. Go to to raise the recharge.

Posted by Merit Campus    2015-02-09 05:06:30

© meritcampus 2019

All Rights Reserved.

Open In App