Java throw Keyword MCQ

Exception Handling MCQ Part – 8 | Java throw Keyword MCQ | Also see:- Exception Handling Interview Questions in JavaMCQ in Java

Q1) Find the output of the below program?

public class Test {
   static ArithmeticException e = new ArithmeticException();
   public static void main(String[] args) {
      throw e;
   }
}

a) Compile-time error
b) ArithmeticException
c) ArithmeticException: / by zero
d) None of these

View Answer Answer:- b) ArithmeticException

The throw keyword can be used to throw the exception.

Q2) Find the output of the below program?

public class Test {
   static ArithmeticException e;
   public static void main(String[] args) {
      throw e;
   }
}

a) Compile-time error
b) ArithmeticException
c) NullPointerException
d) None of these

View Answer Answer:- c) NullPointerException

We are trying to throw “e” but this variable is not initialized yet. By default static variables hold null value, therefore we are getting NullPointerException.

Q3) Find the output of the below program?

public class Test {
   public static void main(String[] args) {
      throw new NumberFormatException();
   }
}

a) Compile-time error
b) ArithmeticException
c) NullPointerException
d) NumberFormatException

View Answer Answer:- d) NumberFormatException

Q4) Find the output of the below program?

public class Test {
   public static void main(String[] args) {
      throw new Test();
   }
}

a) Compile-time error
b) ArithmeticException
c) NullPointerException
d) NumberFormatException

View Answer Answer:- a) Compile-time error

Test class is not a direct/indirect child of Throwable class therefore it gives compile time error:- incompatible types: Test cannot be converted to Throwable => throw new Test();

Q5) Find the output of the below program?

public class Test extends Exception {
   public static void main(String[] args) {
      try {
        throw new Test();
      } catch(Exception e) {
        System.out.println("catch");
      }
   }
}

a) Compile-time error
b) Exception
c) catch
d) None of these

View Answer Answer:- c) catch

In this program, Test class is a child class of Exception, therefore indirectly it is a child class of Throwable. Hence, it can throw Test class objects.

Q6) Find the output of the below program?

class A extends ArithmeticException {}
public class Test {
   public static void main(String[] args) {
      try {
        throw new A();
      } catch(Exception e) {
        System.out.println("catch");
      }
   }
}

a) Compile-time error
b) Exception
c) catch
d) None of these

View Answer Answer:- c) catch

“A” class extends from the ArithmeticException class so it is an indirect subclass of Throwable. When “throw new A()” is called then this line will throw “A”, and the catch block is handling “Exception” which is super class therefore it is valid and the exception will be handled through the catch block.

Q7) Find the output of the below program?

public class Test {
   public static void main(String[] args) {
      System.out.println(10/0);
      System.out.println("KP");
   }
}

a) Compile-time error
b) ArithmeticException
c) KP
d) None of these

View Answer Answer:- b) ArithmeticException

Q8) Find the output of the below program?

public class Test {
   public static void main(String[] args) {
      throw new ArithmeticException("/ by zero");
      System.out.println("KP");
   }
}

a) Compile-time error
b) ArithmeticException
c) KP
d) None of these

View Answer Answer:- a) Compile-time error

Whenever we are using the throw keyword directly in the program then we can’t place any other statements after that. Hence we will get a compile time error:- unreachable statement.

In the previous program for the statement “System.out.println(10/0);” compiler don’t know whether this line will raise any exception or not, but in this program for the statement “throw new ArithmeticException(“/ by zero”);” compiler knows that 100% exception will be raised.

Q9) Find the output of the below program?

public class Test {
   public static void main(String[] args) {
      if(true) throw new ArithmeticException("/ by zero");
      System.out.println("KP");
   }
}

a) Compile-time error
b) ArithmeticException
c) KP
d) None of these

View Answer Answer:- b) ArithmeticException

We are using if block to place a “throw” statement, and if block will be executed only when the condition becomes true. Compiler checks only syntax but not the condition of the if block. Hence, the compiler is not sure whether the if block will be executed or not therefore we can place statements after the if block.

Q10) Find the output of the below program?

public class Test {
   public static void main(String[] args) {
      if(false) throw new ArithmeticException("/ by zero");
      System.out.println("KP");
   }
}

a) Compile-time error
b) ArithmeticException
c) KP
d) None of these

View Answer Answer:- c) KP

The condition of if block is false therefore if block will not be executed.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *