Java OCA OCP Practice Question 1885

Question

What will the following program print?.

public class Main{
   public static void main (String args []){
      int k = 9, s = 5;
      switch (k){
         default  :
         if ( k == 10)  { s = s*2;  }
         else{/*from   w w  w . j a  v a 2s .  c o m*/
            s = s+4;
            break;
          }
         case 7  : s = s+3;
       }
      System.out.println (s);
    }
}

Select 1 option

  • A. 5
  • B. 9
  • C. 12
  • D. It will not compile.


Correct Option is  : B

Note

Since 9 does not match any of the case labels, it is accepted by default block.

In this block, the else part is executed, which sets s to the value of s+4.

Since there is a break in the else block, case 7: is not executed.




PreviousNext

Related