Topics Index

Final keyword936

If you need explanation Read this topic

If you need Answer Take test on this topic

Final keyword936
What will be the output of the following program?
public class Final
    final static short x = 2;
    final 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.

here option is B:
in class Final two varibles created x=2,y=0 as final  type means its values cannot be changed it behaves like constant
then excution starts from main
its enter into forloop for
1st iteration z=0:in switch(z) it goes to case y=>case 0: and it prints 0 and its dont have break thats why it will goes to next cases(case 1 and case 2) and prints 1,2  result for 1st itaration(0,1,2)
2nd iteration z=1:in switch(z) it goes to case x-1=>case 1: and it prints 1 and its dont have break thats why it will goes to next cases(case 3)2     result for 2nd itaration(1,2)
3rd iteration z=2:in switch(z) it goes to case x=>case 2: and it prints 3 and it dont have any break it goes next cases after that they no case thats why it will come out from switch result for 3rd itaration(2)
total result=>0,1,2,1,2,2 option B.
(here main point is in switch case label only  constant values used .here we uses the final variables they also contstant type thats why its dnot give any errors)

Posted by Goutham Singarapu    2015-02-10 13:34:09

Ans is B 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 but here we have the variables as constants so we can use it as case labels if remove the final keyword than compiler will raise an error

here z is 0 so first it will comes to the case y so here it will print 0 and there is no break so it will executes the other cases also and prints 1 n 2 next z is 1 so it will come case x-1 and prints 1 2 next z=2 than it wil comes to case x and prints 2 and z=3 so there so no case matches and loop terminates

Posted by Uday Kumar    2015-02-10 14:31:36


Posted by Shubham Bansal    2015-02-10 19:00:38

This dose is now closed and the winners are Goutham Singarapu,  for 'First Correct Comment', Uday Kumar,  for 'Best Comment' and Uday Kumar for the 'Popular Comment'. The 'lucky liker' is Uday Kumar. Please login into Merit Campus using facebook, to claim your recharge. Go to to raise the recharge.

Posted by Merit Campus    2015-02-12 04:27:05

© meritcampus 2019

All Rights Reserved.

Open In App