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 – 4 | Java try-catch MCQ – 1 | 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(9/0);
try {
System.out.print("A");
System.out.print("B");
} catch (ArithmeticException e) {
System.out.print("C");
System.out.print("D");
}
System.out.print("E");
}
}
a) AB
b) CD
c) ABCDE
d) ArithmeticException
View Answer
Answer:- d) ArithmeticExceptionException in thread “main” java.lang.ArithmeticException: / by zero; at Test.main(Test.java:3). The “System.out.print(9/0);” line is outside of the try-catch block.
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("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(9/0);
System.out.print("F");
}
}
a) ArithmeticException
b) ACEFDB
c) ACEDB
d) ACE; ArithmeticException
View Answer
Answer:- d) ACE; ArithmeticExceptionThe exception is raised in the m2() method, after raising the exception program will be terminated abnormally.
Q3) 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(9/0);
System.out.print("B");
} catch (ArithmeticException e) {
System.out.print("C");
System.out.print("D");
}
System.out.print("E");
}
}
a) ACD
b) ACDE
c) ABCDE
d) ArithmeticException
View Answer
Answer:- b) ACDEThe exception is raised in the try block and it is matched with the catch block. Therefore, the program will be completed successfully.
Q4) Find the output of the below program?
public class Test {
public static void main(String[] args) {
System.out.print("A");
try {
System.out.print(9/0);
}
catch (Exception e) {}
}
}
a) A
b) A0
c) Compile-time error
d) Runtime exception
View Answer
Answer:- a) AException is raised in the try block, and the catch block is also matched but it isn’t performing any operation. Hence the program will run successfully.
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(9/0);
} catch (ArithmeticException e) {
System.out.print("C");
System.out.print("D");
}
System.out.print("E");
}
}
a) ACD
b) ACDE
c) ABCDE
d) ArithmeticException
View Answer
Answer:- c) ABCDEQ6) Find the output of the below program?
public class Test {
public static void main(String[] args) {
System.out.print("A");
m1();
System.out.print("B");
}
public static void m1() {
System.out.print("C");
m2();
System.out.print(9/0);
System.out.print("D");
}
public static void m2() {
System.out.print("E");
System.out.print("F");
}
}
a) ACEF; ArithmeticException
b) ACEFDB; ArithmeticException
c) ABC; ArithmeticException
d) None of these
View Answer
Answer:- a) ACEF; ArithmeticExceptionQ7) Find the output of the below program?
public class Test {
public static void main(String[] args) {
System.out.print("A");
try {
System.out.print("B");
}
System.out.print("C");
catch (Exception e) {
System.out.print("D");
}
}
}
a) ABCD
b) ABC
c) Compile-time error
d) Runtime exception
View Answer
Answer:- c) Compile-time errorThe catch block must be placed immediately after the try block else it leads to compile time error: catch without try.
Q8) 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("C");
System.out.print("D");
}
System.out.print("E");
}
}
a) ABCDE
b) NumberFormatException
c) AB; NumberFormatException
d) ABE
View Answer
Answer:- c) AB; NumberFormatExceptionThe NumberFormatException raised in the try block but the catch block is not handling it therefore the program will be terminated abnormally, catch block & remaining lines won’t execute.
Q9) Find the output of the below program?
public class Test {
try {
System.out.print("A");
} catch (Exception e) {
System.out.print("B");
}
public static void main(String[] args) {
try {
System.out.print("C");
} catch(Exception e) {
System.out.println("D");
}
}
}
a) AC
b) BD
c) ABCD
d) Compile-time error
View Answer
Answer:- d) Compile-time errorThe try-catch blocks are not allowed at class level directly.
Q10) 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(9/0);
System.out.print("D");
}
System.out.print("E");
}
}
a) ABCE
b) ABC; ArithmeticException
c) ABCDE
d) None of these
View Answer
Answer:- a) ABCEIn this program, exceptions will not be raised in the try block hence the catch block will not be executed. Program will complete successfully without executing a catch block.
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!