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) 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 first 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

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.







Subscribe Newsletter & Posts
Sanfoundry Internships
Follow Manish & Sanfoundry