Java Multiple Choice Questions & Answers on Exception Handling Basics for Freshers

https://www.computersprofessor.com/2017/12/java-multiple-choice-questions-answers_26.html
1. When does Exceptions in Java arises in code sequence?
a) Run Time
b) Compilation Time
c) Can Occur Any Time
d) None of the mentioned
Answer: a
Explanation: Exceptions in java are run-time errors.
2. Which of these keywords is not a part of exception handling?
a) try
b) finally
c) thrown
d) catch
Answer: c
Explanation: Exceptional handling is managed via 5 keywords – try, catch, throws, throw and finally.
3. Which of these keywords must be used to monitor for exceptions?
a) try
b) finally
c) throw
d) catch
Answer: a
4. Which of these keywords must be used to handle the exception thrown by try block in some rational manner?
a) try
b) finally
c) throw
d) catch
Answer: d
Explanation: If an exception occurs within the try block, it is thrown and cached by catch block for processing.
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);
}
catch(ArithmeticException e) {
System.out.print("World");
}
}
}
a) Hello
b) World
c) HelloWorld
d) Hello World
b) World
c) HelloWorld
d) Hello World
Answer: b
Explanation: System.ou.print() function fist converts the whole parameters into string and then prints, before “Hello” goes to output stream 1 / 0 error is encountered which is cached by catch block printing just “World” .
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, b;
b = 0;
a = 5 / b;
System.out.print("A");
}
catch(ArithmeticException e) {
System.out.print("B");
}
finally {
System.out.print("C");
}
}
}
a) A
b) B
c) AC
d) BC
b) B
c) AC
d) BC
Answer: d
Explanation: finally keyword is used to execute the code before try and catch block end.
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: Value of variable sum is printed outside of try block, sum is declared only in try block, outside try block it is undefined.
10. 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);
System.out.print(i);
}
}
catch(ArithmeticException e) {
System.out.print("0");
}
}
}
a) -1
b) 0
c) -10
d) -101
b) 0
c) -10
d) -101
Answer: c
Explanation: For the 1st iteration -1 is displayed. The 2nd exception is caught in catch block and 0 is displayed.