Skip to content

Java Exception Handling Quiz Online Test – Part 1

Java Exception handling Quiz part 1 contains 10 single choice questions. Java Exception handling quiz questions are designed in such a way that it will help you understand how exception handling works in Java. At the end of the quiz, result will be displayed along with your score and exception handling quiz answers.

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

  1. Will this code compile?

    public class Test extends Exception{	
    	
    	public static void main(String[] args){
    		
    		try{
    			
    			System.out.println("My Custom Exception test");
    			throw new Test();
    			
    		}catch(Exception e){
    			System.out.println("Test Exception");
    		}
    	}
    	
    }
    
  2. What will happen when you compile and run the following code?

    public class Test extends Exception{	
    	
    	private String message;
    	
    	public Test(String message){
    		this.message = message;
    	}
    	
    	public static void main(String[] args){
    		
    		int a = 5, b = 3;
    		try{
    			
    			if( a % b > 0 )
    				throw new Test();
    			
    		}catch(Exception e){
    			System.out.println("Test Exception");
    		}
    	}
    	
    }
    
  3. What will happen when you compile and run the following code?

    public class Test{	
    	
    	public static void main(String[] args){
    		
    		try{
    			
    			Test t = new Test();
    			int d = t.getNum(2, 5);
    			System.out.println(d);
    			
    		}catch(Exception e){
    			System.out.print("Exception 1 ");
    		}
    	}
    	
    	public int getNum(int a, int b){
    		
    		int c = 0;
    		try{
    			
    			c = a * b;
    			if(c > 10)
    				throw new String("Cannot be more than 10 ");
    			
    		}catch(Exception e){
    			System.out.print("Exception 2 ");
    		}
    		
    		return c;
    	}
    	
    }
    
  4. What will happen when you compile and run the following code?

    public class Test{	
    	
    	String className;
    	
    	public static void main(String[] args){
    		
    		
    		try{
    			
    			Test t = new Test();
    			if(t.className.equals("Test"))
    				System.out.print("Test ");
    			else
    				System.out.print("Other ");
    			
    		}catch(Exception e){
    			System.out.print("Exception ");
    		}catch(NullPointerException ne){
    			System.out.print("Null ");
    		}
    		
    	}
    	
    }
    
  5. RuntimeException and its sub-classes can be caught by the catch block.

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

    public class Test{	
    	
    	public static void main(String[] args){
    		
    		try{
    			
    			String[] names = {"Jack", "John", "Jill"};
    			printNames(names);
    			
    		}catch(NameIsJohnException e){
    			System.out.print("NameIsJohnException ");
    		}
    	}
    	
    	private static void printNames(String[] names) throws NameIsJohnException{
    		for(String name : names){
    			if(name.equals("John"))
    				throw new NameIsJohnException("Name cannot be John ");
    			
    			System.out.print(name + " ");
    		}
    	}
    }
    
    class NameIsJohnException{
    	String message;
    	
    	public NameIsJohnException(){
    		
    	}
    	public NameIsJohnException(String message){
    		this.message = message;
    	}
    	
    	public String getMessage(){
    		return this.message;
    	}
    
    }
    
  7. It is not required for the caller method to catch or re-throw the checked exception.

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

    import java.io.IOException;
    
    public class Test{	
    	
    	public static void main(String[] args){
    		
    		
    		try{
    			
    			Test t = new Test();
    			t.doNothing();
    			System.out.println("I have done nothing");
    			
    		}catch(IOException e){
    			System.out.println("Exception1");
    		}
    	}
    	
    	private void doNothing(){
    		for(int i = 0 ; i < 10; i++){
    		}
    	}
    }
    
  9. What will happen when you compile and run the following code?

    public class Test extends Exception{	
    	
    	public Test(){}
    	public Test(String str){
    		super(str);
    	}
    	
    	int importantData = 5;
    	public static void main(String[] args){
    		Test t = new Test();
    		t.importantMethod();
    	}
    	
    	private void importantMethod(){
    		if( importantData > 5)
    			throw new Test("Important data is invalid");
    		else
    			System.out.println(importantData);
    	}
    	
    }
    
  10. What will happen when you compile and run the following code?

    public class Test {
    	
    	public static void main(String[] args){		
    		
    		try{
    			
    			AnotherClass obj = new AnotherClass();
    			obj.method1();
    			System.out.println("Main Completed");
    		
    		}catch (Exception e){}
    
    	}
    }
    
    class AnotherClass{
    	
    	public void method2(){
    		throw new ArrayIndexOutOfBoundsException();
    	}
    	
    	public void method1(){
    		
    		try{
    			method2();
    		}catch (NullPointerException ae){
    			System.out.println("Exception caught");
    		}finally{
    			System.out.println("Method 1 ends");
    		}	
    		
    	}
    }