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: c
Explanation: Since sum is declared inside try block and we are trying to access it outside the try block it results in error.
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: c
Explanation: There can be more than one catch for a single try block. Here Arithmetic exception(/ by 0) occurs instead of Array index out of bound exception, so 2nd catch block is executed.
Output:
$ javac exception_handling.java
$ java exception_handling
12345B

Sanfoundry Global Education & Learning Series – Java Programming Language.

Subscribe Newsletter & Posts
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.

1000 Java MCQs | Quiz

Integer and Floating Data Types
Character and Boolean Data Types
Literals & Variables
Type Conversions, Promotions and Castings
Arrays
Arithmetic Operators
Bitwise Operators
Relational Operators and Boolean Logic Operators
Assignment Operators and Operator Precedence
Control Statements
Class Fundamentals & Declaring objects
Introduction to Methods
Constructors & Garbage Collection
Overloading Methods & Argument Passing
Access Control
Arrays Revisited & Keyword static
String Class
Inheritance
Method overriding
The Object Class
String Handling Basics
Character Extraction
String Comparison
Searching & Modifying a String
StringBuffer class
StringBuffer Methods
Packages
Interfaces
Java.lang Introduction
Java.lang - Integer, Long And Character Wrappers
Java.lang - Void, Process and System Class
Java.lang - Object & Math Class
Java.io Introduction
Java.io Byte Streams
Java.io Character Streams
Serialization
Serialization & Deserialization
Networking Basics
URL class
HttpResponse & URLConnection Class
networking - Server, Sockets & httpd Class
networking - httpd.java Class
networking - Datagrams
Java.util - ArrayList Class
Java.util - LinkedList, HashSet & TreeSet Class
Java.util - Maps
Java.util - Array Class
Java.util - Vectors & Stack
Java.util - Dictionary, Hashtable & Properties
Java.util - BitSet & Date class
Methods Taking Parameters
Exceptional Handling Basics
Exceptions Types
Throw, Throws & Nested Try
isAlive(), Join() & Thread Synchronization
Implementing Runnable interface for Threads
Thread class
Multithreading Basics
Networking Basics
Finally & Built in Exceptions
Creating Threads
Input & Output Basics
Reading Console Input
Writing Console Output
Reading & Writing Files
Applets Fundamentals
Core Java API Packages
Remote Method Invocation (RMI)
Text Formatting
Event Handling Basics
ActionEvent & AdjustmentEvent Class
ComponentEvent, ContainerEvent & FocusEvent Class
MouseEvent, TextEvent & WindowEvent Class
Event Listeners Interfaces
Java.lang - Rounding Functions
Java.lang - Miscellaneous Math Methods & StrictMath Class
Java.lang - ThreadGroup class & Runnable Interface
Java.lang - System Class Advance
Java.lang - Double & Float Wrappers
Java.lang - Byte & Short Wrappers
Java.lang - Character Wrapper Advance
Java.lang - Boolean Wrapper Advance
Java.lang - Runtime & ClassLoader Classes
java.lang - Class
Collection Framework Overview
Collections Interface
Collection Algorithms
Iterators
Locale & Random Classes
Observable & Timer Class
Restrictions On Generics
Wildcards
Recursion
Command Line Arguments
Inheritance - Abstract Class and Super
Try & Catch
Creating Exceptions
Generics
Generic Methods
Type Interface
Subscribe Newsletter & Posts
Follow Manish & Sanfoundry