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 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 handles the exception when no catch is used?
a) Default handler
b) finally
c) throw handler
d) Java run time system
View Answer

Answer: a
Explanation: None.

5. 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.

6. What is the output of this program?

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

a) TypeA
b) TypeB
c) 0TypeA
d) 0TypeB
Note : Execution command line : $ java exception_handling
View Answer

Answer: b
Explanation: None.
Output:
$ javac exception_handling.java
$ java exception_handling
TypeB

7. What is the output of this program?

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

a) A
b) B
c) Hello
d) Runtime Error
View Answer

Answer: b
Explanation: None.
Output:
$ javac exception_handling.java
$ java exception_handling
B

8. What is the output of this program?

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

a) A
b) 0
c) 0A
d) Exception
View Answer

Answer: c
Explanation: None.
Output:
$ javac exception_handling.java
$ java exception_handling
0A

9. What is the output of this program?

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

a) TypeA
b) TypeB
c) 0TypeA
d) 0TypeB
Note : Execution commandline : $ java exception_handling one
View Answer

Answer: c
Explanation: Execution command line is “$ java exception_handling one” hence there is only single string that is in args array, making its length 1, hence “a = a/ a – a” in second try block is executing which throws arithmeticexception which is caught by catch of firts try block as the nested try block does not have a catch block which can detect ArithmeticException. Hence 0TypeA is printed
Output:
$ javac exception_handling.java
$ java exception_handling
0TypeA

10. What is the output of this program?

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

a) TypeA
b) TypeB
c) 0TypeA
d) 0TypeB
Note: Execution command line: $ java exception_handling one two
View Answer

Answer: c
Explanation: Execution command line is “$ java exception_ handling one two” hence there are tro input making args.length = 2, hence “c[8] = 9″ in second try block is executing which throws ArrayIndexOutOfBoundException which is caught by catch of nested try block. Hence 0TypeB is printed
Output:
$ javac exception_handling.java
$ java exception_handling
0TypeB

Sanfoundry Global Education & Learning Series – Java Programming Language.

About

Manish Bhojasia, a technology veteran with 17+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer and SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage & Cluster Administration, Advanced C Programming, SAN Storage Technologies, SCSI Internals and Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him below.


Sanfoundry Training Center of Excellence (CoE)

Manish Bhojasia, a technology veteran with 17+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer and SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage & Cluster Administration, Advanced C Programming, SAN Storage Technologies, SCSI Internals and Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him below.







Sanfoundry Internships
Subscribe Newsletter & Posts
Follow Manish & Sanfoundry