MCQ – Java Exceptions

Q) Direct subclass of Throwable in Java
  1. Exception
  2. Error
  3. Both A & C
  4. None

Answer: 3


Q) un-checked(runtime) exception in java is/are
  1. ArrayIndexOutOfBoundsException
  2. ArithmeticException
  3. NullPointerException
  4. All

Answer: 4


Q) In below java program, which exception will occur?
public static void main(String[] args) {
    	
    	 FileReader file = new FileReader("test.txt");        
      
    }
  1. NullPointerException at compile time
  2. NullPointerException at run time
  3. FileNotFoundException at compiler time
  4. FileNotFoundException at runtime

Answer: 3

During compile time, itself File not found exception will occur. So, to avoid it, we need to wrap above statement with try catch block or with throws exception e.g.

public static void main(String[] args) throws FileNotFoundException {    	
    	 FileReader file = new FileReader("test.txt");     
      }

OR

public static void main(String[] args) {
    	
    	 try {
			FileReader file = new FileReader("test.txt");
		} catch (FileNotFoundException e) {
			
			e.printStackTrace();
		} 
      
    }

Q) Incorrect statement(s) about finally block in java exception
  1. Finally block always follow try catch block
  2. finally block always executes whether exception is handled or not.
  3. There can be multiple finally blocks followed by try catch block.
  4. All are correct

Answer: 3
In java exception handling, for each try block there can be zero or more catch blocks, but only one finally block.


Q) True statement(s) about try catch block
  1. It is mandatory to have catch block with every try block
  2. There must be only one catch block followed by try block
  3. There can be multiple catch block followed by try block.
  4. All

Answer: 3


Related Posts