Java Questions & Answers – Exceptions Types

This section of our 1000+ Java MCQs focuses on Exceptions types in Java Programming Language.

1. Which of these is a super class of all exceptional type classes?
a) String
b) RuntimeExceptions
c) Throwable
d) Cachable
View Answer

Answer: c
Explanation: All the exception types are subclasses of the built in class Throwable.

2. Which of these class is related to all the exceptions that can be caught by using catch?
a) Error
b) Exception
c) RuntimeExecption
d) All of the mentioned
View Answer

Answer: b
Explanation: Error class is related to java run time error that can’t be caught usually, RuntimeExecption is subclass of Exception class which contains all the exceptions that can be caught.

3. Which of these class is related to all the exceptions that cannot be caught?
a) Error
b) Exception
c) RuntimeExecption
d) All of the mentioned
View Answer

Answer: a
Explanation: Error class is related to java run time error that can’t be caught usually, RuntimeExecption is subclass of Exception class which contains all the exceptions that can be caught.

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 manually throw an exception?
a) try
b) finally
c) throw
d) catch
View Answer

Answer: c
Explanation: None.

6. What is the output of this program?

  1.     class exception_handling {
  2.         public static void main(String args[]) {
  3.             try {
  4.                 System.out.print("Hello" + " " + 1 / 0);
  5.             }
  6.             finally {
  7.         	System.out.print("World");        	
  8.             }
  9.         }
  10.     }

a) Hello
b) World
c) Compilation Error
d) First Exception then World
View Answer

Answer: d
Explanation: None.
Output:
$ javac exception_handling.java
$ java exception_handling
Exception in thread “main” java.lang.ArithmeticException: / by zero
World

7. What is the output of this program?

  1.     class exception_handling {
  2.         public static void main(String args[]) {
  3.             try {
  4.                 int a, b;
  5.                 b = 0;
  6.                 a = 5 / b;
  7.                 System.out.print("A");
  8.             }
  9.             catch(ArithmeticException e) {
  10.         	System.out.print("B");        	
  11.             }
  12.         }
  13.     }

a) A
b) B
c) Compilation Error
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.         public static void main(String args[]) {
  3.             try {
  4.                 int a[] = {1, 2,3 , 4, 5};
  5.                 for (int i = 0; i < 7; ++i) 
  6.                     System.out.print(a[i]);
  7.             }
  8.             catch(ArrayIndexOutOfBoundsException e) {
  9.         	System.out.print("0");        	
  10.             }
  11.         }
  12.     }

a) 12345
b) 123450
c) 1234500
d) Compilation Error
View Answer

Answer: b
Explanation: When array index goes out of bound then ArrayIndexOutOfBoundsException exceptio is thrown by the system.
Output:
$ javac exception_handling.java
$ java exception_handling
123450

9. What is the output of this program?

  1.     class exception_handling {
  2.         public static void main(String args[]) {
  3.             try {
  4.                 int i, sum;
  5.                 sum = 10;
  6.                 for (i = -1; i < 3 ;++i)
  7.                     sum = (sum / i);
  8.             }
  9.             catch(ArithmeticException e) {
  10.         	System.out.print("0");        	
  11.             } 
  12.             System.out.print(sum);
  13.         }
  14.     }

a) 0
b) 05
c) Compilation Error
d) Runtime Error
View Answer

Answer: b
Explanation: Value of variable sum is printed outside of try block, sum is declared only in try block, outside try block it is undefined.
Output:
$ javac exception_handling.java
Exception in thread “main” java.lang.Error: Unresolved compilation problem:
sum cannot be resolved to a variable

10. What is the output of this program?

  1.     class exception_handling {
  2.         public static void main(String args[]) {
  3.             try {
  4.                 int a[] = {1, 2,3 , 4, 5};
  5.                 for (int i = 0; i < 5; ++i) 
  6.                     System.out.print(a[i]);
  7.                 int x = 1/0;
  8.             }
  9.             catch(ArrayIndexOutOfBoundsException e) {
  10.         	System.out.print("A");        	
  11.             }
  12.             catch(ArithmeticException e) {     	
  13.                 System.out.print("B");
  14.             }
  15.         }
  16.     }

a) 12345
b) 12345A
c) 12345B
d) Comiplation Error
View Answer

Answer: d
Explanation: There can be more than one catch of a single try block. Here Arithmetic exception occurs instead of Array index out of bound exception hence B is printed after 12345
Output:
$ javac exception_handling.java
$ java exception_handling
12345B

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