switch case interview questions and answers in java

Objective questions on switch case  in java and answers. Free online interview questions for beginners.  

(1)

public class SwitchCase {
    public static void main(String[] args) {
         int a=12;
         switch(a){
         case 014:
             System.out.print("I know java");
             break;
         case 12:
             System.out.print("I don't know java");
         default:
             System.out.print("I also know c++");
         }
    }
}

What will be the output of above java program?
         
(a) I don't know java
(b) I don't know java
    I also know c++
(c) I know java
(d)Compiler error





Answer: (d)

(2)

public class SwitchCase {
    public static void main(String[] args) {
         int a=10;
         switch(a++){
         case 10:
              switch(a--){
              default:System.out.print("Exit");
              case 10:
             }
         default:System.out.print(a);
         }
    }
}

What will be the output of above java program?
         
(a) 11
(b) 10
(c) Exit10
(d)Compiler error





Answer: (c)

(3)

Which of the following is not correct in java?
         
(a)Duplicate case is not possible.
(b)Nested switch case is possible.
(c)Switch case statement is more efficient than equivalent if else ladder.
(d)A switch statement without any case is not possible.





Answer: (d)

(4)

public class SwitchCase {
    public static void main(String[] args) {
         int a=9;
         switch(a){
         case 0:
             a++;
         case 9:
             a+=a;
         default:
             a%=5;
         }
         System.out.print(a);
    }
}

What will be the output of above java program?
         
(a)10
(b)18
(c)3
(d)Compiler error





Answer: (c)

(5)

public class SwitchCase {
    public static void main(String[] args) {
         Integer a=5;
         switch(a){
         default:
             a^=a;
         case 5:
             a++;
         case 10:
             a+=a;
         }
         System.out.print(a);
    }
}

What will be the output of above java program?

(a)6
(b)12
(c)0
(d)Compiler error





Answer: (b)

(6)

public class SwitchCase {
    public static void main(String[] args) {
         double a=1.33D;
         switch(a){
         default:
             a%=a;
         case 1.333:
             a++;break;
         case 1.3:
             a+=a;break;
         }
         System.out.print(a);
    }
}

What will be output of above program?
         
(a)2.333
(b)0
(c)1.334
(d)Compiler error





Answer: (d)

(7)

public class SwitchCase {
    public static void main(String[] args) {
         boolean b=97>='a';
         int a=65/'A'+1;
         switch(b){
   
         case true:
             a++;break;
         case false:
             a+=a;
         }
         System.out.print(a);
    }
}

What will be the output of above java program?
         
(a)3
(b)4
(c)5
(d)Compiler error






Answer: (d)

(8)

public class SwitchCase {
    public static void main(String[] args) {
         char c='2';
         int a=65/'A';
         switch(c){
   
         }
         System.out.print(a);
    }
}

What will be the output of above java program?
         
(a)1
(b)’1’
(c)Run time exception
(d)switch without any case, Compiler error






2 comments:

Anonymous said...

nice set of questions...

Unknown said...

I like it