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 – 7 | Java Nested try-catch MCQ | Also see:- Exception Handling Interview Questions in Java, MCQ in Java
Q1) Find the output of the below program?
public class Test {
public static void main(String[] args) {
try {
System.out.print("A");
} catch(ArithmeticException e) {
System.out.println("B");
}
try {
System.out.print("C");
} catch(ArithmeticException e) {
System.out.println("D");
}
}
}
a) AC
b) BD
c) ABCD
d) Compile-time error
View Answer
Answer:- a) ACWe can use more than one try block in a method.
Q2) Find the output of the below program?
public class Test {
public static void main(String[] args) {
try {
System.out.print("A");
} catch(String e) {
System.out.println("B");
}
}
}
a) A
b) B
c) Compile-time error
d) Runtime error
View Answer
Answer:- c) Compile-time errorIn catch block, parameter must be of type java.lang.Throwable or its subclass else it leads to compile time error. In this program we will get:- incompatible types: String cannot be converted to Throwable.
Q3) 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");
}
}
a) ABC
b) AC
c) Compile-time error
d) Runtime exception
View Answer
Answer:- c) Compile-time errorThe try block must follow either 0 or “N” number of catch blocks or “1” finally block else it leads to compile time error:- “try without catch or finally”
Q4) Find the output of the below program?
public class Test {
public static void main(String[] args) {
try {
System.out.print("A");
try {
System.out.print("B");
System.out.print(9/0);
} catch(NumberFormatException nfe) {
System.out.print("C");
}
} catch(NumberFormatException e) {
System.out.print("D");
} catch(ArithmeticException e) {
System.out.print("E");
} catch(Exception e) {
System.out.print("F");
}
}
}
a) AB; ArithmeticException
b) ABE
c) Compile-time error
d) No output
View Answer
Answer:- b) ABEQ5) Find the output of the below program?
public class Test {
public static void main(String[] args) {
try {
System.out.print("A");
try {
System.out.print("B");
} catch(NumberFormatException nfe) {
System.out.print("C");
}
System.out.print("D");
} catch(NumberFormatException e) {
System.out.print("E");
}
}
}
a) ABCD
b) AB
c) ABD
d) ABE
View Answer
Answer:- c) ABDQ6) Find the output of the below program?
public class Test {
public static void main(String[] args) {
System.out.print(9/0);
try {
System.out.print("A");
try {
System.out.print("B");
} catch(NumberFormatException nfe) {
System.out.print("C");
}
System.out.print("D");
} catch(NumberFormatException e) {
System.out.print("E");
}
}
}
a) Compile-time error
b) Exception, program terminated
c) ABD
d) None of these
View Answer
Answer:- b) Exception, program terminatedException is raised before the outer try block.
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(Integer.parseInt("X"));
try {
System.out.print("B");
} catch(NumberFormatException nfe) {
System.out.print("C");
}
System.out.print("D");
} catch(NumberFormatException e) {
System.out.print("E");
}
}
}
a) AB
b) ABD
c) AE
d) Exception, program terminated
View Answer
Answer:- c) AEException is raised in the outer try before the inner try-catch block, therefore the inner try-catch block won’t do anything for this exception.
Q8) Find the output of the below program?
public class Test {
public static void main(String[] args) {
try {
System.out.print("A");
try {
System.out.print("B");
System.out.print(Integer.parseInt("X"));
} catch(NumberFormatException nfe) {
System.out.print("C");
}
System.out.print("D");
} catch(NumberFormatException e) {
System.out.print("E");
}
System.out.print("F");
}
}
a) ABC
b) ABDE
c) ABCDF
d) Exception, program terminated
View Answer
Answer:- c) ABCDFException raised in inner try block. If an exception matches both the inner and outer catch block then the inner block will be executed.
Q9) Find the output of the below program?
public class Test {
public static void main(String[] args) {
try {
System.out.print("A");
try {
System.out.print("B");
System.out.print(9/0);
} catch(NumberFormatException nfe) {
System.out.print("C");
}
System.out.print("D");
} catch(NumberFormatException e) {
System.out.print("E");
}
System.out.print("F");
}
}
a) AB; ArithmeticException
b) ABE; ArithmeticException
c) ABDE; ArithmeticException
d) ABE
View Answer
Answer:- a) AB; ArithmeticExceptionException is raised in the inner try block but the catch block is not matched in both inner & outer block therefore the program will be terminated abnormally.
Q10) Find the output of the below program?
public class Test {
public static void main(String[] args) {
try {
System.out.print("A");
} catch(NumberFormatException nfe) {
System.out.println("B");
} finally {
System.out.print("C");
}
System.out.print("D");
}
}
a) AD
b) AC
c) ACD
d) BC
View Answer
Answer:- b) ACDIf 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!