250+ TOP MCQs on Creating Exceptions and Answers

Java MCQs 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

Answer: a
Clarification: None.

advertisement

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

Answer: b
Clarification: 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()

Answer: b
Clarification: None.

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

Answer: a
Clarification: None.

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

Answer: a
Clarification: 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

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

advertisement

$ javac Output.java
java Output
Exception

7. 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(DevideByZeroException e)
  26.            {
  27.                System.out.print("Exception");
  28.            }
  29.         }
  30.     }

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

Answer: c
Clarification: 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?

advertisement

  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

Answer: d
Clarification: 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 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

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

$ javac Output.javac
java Output
Exception

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

advertisement

  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

Answer: c
Clarification: try without catch or finally
Output:

$ javac exception_handling.java
$ java exception_handling
error: 'try' without 'catch', 'finally' or resource declarations

Leave a Reply

Your email address will not be published. Required fields are marked *