Java Exception Handling Quiz Online Test – Part 2

Java Exception handling Quiz part 2 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. Which of these statements are true?

  2. 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();
    			t.method1();
    			
    		}catch (Exception e){
    			System.out.print("Main ");
    		}		
    
    	}
    	
    	public void method1(){
    		try{
    			method2();
    		}catch(Exception e){
    			System.out.print("Method1 ");
    		}finally{
    			System.out.print("Finally ");
    		}
    	}
    	
    	public void method2(){
    		throw new NullPointerException();
    	}
    }
    
  3. What will happen when you compile and run the following code with “java Test one two three” command?

    public class Test {
    	
    	public static void main(String[] args){		
    		
    		try{
    			
    			for(int i = 0 ; i < = args.length; i++){
    				System.out.print(args[i] + " ");
    			}
    			
    		}catch (Exception e){
    			System.out.print("Exception ");
    		}finally{
    			System.out.print("Finally ");
    		}
    
    	}
    }
    
  4. 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();
    			t.method1();
    			
    		}catch (Exception e){
    			System.out.print("Exception ");
    		}finally{
    			System.out.print("Finally ");
    		}
    
    	}
    	
    	public void method1(){
    		throw new RuntimeException();
    	}
    }
    
  5. 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();
    			t.method1();
    			
    		}catch (Exception e){
    			System.out.print("Exception ");
    		}finally{
    			System.out.print("Finally ");
    		}
    
    	}
    	
    	public void method1(){
    		throw new AssertionError();
    	}
    }
    
  6. Will this code compile?

    public class Test {
    	
    	public static void main(String[] args){		
    		
    		try{
    			
    			Test t = new Test();
    			t.method1();
    			
    		}catch (RuntimeException re){
    			System.out.print("Exception ");
    		}finally{
    			System.out.print("Finally ");
    		}
    
    	}
    	
    	public void method1(){
    		throw new RuntimeException();
    	}
    }
    
  7. What will happen when you compile and run the following code?

    public class Test {
    	
    	private int i;
    	
    	public static void main(String[] args){		
    		Test t = new Test();
    		System.out.println(t.method1());
    	}
    	
    	public int method1(){
    
    		try{
    			
    			if(i == 0)
    				return 0;
    			else
    				return 10/i;
    			
    		}catch (Exception re){
    			System.out.print("Exception ");
    		}finally{
    			System.out.print("Finally ");
    		}
    	
    		return 0;
    	}
    }
    
  8. What will happen when you compile and run the following code?

    public class Test {
    	
    	public int i;
    	
    	public static void main(String[] args){	
    		
    		Test t = new Test();
    		try{	
    			
    			t.method1();
    			
    		}catch(Exception e){
    			t.here();
    		}finally{
    			t.here();
    		}
    		
    		System.out.println(t.i);
    	}
    	
    	public void method1(){
    		
    		try{
    			method2();
    			here();
    		}catch(Exception e){
    			here();
    		}finally{
    			here();
    		}
    
    	}
    	
    	public void method2(){
    		
    		try{
    			throw new Exception();
    		}catch(Exception e){
    			here();
    		}finally{
    			here();
    		}
    
    	}
    	
    	public void here(){
    		i++;
    	}
    }
    
  9. Which class will compile without any errors out of the below given two classes?

    import java.io.IOException;
    
    public class Test1 {
    	
    	public static void main(String[] args){
    		
    		try{
    			
    		}catch(IOException ioe){
    			
    		}
    	}
    }
    
    
    public class Test2 {
    	
    	public static void main(String[] args){
    		
    		try{
    			
    		}catch(Exception e){
    			
    		}
    	}
    }
    
  10. The class Test defines a method myMethod (float f) as given below. Which of the following declarations of myMethod (float f) are valid in the class TestChild?

    import java.io.IOException;
    
    public class Test{
    	
    	public void myMethod(float f) throws IOException{
    		throw new IOException();
    	}   
    
    }
    
    class TestChild extends Test{
    	
    	//method declaration here	
    
    }