Java OCA OCP Practice Question 820

Question

What will the following code print?

enum Season {/*  www.j  av a2 s  . c o m*/
   SUMMER, WINTER, SPRING, FALL
}

public class Main {
   public static void main(String[] args) throws Exception {
 
      Season s = Season.SPRING; 
      switch (s){ 
                  case SUMMER  : System .out.println ("SUMMER"); 
                  case default  : System .out.println ("SEASON"); 
                  case WINTER  :  System .out.println ("WINTER"); 
      } 

   }
}

Select 1 option

  • A. SEASON
  • B. SEASON WINTER
  • C. It will not compile.
  • D. It will not print anything.


Correct Option is  : C

Note

If the code is changed to default: System .out.println ("SEASON"); it will print SEASON WINTER.

Since there is no break after the case statements, the control will go through the rest of the case statements without even checking the value of the cases.




PreviousNext

Related