Java Questions & Answers – Throw, Throws & Nested Try

This section of our 1000+ Java MCQs focuses 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
View Answer

Answer: c
Explanation: None.

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

Answer: c
Explanation: 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
View Answer

Answer: a
Explanation: 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.
advertisement
advertisement

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
View Answer

Answer: c
Explanation: 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
View Answer

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

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
View Answer

Answer: d
Explanation: None.
Output:

advertisement
$ 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
View Answer

Answer: a
Explanation: 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
View Answer

Answer: d
Explanation: None

Sanfoundry Global Education & Learning Series – Java Programming Language.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.