Menu
Topics Index
...
`

print statements by using Switch1651


If you need explanation Read this topic

If you need Answer Take test on this topic

print statements by using Switch1651
What will be the output of the following program? Assume that we are using JDK1.7?
public class Select {
    public static void choose(String str) {
        switch (str) {
            case "M" :
                System.out.print("Merit ");
            case "C" :
                System.out.print("Campus");
                break;
            case "W" :
                System.out.print("Welcome ");
                break;
            case "T" :
                System.out.print("To ");
                break;
            case "H" :
                System.out.println();
            default :
                System.out.println("Default");
                break;
        }
    }
    public static void main(String arg[]) {
        Select.choose("W");
        Select.choose("M");
        Select.choose("w");
    }
}

A. Merit Campus
B. Welcome Merit CampusDefault
C. Welcome Merit Campus
Default
D. Welcome Merit Campus Default
E. Compilation Error or Runtime Error
Topic: Static and Non Static Variables - Static and Non Static Methods

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.

option c  because case M doesnt contain break and default is printed for "w" with a println(next line) statement

Posted by Vivek Roshan    2014-04-25 13:01:50


Option B :as after matching with the first "W' we get Welcome the for Case "M" there is no break thus the next case is also executed and we get "Campus" and as there is no case "w' the default case is executed in the same line

Posted by Deepak Kumar Yadav    2014-04-25 13:11:59


Congratulations Deepak Kumar Yadav. You are this dose winner. We will send you the link using which you can claim your recharge.

Posted by Merit Campus    2014-04-28 04:13:14


© meritcampus 2019

All Rights Reserved.

Open In App