Java Multiple Choice Questions & Answers on Exceptions Types for Freshers

https://www.computersprofessor.com/2017/12/java-multiple-choice-questions-answers_28.html
1. Which of these is a super class of all exceptional type classes?
a) String
b) RuntimeExceptions
c) Throwable
d) Cachable
Answer: c
Explanation: All the exception types are subclasses of the built in class Throwable.
2. Which of these class is related to all the exceptions that can be caught by using catch?
a) Error
b) Exception
c) RuntimeExecption
d) All of the mentioned
Answer: b
Explanation: Error class is related to java run time error that can’t be caught usually, RuntimeExecption is subclass of Exception class which contains all the exceptions that can be caught.
3. Which of these class is related to all the exceptions that cannot be caught?
a) Error
b) Exception
c) RuntimeExecption
d) All of the mentioned
Answer: a
Explanation: Error class is related to java run time error that can’t be caught usually, RuntimeExecption is subclass of Exception class which contains all the exceptions that can be caught.
4. Which of these handles the exception when no catch is used?
a) Default handler
b) finally
c) throw handler
d) Java run time system
Answer: a
5. Which of these keywords is used to manually throw an exception?
a) try
b) finally
c) throw
d) catch
Answer: c
6. What is the output of this program?
class exception_handling {
public static void main(String args[]) {
try {
System.out.print("Hello" + " " + 1 / 0);
}
finally {
System.out.print("World");
}
}
}
a) Hello
b) World
c) Compilation Error
d) First Exception then World
b) World
c) Compilation Error
d) First Exception then World
Answer: d
7. What is the output of this program?
class exception_handling {
public static void main(String args[]) {
try {
int a, b;
b = 0;
a = 5 / b;
System.out.print("A");
}
catch(ArithmeticException e) {
System.out.print("B");
}
}
}
a) A
b) B
c) Compilation Error
d) Runtime Error
b) B
c) Compilation Error
d) Runtime Error
Answer: b
8. What is the output of this 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
b) 123450
c) 1234500
d) Compilation Error
Answer: b
Explanation: When array index goes out of bound then ArrayIndexOutOfBoundsException exceptio is thrown by the system.
9. What is the output of this program?
class exception_handling {
public static void main(String args[]) {
try {
int i, sum;
sum = 10;
for (i = -1; i < 3 ;++i)
sum = (sum / i);
}
catch(ArithmeticException e) {
System.out.print("0");
}
System.out.print(sum);
}
}
a) 0
b) 05
c) Compilation Error
d) Runtime Error
b) 05
c) Compilation Error
d) Runtime Error
Answer: c
Explanation: Since sum is declared inside try block and we are trying to access it outside the try block it results in error.
10. What is the output of this 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) Comiplation Error
b) 12345A
c) 12345B
d) Comiplation Error
Answer: c
Explanation: There can be more than one catch for a single try block. Here Arithmetic exception(/ by 0) occurs instead of Array index out of bound exception, so 2nd catch block is executed.