Java Questions & Answers – Java’s Built in Exceptions

This section of our 1000+ Java MCQs focuses on Java’s built in exceptions of Java Programming Language.

1. Which of these exceptions handles the situations when an illegal argument is used to invoke a method?
a) IllegalException
b) Argument Exception
c) IllegalArgumentException
d) IllegalMethodArgumentExcepetion
View Answer

Answer: c
Explanation: None.

2. Which of these exceptions will be thrown if we declare an array with negative size?
a) IllegalArrayException
b) IllegalArraySizeExeption
c) NegativeArrayException
d) NegativeArraySizeExeption
View Answer

Answer: d
Explanation: Array size must always be positive if we declare an array with negative size then built in exception “NegativeArraySizeException” is thrown by the java’s run time system.

3. Which of these packages contain all the Java’s built in exceptions?
a) java.io
b) java.util
c) java.lang
d) java.net
View Answer

Answer: c
Explanation: None.
advertisement
advertisement

4. Which of these exceptions will be thrown if we use null reference for an arithmetic operation?
a) ArithmeticException
b) NullPointerException
c) IllegalAccessException
d) IllegalOperationException
View Answer

Answer: b
Explanation: If we use null reference anywhere in the code where the value stored in that reference is used then NullPointerException occurs.

5. Which of these class is used to create user defined exception?
a) java.lang
b) Exception
c) RunTime
d) System
View Answer

Answer: b
Explanation: Exception class contains all the methods necessary for defining an exception. The class contains the Throwable class.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

6. What will be the output of the following Java program?

  1.     class exception_handling 
  2.     {
  3.         public static void main(String args[]) 
  4.         {
  5.             try 
  6.             {
  7.                 int a[] = {1, 2,3 , 4, 5};
  8.                 for (int i = 0; i < 7; ++i) 
  9.                     System.out.print(a[i]);
  10.             }
  11.             catch(ArrayIndexOutOfBoundsException e) 
  12.             {
  13.         	System.out.print("0");        	
  14.             }
  15.         }
  16.     }

a) 12345
b) 123450
c) 1234500
d) Compilation Error
View Answer

Answer: b
Explanation: When array index goes out of bound then ArrayIndexOutOfBoundsException exception is thrown by the system.
Output:

advertisement
$ javac exception_handling.java
$ java exception_handling
123450

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

advertisement
  1.     class exception_handling 
  2.     {
  3.         public static void main(String args[]) 
  4.         {
  5.             try 
  6.             {
  7.                 int a[] = {1, 2,3 , 4, 5};
  8.                 for (int i = 0; i < 5; ++i) 
  9.                     System.out.print(a[i]);
  10.                 int x = 1/0;
  11.             }
  12.             catch(ArrayIndexOutOfBoundsException e) 
  13.             {
  14.         	System.out.print("A");        	
  15.             }
  16.             catch(ArithmeticException e) 
  17.             {     	
  18.                 System.out.print("B");
  19.             }
  20.         }
  21.     }

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

Answer: c
Explanation: There can be more than one catch of a single try block. Here Arithmetic exception occurs instead of Array index out of bound exception hence B is printed after 12345
Output:

$ javac exception_handling.java
$ java exception_handling
12345B

8. What will be the output of the following Java program?

  1.     class exception_handling 
  2.     {
  3.             static void throwexception() throws ArithmeticException 
  4.             {        
  5.                 System.out.print("0");
  6.                 throw new ArithmeticException ("Exception");
  7.             }
  8.             public static void main(String args[]) 
  9.             {
  10.             try 
  11.             {
  12.                 throwexception();
  13.             }
  14.             catch (ArithmeticException e) 
  15.             {
  16.                     System.out.println("A");
  17.             }
  18.         }
  19.     }

a) A
b) 0
c) 0A
d) Exception
View Answer

Answer: c
Explanation: None.
Output:

$ javac exception_handling.java
$ java exception_handling
0A

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

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

a) A
b) B
c) AB
d) BA
View Answer

Answer: a
Explanation: The inner try block does not have a catch which can tackle ArrayIndexOutOfBoundException hence finally is executed which prints ‘A’ the outer try block does have catch for NullPointerException exception but no such exception occurs in it hence its catch is never executed and only ‘A’ is printed.
Output:

$ javac exception_handling.java
$ java exception_handling
A

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

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

Note: Execution command line: $ java exception_handling one two
a) TypeA
b) TypeB
c) 0TypeA
d) 0TypeB
View Answer

Answer: d
Explanation: Execution command line is “$ java exception_ handling one two” hence there are two input making args.length = 2, hence “c[8] = 9” in second try block is executing which throws ArrayIndexOutOfBoundException which is caught by catch of nested try block. Hence 0TypeB is printed.
Output:

$ javac exception_handling.java
$ java exception_handling
0TypeB

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.