Java Exception Handling Quiz Online Test – Part 3

Java Exception handling Quiz part 3 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. What all method1 method declarations are valid for TestChild class at the line number 11?

    class TestException extends Exception{}
    class TestChild1Exception extends TestException{}
    class TestChild2Exception extends TestChild1Exception{}
    
    public class Test{
    	public void method1() throws TestChild1Exception{
    	}
    }
    
    class TestChild extends Test{
    	//method declaration here
    }
    
  2. Will this class compile?

    public class Test{
    	
    	public static void main(String[] args){
    		Test t = new Test();
    		t.method1();
    	}
    	
    	public void method1(){
    		method2();
    	}
    	
    	public void method2(){
    		try{
    			throw new NullPointerException();
    		}catch(NullPointerException e){
    			throw e;
    		}
    	}
    }
    
  3. What will happen when you compile and run the following code?

    public class Test{
    	
    	public static void main(String[] args){
    		Test t = new Test();
    		t.method1();
    		System.out.print("5");
    	}
    	
    	public void method1(){
    		try{
    			
    			method2();
    			System.out.print("1");
    			
    		}catch(NullPointerException ne){
    			System.out.println("2");
    		}finally{
    			System.out.println("3");
    		}
    		System.out.print("4");		
    	}
    	
    	public void method2(){
    		throw new ArrayIndexOutOfBoundsException();
    	}
    }
    
  4. Error (and its subclasses) cannot be caught, only Exception (and its subclasses) can be caught using the catch block.

  5. Will this code compile?

    public class Test{
    	
    	public static void main(String[] args){
    		try{
    			Test t = new Test();
    			t.method1();
    		}catch(NullPointerException ne){
    			
    		}
    	}
    	
    	public void method1() throws Exception{			
    	}
    	
    }
    
  6. What will happen when you compile and run the following code?

    public class Test{
    	
    	public static void main(String[] args){
    	
    		try{
    			
    			System.out.print("1");
    
    			try{
    				System.out.print("a");
    				throw new Exception();
    			}catch(Exception e){
    				System.out.print("b");
    				throw e;
    			}finally{
    				System.out.print("c");
    			}
    
    		}catch(Exception e){
    			System.out.print("3");
    		}finally{
    			System.out.print("4");
    		}
    	}	
    }
    
  7. What will you write at the line number 16, provided that method name is method1?

    class TestException extends Exception{}
    
    public class Test{
    	
    	private int i = 0;
    	
    	public static void main(String[] args){
    		try{
    			Test t = new Test();
    			t.method1();
    		}catch(Exception e){
    			
    		}
    	}
    	
    	//method declaration here
    	{
    		if( i == 0 )
    			throw new TestException();
    	}
    	
    }
    
  8. Throwable class is the parent class of both Error and Exception classes.

  9. All subclasses of the RuntimeException class and Error class are unchecked exceptions.

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

    public class Test{
    	
    	private int i = 0;
    	
    	public static void main(String[] args){
    	
    		try{
    			System.out.print("1");
    			System.exit(1);
    			System.out.print("2");
    		}catch(Exception e){
    			System.out.print("3");
    		}finally{
    			System.out.print("4");
    		}
    		System.out.print("5");
    	}
    	
    }