Java MCQ – Home
Java Basic MCQ
➤ Java Hello World MCQ
➤ Find Java Keywords
➤ Java Identifier Quiz
➤ Java Data Types Quiz
➤ If-Else MCQ in Java-1
➤ If-Else MCQ in Java-2
Object class MCQ
➤ Object Class Quiz
➤ equals() Method Quiz
➤ Hashcode Value Quiz
➤ toString() Method Quiz
➤ Clone() Method Quiz
Multithreading MCQ
➤ Define a Thread-1
➤ Define a Thread-2
➤ Get/set ThreadName
➤ Thread State MCQ
➤ Thread Priority MCQ
➤ Yield(), join() & sleep()
➤ Synchronization MCQ
➤ Interthread Comms
➤ Deadlock, Daemon
Exception Handling
➤ Exception Handling-1
➤ Exception Handling-2
➤ Exception Handling-3
➤ Java try-catch MCQ-1
➤ Java try-catch MCQ-2
➤ Java try-catch MCQ-3
➤ Nested try-catch MCQ
➤ throw Keyword MCQ
➤ finally Block MCQ-1
➤ finally Block MCQ-2
➤ throws Keyword MCQ
Generics MCQ
➤ Java Generics Quiz-1
➤ Java Generics Quiz-2
➤ Java Generics Quiz-3
Collection Framework
➤ Collections Quiz-1
➤ Collections Quiz-2
➤ ArrayList MCQ-1
➤ ArrayList MCQ-2
➤ LinkedList MCQ
➤ Vector Stack MCQ
➤ Java Cursors MCQ
➤ Java TreeSet MCQ
➤ TreeSet & Comparator
Exception Handling MCQ Part – 5 | Java try-catch MCQ – 2 | Also see:- Exception Handling Interview Questions in Java, MCQ in Java
Q1) Find the output of the below Java program?
public class Test {
public static void main(String[] args) {
System.out.print("A");
try {
System.out.print("B");
System.out.print(10/0);
} catch (ArithmeticException e) {
System.out.print("C");
System.out.print(9/0);
}
System.out.print("D");
}
}
a) AB; ArithmeticException
b) ABC; ArithmeticException
c) ABD; ArithmeticException
d) None of these
View Answer
Answer:- b) ABC; ArithmeticExceptionException is raised in the try block, therefore the catch block will be executed. The exception is also raised in the catch block. Now, the program will be terminated without executing the remaining lines of code.
Q2) Find the output of the below program?
public class Test {
public static void main(String[] args) {
System.out.print("A");
m1();
System.out.print(9/0);
System.out.print("B");
}
public static void m1() {
System.out.print("C");
m2();
System.out.print("D");
}
public static void m2() {
System.out.print("E");
System.out.print("F");
}
}
a) ACEF; ArithmeticException
b) ACEFD; ArithmeticException
c) ABC; ArithmeticException
d) None of these
View Answer
Answer:- b) ACEFD; ArithmeticExceptionQ3) Find the output of the below Java program?
public class Test {
public static void main(String[] args) {
System.out.print("A");
try {
System.out.print("B");
System.out.print(Integer.parseInt("C"));
} catch (ArithmeticException e) {
System.out.print("D");
System.out.print(9/0);
}
System.out.print("E");
}
}
a) ABD; ArithmeticException
b) ABE
c) ABDE
d) AB; NumberFormatException
View Answer
Answer:- d) AB; NumberFormatExceptionThe exception is raised in the try block, but the catch block is not matched hence the program will be terminated without executing the catch block and remaining code.
Q4) Find the output of the below Java program?
public class Test {
public static void main(String[] args) {
System.out.print("A");
try {
System.out.print("B");
System.out.print("C");
} catch (ArithmeticException e) {
System.out.print(d);
System.out.print("E");
}
System.out.print("F");
}
}
a) ABCF
b) Compile-time error
c) ArithmeticException
d) None of these
View Answer
Answer:- b) Compile-time errorIn this program, the 1st line of the catch block contains the “d” variable which is not declared. If an exception is not raised in the try block then the catch block will not be executed. But it doesn’t mean the compiler will also ignore the errors in the catch block.
Q5) Find the output of the below Java program?
public class Test {
public static void main(String[] args) {
System.out.print("A");
try {
System.out.print("B");
System.out.print("C");
} catch (ArithmeticException e) {
System.out.print("D");
System.out.print("E");
}
System.out.print(10/0);
}
}
a) ABCDE; ArithmeticException
b) ABC; ArithmeticException
c) ArithmeticException
d) ABC
View Answer
Answer:- b) ABC; ArithmeticExceptionException is not raised in the try block, therefore the catch block will not be executed. But the exception is raised at the last line which is not handled.
Q6) Find the output of the below Java program?
public class Test {
public static void main(String[] args) {
System.out.print("A");
try {
System.out.print("B");
System.out.print(Integer.parseInt("C"));
} catch (Exception e) {
System.out.print("D");
System.out.print("E");
}
System.out.print("F");
}
}
a) AB; NumberFormatException
b) ABDEF
c) ABF
d) Compile-time error
View Answer
Answer:- b) ABDEFNumberFormatException is raised in the try block, the catch block is handling “Exception” which is a super class of NumberFormatException. Hence exceptions will be handled by the catch block, and the program will be completed successfully.
Q7) Find the output of the below program?
public class Test {
public static void main(String[] args) {
try {
System.out.print("A");
System.out.print(9/0);
System.out.print(Integer.parseInt("B"));
System.out.print("C");
} catch(ArithmeticException e) {
System.out.println("D");
} catch(NumberFormatException e) {
System.out.println("E");
}
}
}
a) ADE
b) A
c) AD
d) AE
View Answer
Answer:- c) ADQ8) Find the output of the below program?
public class Test {
public static void main(String[] args) {
try {
System.out.print("A");
System.out.print(9/0);
System.out.print("B");
} catch(ArithmeticException e) {
System.out.println("C");
System.out.print(Integer.parseInt("D"));
} catch(NumberFormatException e) {
System.out.println("E");
}
}
}
a) ACE
b) A; NumberFormatException
c) AC; NumberFormatException
d) ACE; NumberFormatException
View Answer
Answer:- c) AC; NumberFormatExceptionQ9) Find the output of the below program?
public class Test {
public static void main(String[] args) {
try {
System.out.print("A");
System.out.print(9/0);
System.out.print("B");
} catch(Exception e) {
System.out.println("C");
} catch(ArithmeticException e) {
System.out.println("D");
}
}
}
a) AC
b) AD
c) ACD
d) Compile-time error
View Answer
Answer:- d) Compile-time errorIf we are using a try with multiple catch blocks then the order of the catch block is very important. We have to take the child first & then parent otherwise we will get a compile time error saying:- Exception has already been caught.
Q10) Find the output of the below program?
public class Test {
public static void main(String[] args) {
try {
System.out.print("A");
System.out.print(9/0);
System.out.print("B");
} catch(ArithmeticException e) {
System.out.println("C");
} catch(ArithmeticException e) {
System.out.println("D");
}
}
}
a) AC
b) AD
c) ACD
d) Compile-time error
View Answer
Answer:- d) Compile-time errorWe can’t declare more than one catch block to handle the same exception. Both catch blocks want to handle ArithmeticException, which is invalid.
If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!