Java throws Keyword MCQ

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

Q1) Find the output of the below program?

import java.io.*;
public class Test {
   public static void main(String[] args) {
      PrintWriter pw = new PrintWriter("abc.txt");
      pw.println("Hello");
   }
}

a) “Hello” in the file
b) Compile-time error:- unreported exception FileNotFoundException
d) FileNotFoundException
e) None of these

View Answer Answer:- b) Compile-time error:- unreported exception FileNotFoundException

The FileNotFoundException is a checked exception. The PrintWriter() constructor throws FileNotFoundException therefore it must be handled either through catch block or throws keyword.

Q2) Which of the following statements is true about the “throws” keyword?

a) It can prevent raising an exception.
b) It is used only to convince the compiler, it can’t prevent raising an exception.
c) It can be used at the class level like- class A throws Exception { }
d) All of these

View Answer Answer:- b) It is used only to convince the compiler, it can’t prevent raising an exception.

The throws keyword required only to convince compiler & uses of throws keyword doesn’t prevent abnormal termination of the program.

Q3) Compile time error will occur in which method?

public class Test {
   public static void main(String[] args) { 
      m1();
   }
   public static void m1() { 
      m2();
   }
   public static void m2() { 
      Thread.sleep(1000);
   }
}

a) main()
b) m1()
c) m2()
d) Compiled Successfully

View Answer Answer:- c) m2()

In m2() method Thread.sleep() method which throws “InterruptedException”, which is a checked exception therefore m2() must catch or handle this exception.

Q4) Compile time error will occur in which method?

public class Test {
   public static void main(String[] args) { 
      m1();
      Integer.parseInt("two");
   }
   public static void m1() { 
      m2();
      System.out.println(9/0);
   }
   public static void m2() {
      Thread.sleep(1000);
   }
}

a) main()
b) m1()
c) m2()
d) Compiled Successfully

View Answer Answer:- c) m2()

In this program, there are 3 exceptions which may be raised:- NumberFormatException in main(), ArithmeticException in m1(), and InterruptedException in m2() method. NumberFormatException, and ArithmeticException are unchecked exceptions but InterruptedException is an unchecked exception. The handling for unchecked exceptions is optional but handling for unchecked exceptions is compulsory.

Q5) Compile time error will occur in which method?

public class Test {
   public static void main(String[] args) { 
      m1();
   }
   public static void m1() {
      m2();
   }
   public static void m2() 
    throws InterruptedException { 
      Thread.sleep(1000);
   }
}

a) main()
b) m1()
c) m2()
d) Compiled Successfully

View Answer Answer:- b) m1()

The m2() method propagates exceptions to the caller method i.e. m1() method. Hence the caller method must catch the exception or propagate to it’s caller method by using the “throws” keyword.

Q6) Compile time error will occur in which method?

public class Test {
   public static void main(String[] args) 
     throws InterruptedException { 
      m1();
   }
   public static void m1() 
     throws InterruptedException { 
      m2();
   }
   public static void m2() 
    throws InterruptedException { 
      Thread.sleep(1000);
   }
}

a) main()
b) m1()
c) m2()
d) Compiled Successfully

View Answer Answer:- d) Compiled Successfully

The m2() method throws InterruptedException and propagates to the caller method m1(). The m1() method also propagates InterruptedException to the caller method main(). Similarly, the main() also propagates InterruptedException to the JVM. Hence, compile will not give any compile time error.

Q7) The use of the “throws” keyword has no impact on?

a) Unchecked exception
b) Checked exception
c) Both checked & unchecked exception
d) None of these

View Answer Answer:- a) Unchecked exception

The “throws” keyword is required only for checked exceptions and usage of throws keyword for unchecked exceptions has no impact.

Q8) The “throws” keyword can’t be used with?

a) Methods
b) Constructors
c) Classes
d) All of these

View Answer Answer:- c) Classes

We can use throws keywords for method & constructors but not for classes. Example:- public class Test throws Exception { } // invalid

Q9) Find the output of the below program?

public class Test {
   public static void main(String[] args) throws Test {}
}

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

View Answer Answer:- a) Compile-time error

We can use throws keywords only for throwable types. If we are trying to use it for normal Java classes then we will get compile time errors:- incompatible types.

Q10) Find the output of the below program?

public class Test extends RuntimeException {
   public static void main(String[] args) throws Test {}
}

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

View Answer Answer:- c) No output

We can use throws keywords for Throwable types. In this program, Test class extends from RuntimeException hence it is an indirect subclass of Throwable.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do 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 *