250+ TOP MCQs on Throw, Throws & Nested Try and Answers

Java MCQs on throw, throws & nested try of Java Programming Language.

1. Which of these keywords is used to generate an exception explicitly?
a) try
b) finally
c) throw
d) catch

Answer: c
Clarification: None.

2. Which of these class is related to all the exceptions that are explicitly thrown?
a) Error
b) Exception
c) Throwable
d) Throw

Answer: c
Clarification: None.

3. Which of these operator is used to generate an instance of an exception than can be thrown by using throw?
a) new
b) malloc
c) alloc
d) thrown

Answer: a
Clarification: new is used to create an instance of an exception. All of java’s built in run-time exceptions have two constructors: one with no parameters and one that takes a string parameter.

4. Which of these keywords is used to by the calling function to guard against the exception that is thrown by called function?
a) try
b) throw
c) throws
d) catch

Answer: c
Clarification: If a method is capable of causing an exception that it does not handle. It must specify this behaviour the behaviour so that callers of the method can guard themselves against that exception. This is done by using throws clause in methods declaration.

5. What will be the output of the following Java code?

  1.     class exception_handling 
  2.     {
  3.         public static void main(String args[]) 
  4.         {
  5.             try 
  6.             {
  7.                 int a = args.length;
  8.                 int b = 10 / a;
  9.                 System.out.print(a);
  10.                 try 
  11.                 {
  12.                     if (a == 1)
  13.                         a = a / a - a;
  14.                     if (a == 2) 
  15.                     {
  16.                         int []c = {1};
  17.                         c[8] = 9;
  18.                     }
  19.                 }
  20.                 catch (ArrayIndexOutOfBoundException e) 
  21.                 {
  22.                     System.out.println("TypeA");
  23.                 }
  24.                 catch (ArithmeticException e) 
  25.                 {
  26.                     System.out.println("TypeB");
  27.                 }
  28.             }
  29.         }
  30.     }

a) TypeA
b) TypeB
c) Compile Time Error
d) 0TypeB

Answer: c
Clarification: Because we can’t go beyond array limit

6. What will be the output of the following Java code?

  1.     class exception_handling 
  2.     {
  3.         public static void main(String args[]) 
  4.         {
  5.             try 
  6.             {
  7.                 System.out.print("A");
  8.                 throw new NullPointerException ("Hello");
  9.             }
  10.             catch(ArithmeticException e) 
  11.             {
  12.                 System.out.print("B");        	
  13.             }
  14.         }
  15.     }

a) A
b) B
c) Hello
d) Runtime Exception

Answer: d
Clarification: None.
Output:

$ javac exception_handling.java
$ java exception_handling
Exception in thread "main" java.lang.NullPointerException: Hello
	at exception_handling.main

7. What will be the output of the following Java code?

  1. public class San 
  2.  {  
  3.     public static void main(String[] args) 
  4.     {
  5.         try 
  6.         { 
  7.             return; 
  8.         } 
  9.         finally 
  10.         {
  11.             System.out.println( "Finally" ); 
  12.         } 
  13.     } 
  14. }

a) Finally
b) Compilation fails
c) The code runs with no output
d) An exception is thrown at runtime

Answer: a
Clarification: Because finally will execute always.

8. What will be the output of the following Java code?

  1. public class San 
  2. {
  3.     public static void main(String args[])
  4.     {
  5.         try 
  6.         {
  7.             System.out.print("Hello world ");
  8.         }
  9.         finally 
  10.         {
  11.             System.out.println("Finally executing ");
  12.         }
  13.     }
  14. }

a) The program will not compile because no exceptions are specified
b) The program will not compile because no catch clauses are specified
c) Hello world
d) Hello world Finally executing

Answer: d
Clarification: None

Leave a Reply

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