Menu
Topics Index
...
`

Switch test


If you need explanation Read this topic

If you need Answer Take test on this topic

Switch test
What will be the output of the following program?
public class Jerusalem {
    public static void main(String args[]) {
        int i = 1, j = 0;
        switch (i) {
            case 2 :
                j += 6;
            case 4 :
                j += 1;
            default :
                j += 2;
            case 0 :
                j += 4;
        }
        System.out.println("j = " + j);
    }
}

A. j = 0
B. j = 2
C. j = 4
D. j = 6
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.. since i does matching i with any switch case.. it goes to default and next case0 vl be executed so in default j+=2; nxt case0 j+=4; so j value vl be 6..

Posted by Om Prakash Om    2013-06-17 03:38:30


answer is Option D. because after default case there is no break so j=6

Posted by Yeshu Yeshwanthi    2013-06-17 03:42:14


Congratulations Yeshu Yeshwanthi. You are this dose winner. Please send us your mobile details and we will process the recharge asap.

Posted by Merit Campus    2013-06-17 09:15:06


Thanku Merit Campus

Posted by Yeshu Yeshwanthi    2013-06-21 12:06:27


i got my recharge

Posted by Yeshu Yeshwanthi    2013-06-21 12:06:38


© meritcampus 2019

All Rights Reserved.

Open In App