Monday, November 25, 2013

Question: Switch / enum

Given:

public class Test {
    public enum Dogs {collie, harrier};
    

    public static void main(String [] args) {
    Dogs myDog = Dogs.collie;
    switch (myDog) {
       case collie:
          System.out.print("collie ");
       case harrier:
          System.out.print("harrier ");
     }
   }

}
What is the result?
A. collie
B. harrier
C. Compilation fails.
D. collie harrier
E. An exception is thrown at runtime.

Execution:
It will first print coolie but as BREAK is not there in case so it will execute next statement too and will print harrier.
so answer will be coolie harrier

Answer:
D. collie harrier

No comments:

Post a Comment