Menu
Topics Index
...
`

Fall Through2601


If you need explanation Read this topic

If you need Answer Take test on this topic

Fall Through2601
What will be the output of the following program?
public class FallThrough {
    public static void main(String[] args) {
        String str = "Hello";
        switch (str) {
            case "Hai" : {
                System.out.print("Hai" + " ");
            }
            case "Hello" : {
                System.out.print("Hello," + " ");
            }
            case "How" : {
                System.out.print("how" + " ");
            }
            case "Are" : {
                System.out.print("are" + " ");
            }
            case "You" : {
                System.out.print("you");
            }
                System.out.print("?");
        }
    }
}

A. Hello,
B. Hello, how are you
C. Hello, how are you?
D. Hai Hello, how are you?
E. Compilation Error or Runtime Error
Topic: Fall Through Switch Case Statements 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.

A

Posted by Nick Jokic    2014-12-08 12:48:55


Answer is c ..............From the JDK 7 release,  We can use a String object in the expression of a switch statement: ...in the above program, control is passed to case "Hello" since str value is hello and since there is no break for that case , the rest of the cases also gets executed and after the case "You"  Sop which prints "?" is also executed since the control does not come out od switch statement yet as there is no break

Posted by Shashanka Mogaliraju    2014-12-08 12:48:58


© meritcampus 2019

All Rights Reserved.

Open In App