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 – 9 | Java try/catch/finally Block MCQ-1 | Also see:- Exception Handling Interview Questions in Java, MCQ in Java
Q1) Which option can we use in the try block to stop the execution of the “finally” block?
try {
// option
} finally {
// code
}
a) return
b) System.exit(0)
c) break
d) continue
View Answer
Answer:- b) System.exit(0)Whenever a program encounters “System.exit(0)” then the program will be terminated immediately without executing the finally block.
Q2) 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);
} catch(ArithmeticException ae) {
System.out.print("B");
} finally {
System.out.print("C");
}
System.out.print("D");
}
}
a) ACD
b) ABD
c) BCD
d) ABCD
View Answer
Answer:- d) ABCDArithmeticException raised in the try block and it is matched with the catch block parameter, therefore exception will be handled by the catch block. After that finally block & remaining lines of code will be executed.
Q3) 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);
} catch(NumberFormatException nfe) {
System.out.print("B");
} finally {
System.out.print("C");
}
System.out.print("D");
}
}
a) ABCD
b) AD
c) AC
d) AC; Exception
View Answer
Answer:- d) AC; ExceptionThe exception is raised in try block but catch block parameter is not matched, hence exception is propagated to the JVM after executing the finally block.
Q4) Find the output of the below program?
public class Test {
public static void main(String[] args) {
try {
System.out.print("A");
} finally {
System.out.print("B");
}
System.out.print("C");
}
}
a) Compile-time error
b) ABC
c) AC
d) BC
View Answer
Answer:- b) ABCQ5) 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);
} finally {
System.out.print("B");
}
System.out.print("C");
}
}
a) ABC
b) Compile-time error
c) AB
d) AB; Exception
View Answer
Answer:- d) AB; ExceptionQ6) Find the output of the below program?
public class Test {
public static void main(String[] args) {
for (int i=1; i<=5; i++) {
System.out.print(i);
try {
System.out.print("A");
if(i==2) break;
}
finally {
System.out.print("B");
}
System.out.print("C");
}
System.out.print("D");
}
}
a) 1ABC2ABC3ABD
b) 1ABC2ABD
c) 1ABCBD
d) None of these
View Answer
Answer:- b) 1ABC2ABDQ7) Find the output of the below program?
public class Test {
public static void main(String[] args) {
try {
System.out.print("A");
return;
} finally {
System.out.print("B");
}
System.out.print("C");
}
}
a) AB
b) ABC
c) Compile-time error
d) Any exception
View Answer
Answer:- c) Compile-time errorAfter the return statement, only the finally block will get a chance to execute. The last line “System.out.print(“C”);” will never get chance to execute therefore this program gives compile time error:- unreachable statement, System.out.print(“C”);
Q8) Find the output of the below program?
public class Test {
public static void main(String[] args) {
m1();
}
public static void m1() {
try {
System.out.print("A");
return;
} finally {
System.out.print("B");
}
System.out.print("C");
return;
}
}
a) AB
b) ABC
c) Compile-time error
d) Any exception
View Answer
Answer:- c) Compile-time errorSimilar to the previous program, again in this program also “System.out.print(“C”);” will never get a chance to execute.
Q9) Find the output of the below program?
public class Test {
public static void main(String[] args) {
for (int i=1; i<=5; i++) {
System.out.print(i);
try {
System.out.print("A");
if(i==2) return;
}
finally {
System.out.print("B");
}
System.out.print("C");
}
System.out.print("D");
}
}
a) 1ABC2AB
b) 1ABC2ABD
c) 1ABCBD
d) None of these
View Answer
Answer:- a) 1ABC2ABOnce control reaches the “return” statement, only the finally block will be executed one time & program completed.
Q10) Find the output of the below program?
public class Test {
public static void main(String[] args) {
m1();
}
public static void m1() {
try {
System.out.print("A");
System.exit(0);
} finally {
System.out.print("B");
}
System.out.print("C");
return;
}
}
a) A
b) ABC
c) Compile-time error
d) AC
View Answer
Answer:- a) AWhenever a program encounters “System.exit(0)” then the program will be terminated immediately without executing the finally block, JVM will be shutdown.
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!