SwitchCase in Java Exercise I


Ques 1 SwitchCase


What is the output of the following Java Program?

class Main{
     public static void main(String[] args){
     int a=1,b=2;
     switch(a){
     case 1,2:
         b=1;
     case 2:
         b=2;
     default:
         b=3;
     }
     System.out.println("a="+b);
     }
}

  a) a=1
  b) a=2
  c) a=3
  d) Error


d is the correct option




Ques 2 SwitchCase


What is the output of the following Java Program?

class Main{
     public static void main(String[] args){
     int a=1,b=2;
     switch(a){
     case 1:
         b=1;
     case 2:
         b=2;
     default:
         b=3;
     }
     System.out.println("a="+b);
     }
}

  a) a=1
  b) a=2
  c) a=3
  d) Error


c is the correct option




Ques 3 SwitchCase


What is the output of the following Java Program?

class Main{
     public static void main(String[] args){
     float num=13;
     switch(num){
     case 12+1:
         System.out.print("case 12+1 ");
     case 0:
         System.out.print("case 0 ");
     case 13.0:
         System.out.print("case 13.0 ");
     default:
         System.out.print("Default ");
     }
     }
}

  a) case 0
  b) case 13.0
  c) Default
  d) Error


d is the correct option




Ques 4 SwitchCase


What is the output of the following Java Program?

class Main{
     public static void main(String[] args){
     float num=13;
     switch((int)num){
     case 12+1:
         System.out.print("case 12+1 ");
     case 0:
         System.out.print("case 0 ");
     default:
         System.out.print("Default ");
     }
     }
}

  a) case 12+1 case 0 Default
  b) case 12+1
  c) case 12+1 Default
  d) Error


a is the correct option




Ques 5 SwitchCase


What is the output of the following Java Program?

class Main{
     public static void main(String[] args){
     float num=13;
     for(int i=0;i<3;i++)
     {
         switch(i){
         case 0:
             break;
         case 1:
             System.out.print("case 1 ");
             break;
         case 2:
            System.out.print("case 2 ");
            break;
         default:
             System.out.print("Default ");
         break;
         }
    }
    }
}

  a) case 0
  b) case 1
  c) case 1 case 2
  d) Error


c is the correct option