Java MCQs 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? Answer: c 2. Which of these exceptions will be thrown if we declare an array with negative size? Answer: d 3. Which of these packages contain all the Java’s built in exceptions? Answer: c 4. Which of these exceptions will be thrown if we use null reference for an arithmetic operation? Answer: b 5. Which of these class is used to create user defined exception? Answer: b 6. What will be the output of the following Java program? a) 12345 7. What will be the output of the following Java program? a) 12345 8. What will be the output of the following Java program? a) A 9. What will be the output of the following Java program? a) A 10. What will be the output of the following Java program? Note: Execution command line: $ java exception_handling one two
a) IllegalException
b) Argument Exception
c) IllegalArgumentException
d) IllegalMethodArgumentExcepetion
Clarification: None.
a) IllegalArrayException
b) IllegalArraySizeExeption
c) NegativeArrayException
d) NegativeArraySizeExeption
Clarification: 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.
a) java.io
b) java.util
c) java.lang
d) java.net
Clarification: None.
a) ArithmeticException
b) NullPointerException
c) IllegalAccessException
d) IllegalOperationException
Clarification: If we use null reference anywhere in the code where the value stored in that reference is used then NullPointerException occurs.
a) java.lang
b) Exception
c) RunTime
d) System
Clarification: Exception class contains all the methods necessary for defining an exception. The class contains the Throwable class.
class exception_handling
{
public static void main(String args[])
{
try
{
int a[] = {1, 2,3 , 4, 5};
for (int i = 0; i < 7; ++i)
System.out.print(a[i]);
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.print("0");
}
}
}
b) 123450
c) 1234500
d) Compilation Error
Clarification: When array index goes out of bound then ArrayIndexOutOfBoundsException exception is thrown by the system.
Output:
$ javac exception_handling.java
$ java exception_handling
123450
class exception_handling
{
public static void main(String args[])
{
try
{
int a[] = {1, 2,3 , 4, 5};
for (int i = 0; i < 5; ++i)
System.out.print(a[i]);
int x = 1/0;
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.print("A");
}
catch(ArithmeticException e)
{
System.out.print("B");
}
}
}
b) 12345A
c) 12345B
d) Compilation Error
Clarification: 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
class exception_handling
{
static void throwexception() throws ArithmeticException
{
System.out.print("0");
throw new ArithmeticException ("Exception");
}
public static void main(String args[])
{
try
{
throwexception();
}
catch (ArithmeticException e)
{
System.out.println("A");
}
}
}
b) 0
c) 0A
d) Exception
Clarification: None.
Output:
$ javac exception_handling.java
$ java exception_handling
0A
class exception_handling
{
public static void main(String args[])
{
try
{
int a = 1;
int b = 10 / a;
try
{
if (a == 1)
a = a / a - a;
if (a == 2)
{
int c[] = {1};
c[8] = 9;
}
finally
{
System.out.print("A");
}
}
catch (NullPointerException e)
{
System.out.println("B");
}
}
}
b) B
c) AB
d) BA
Clarification: 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
class exception_handling
{
public static void main(String args[])
{
try
{
int a = args.length;
int b = 10 / a;
System.out.print(a);
try
{
if (a == 1)
a = a / a - a;
if (a == 2)
{
int c = {1};
c[8] = 9;
}
}
catch (ArrayIndexOutOfBoundException e)
{
System.out.println("TypeA");
}
catch (ArithmeticException e)
{
System.out.println("TypeB");
}
}
}
a) TypeA
b) TypeB
c) 0TypeA
d) 0TypeB
Clarification: 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