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
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
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
Explanation: None.
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
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
Explanation: Exception class contains all the methods necessary for defining an exception. The class contains the Throwable class.
6. What will be the output of the following Java program?
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");
}
}
}
a) 12345
b) 123450
c) 1234500
d) Compilation Error
View Answer
Explanation: When array index goes out of bound then ArrayIndexOutOfBoundsException exception is thrown by the system.
Output:
$ javac exception_handling.java $ java exception_handling 123450
7. What will be the output of the following Java program?
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");
}
}
}
a) 12345
b) 12345A
c) 12345B
d) Compilation Error
View Answer
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?
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");
}
}
}
a) A
b) 0
c) 0A
d) Exception
View Answer
Explanation: None.
Output:
$ javac exception_handling.java
$ java exception_handling
0A
9. What will be the output of the following Java program?
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");
}
}
}
a) A
b) B
c) AB
d) BA
View Answer
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?
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");
}
}
}
Note: Execution command line: $ java exception_handling one two
a) TypeA
b) TypeB
c) 0TypeA
d) 0TypeB
View Answer
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.
- Check Programming Books
- Practice Information Technology MCQs
- Apply for Java Internship
- Check Java Books
- Apply for Computer Science Internship