Java Questions & Answers – Creating Exceptions

This section of our 1000+ Java MCQs focuses on creating exceptions in Java Programming Language.

1. Which of these classes is used to define exceptions?
a) Exception
b) Trowable
c) Abstract
d) System
View Answer

Answer: a
Explanation: None.

2. Which of these methods return description of an exception?
a) getException()
b) getMessage()
c) obtainDescription()
d) obtainException()
View Answer

Answer: b
Explanation: getMessage() returns a description of the exception.

3. Which of these methods is used to print stack trace?
a) obtainStackTrace()
b) printStackTrace()
c) getStackTrace()
d) displayStackTrace()
View Answer

Answer: b
Explanation: None.

4. Which of these methods return localized description of an exception?
a) getLocalizedMessage()
b) getMessage()
c) obtainLocalizedMessage()
d) printLocalizedMessage()
View Answer

Answer: a
Explanation: None.

5. Which of these classes is super class of Exception class?
a) Throwable
b) System
c) RunTime
d) Class
View Answer

Answer: a
Explanation: None.

6. What is the output of this program?

  1.     class Myexception extends Exception {
  2. 	int detail;
  3.         Myexception(int a) {
  4.             detail = a;
  5. 	}
  6. 	public String toString() {
  7. 	    return "detail";
  8. 	}
  9.     }
  10.     class Output {
  11.         static void compute (int a) throws Myexception {
  12. 	     throw new Myexception(a);	 
  13. 	}
  14. 	public static void main(String args[]) {
  15.             try {
  16.                 compute(3);
  17.             }
  18.            catch(Myexception e) {
  19.                System.out.print("Exception");
  20.            } 
  21.         }
  22.     }

a) 3
b) Exception
c) Runtime Error
d) Compilation Error
View Answer

Answer: b
Explanation: Myexception is self defined exception.
Output:
$ javac Output.java
java Output
Exception

7. What is the output of this program?

  1.     class Myexception extends Exception {
  2. 	int detail;
  3.         Myexception(int a) {
  4.         detail = a;
  5. 	}
  6. 	public String toString() {
  7. 	    return "detail";
  8. 	}
  9.     }
  10.     class Output {
  11.         static void compute (int a) throws Myexception {
  12. 	     throw new Myexception(a);	 
  13. 	}
  14. 	public static void main(String args[]) {
  15.             try {
  16.                 compute(3);
  17.             }
  18.            catch(DevideByZeroException e) {
  19.                System.out.print("Exception");
  20.            } 
  21.         }
  22.     }

a) 3
b) Exception
c) Runtime Error
d) Compilation Error
View Answer

Answer: c
Explanation: Mexception is self defined exception, we are generating Myexception but catching DevideByZeroException which causes error.
Output:
$ javac Output.javac

8. 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) Compilation Error
d) Runtime Error
View Answer

Answer: d
Explanation: try block is throwing NullPointerException but the catch block is used to counter Arithmetic Exception. Hence NullPointerException occurs since no catch is there which can handle it, runtime error occurs.
Output:
$ javac exception_handling.java
$ java exception_handling
Exception in thread “main” java.lang.NullPointerException: Hello

9. What is the output of this program?

  1.     class Myexception extends Exception {
  2. 	int detail;
  3.         Myexception(int a) {
  4.         detail = a;
  5. 	}
  6. 	public String toString() {
  7. 	    return "detail";
  8. 	}
  9.     }
  10.     class Output {
  11.         static void compute (int a) throws Myexception {
  12. 	     throw new Myexception(a);	 
  13. 	}
  14. 	public static void main(String args[]) {
  15.             try {
  16.                 compute(3);
  17.             }
  18.            catch(Exception e) {
  19.                System.out.print("Exception");
  20.            } 
  21.         }
  22.     }

a) 3
b) Exception
c) Runtime Error
d) Compilation Error
View Answer

Answer: b
Explanation: Myexception is self defined exception.
Output:
$ javac Output.javac
java Output
Exception

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

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

Answer: c
Explanation: try without catch or finally
Output:
$ javac exception_handling.java
$ java exception_handling
error: ‘try’ without ‘catch’, ‘finally’ or resource declarations

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
Follow Manish & Sanfoundry