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) Throwable
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.
advertisement
advertisement

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 will be the output of the following Java code?

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

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

Answer: b
Explanation: Myexception is self defined exception.
Output:

advertisement
$ javac Output.java
java Output
Exception

7. What will be the output of the following Java code?

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

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 will be the output of the following Java code?

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

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

Answer: c
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, compilation error occurs.

9. What will be the output of the following Java code?

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

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 will be the output of the following Java code?

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

Note : Execution command line : $ java exception_handling one
a) TypeA
b) TypeB
c) Compilation Error
d) Runtime Error
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.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.