Skip to content

Java Switch Quiz Online Test

Java Switch Quiz contains 12 single and multiple choice questions. Switch quiz questions are designed in such a way that it will help you understand how switch statement works in Java. At the end of the quiz, result will be displayed along with your score and switch quiz answers.

There is no time limit to complete the quiz. Click Start Quiz button to start the Java Switch quiz online.

  1. What will happen when you compile and run the following code?

    public class Test{	
    	
    	public static void main(String[] args){
    		
    		int i = 1;
    		switch(i){
    		
    			default: 
    				System.out.println("Default ");
    			case 1:
    				System.out.println("1 ");
    				break;
    			case 0:
    				System.out.println("0 ");
    				
    		}
    	}
    	
    }
    
  2. What will happen when you compile and run the following code?

    public class Test{	
    	
    	private static final int ON = 1;
    	private static final int OFF = 0;
    	
    	public static void main(String[] args){
    		
    		int state = 1;
    		switch(state){
    		
    			case ON:
    				System.out.println("On");
    				break;
    
    			default: 
    				System.out.println("Unknown");
    			
    			case OFF:
    				System.out.println("Off");
    				
    			}
    	}
    	
    }
    
  3. What will happen when you compile and run the following code?

    public class Test{	
    	
    	public static void main(String[] args){
    
    		final int ON = 1;
    		final int OFF = 0;
    		
    		int state = 3;
    		
    		switch(state){		
    			case ON:
    				System.out.print("On");
    				break;
    
    			default: 
    				System.out.print("Unknown");
    			
    			case OFF:
    				System.out.print("Off");
    		}
    	}	
    }
    
  4. What will happen when you compile and run the following code?

    public class Test{
    	
    	static char BLGR_A = 'a';
    	static char BLGR_B = 'b';
    	static char BLGR_O = 'o';
    	
    	public static void main(String[] args) {
    		
    		char bloodgroup = 'a';
    		
    		switch(bloodgroup){
    			case BLGR_A:
    				System.out.print("A");
    			case BLGR_B:
    				System.out.print("B");
    			case BLGR_O:
    				System.out.print("O");
    				break;
    		}
    	}
    }
    
  5. What will happen when you compile and run the following code?

    public class Test{
    	
    	public static void main(String[] args) {
    
    		final char ON = '1';
    		final char OFF = '0';
    		
    		char state = '0';
    		
    		switch(state){
    			
    			case ON:
    				System.out.println("On");
    				break;
    			case OFF:
    				System.out.println("Off");
    				break;
    			default:
    				assert false;
    		}
    
    	}
    }
    
  6. What will happen when you compile and run the following code?

    public class Test{	
    	
    	public static void main(String[] args){
    		
    		char c = 'd';
    		switch(c){
    		case 'a' :
    			System.out.print("a");
    		case 'b' :
    			System.out.print("b");
    		case 'c' :
    			System.out.print("c");
    		case 100 :
    			System.out.print("100");
    		default :
    			System.out.print("No match");
    		}
    	}
    	
    }
    
  7. What will happen when you compile and run the following code?

    public class Test{	
    	
    	public static void main(String[] args){
    
    		int age = 9;
    		
    		switch(age){
    			
    			case 1 : case 2: case 3: case 4: case 5:
    				System.out.println("12345");
    				break;
    			case 6: 
    				System.out.println("6");
    				break;
    			case 7 : case 8: case 9:
    				System.out.println("789"); 
    		}
    
    	}	
    }
    
  8. What will happen when you compile and run the following code?

    public class Test{	
    	
    	public static void main(String[] args){
    
    		int year = 2004;
    		
    		switch(year){
    			
    			case 2001: case 2002 : case 2003: case 2004: case 2005:
    				System.out.print("Normal Year");
    				break;
    			case 2004: 
    				System.out.print("Leap Year");
    				break;
    		}
    
    	}
    	
    }
    
  9. What will happen when you compile and run the following code?

    public class Test{	
    	
    	public static void main(String[] args){
    
    		int i = 97;
    		
    		switch(i){
    			case 95 :
    				System.out.print("95");
    			case 96 :
    				System.out.print("96");
    			case 'a' :
    				System.out.print("a");
    			case 'b' :
    				System.out.print("b");
    			case 'c' :
    				System.out.print("c");
    		}
    	}	
    }
    
  10. What will happen when you compile and run the following code?

    public class Test{	
    	
    	public static void main(String[] args){
    
    		int i = 97;
    		
    		switch(i){
    			case 'a' :
    				System.out.print("a");
    			case 'b' :
    				System.out.print("b");
    			case 'c' :
    				System.out.print("c");
    			case 96 :
    				System.out.print("97");
    			case 97 :
    				System.out.print("96");
    		}
    	}	
    }
    
  11. What will happen when you compile and run the following code?

    public class Test{	
    	
    	public static void main(String[] args){
    		int time = 8;
    		switch(time){
    			case 7:
    				System.out.println("Let's drink");
    			case 9:
    				System.out.println("Good night");
    			case 010:
    				System.out.println("Let's eat");
    		}
    	}	
    }
    
  12. What will happen when you compile and run the following code?

    public class Test{	
    	
    	public static void main(String[] args){
    		int age = 8;
    		switch(age){
    			case 4: case 5:{
    				String group = "Play House";
    			}
    			case 6: case 7:case 8:
    				String group = "Nursery";
    		}
    		System.out.println(group);
    	}
    	
    }