Menu
Topics Index
...
`

Switch statement


If you need explanation Read this topic

If you need Answer Take test on this topic

Switch statement
What will be the output of the following program?
public class Prize {
    public static void main(String args[]) {
        int x = 111, y = 101;
        switch (x & y) {
            case 1 :
                System.out.println("-1-");
            case 101 :
                System.out.println("-101-");
            case 111 :
                System.out.println("-111-");
            case 010 :
                System.out.println("-010-");
            default :
                System.out.println("-" + x & y + "-");
        }
    }
}

A. -101-
-101-
B. -111-
-010-
-111-
C. -1-
-101-
-111-
-010-
-1-
D. -101-
-111-
-010-
-101-
E. Some other output
F. Compilation Error or Runtime Error
Topic: switch Statement 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.

Ans is D.. x&y =101 and 101 case doesnt contain break so all below statemnts vl be executed..

Posted by Om Prakash Om    2013-10-31 09:10:46


Answer is D,
x&y value is 101 so it prints all after case 101 as there is no break statement.

Posted by Amar Kumar Biradar    2013-10-31 09:22:30


answer: e =:=bad operand  "&" is specified between two string operands(x,y) because it is a bit wise operator

Posted by Sapparapu Pradeep Kumar    2013-10-31 10:23:29


answer is F because "&" operator cannot be applied to strings so we get an compilation error in printf statement present in default case

Posted by Sai Teja    2013-10-31 11:41:35


Congratulations Sai Teja. You are this dose winner. We will send you the link using which you can claim your recharge.

Posted by Merit Campus    2013-11-01 03:35:10


© meritcampus 2019

All Rights Reserved.

Open In App