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 – 10 | Java try/catch/finally Block MCQ-2 | 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) {
System.out.println(m1());
}
public static int m1() {
try {
System.out.print("A");
return 1;
} finally {
System.out.print("B");
return 2;
}
}
}
a) A1
b) AB1
c) AB2
d) A2
View Answer
Answer:- c) AB2The value returned from try block or from catch block is replaced by finally block returning value.
Q2) Find the output of the below program?
public class Test {
public static void main(String[] args) {
System.out.println(m1());
}
public static int m1() {
try {
System.out.print(10/0);
} finally {
System.out.print("B");
return 2;
}
}
}
a) ArithmeticException
b) B2
c) 2
d) B; ArithmeticException
View Answer
Answer:- b) B2If we place the return statement in the finally block, then the exception raised in the try block is never propagated to the calling method, because the returned value replaces the exception object in JDH (Java default handler).
Q3) Find the output of the below program?
public class Test {
public static void main(String[] args) {
System.out.println(m1());
}
public static int m1() {
try {
System.out.print(10/0);
} finally {
System.out.print("A");
}
return 20;
}
}
a) ArithmeticException
b) A2
c) 2
d) A; ArithmeticException
View Answer
Answer:- d) A; ArithmeticExceptionThis time the return statement is outside of the finally block, Exception is raised before returning the value.
Q4) 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(10/0);
} finally {
System.out.print("A");
return;
}
}
}
a) ArithmeticException
b) Compile-time error
c) A
d) A; ArithmeticException
View Answer
Answer:- c) AException raised in try block is never propagated to the calling method even though it’s return type is void.
Q5) Find the output of the below program?
public class Test {
public static void main(String[] args) {
m1();
System.out.println("A");
}
public static void m1() {
try {
System.out.print(10/0);
} finally {
int num = Integer.parseInt("two");
}
}
}
a) ArithmeticException
b) NumberFormatException
c) A
d) None of these
View Answer
Answer:- b) NumberFormatExceptionThe value returned from try block or the exception throwing from try catch block will be replaced with finally block throwing exception, and then finally block exception will be propagated to the main() method.
Q6) Find the output of the below program?
public class Test {
public static void main(String[] args) {
System.out.print(m1());;
}
public static int m1() {
try {
System.out.print("A");
return 10;
} finally {
System.out.print("B");
return 20;
}
return 30;
}
}
a) AB10
b) AB20
c) AB30
d) Compile-time error
View Answer
Answer:- d) Compile-time errorSince the finally block contains a return statement therefore the remaining line of m1() method will never get a chance of execution. Hence it gives compile time error:- unreachable statement, return 30;
Q7) Find the output of the below program?
public class Test {
public static void main(String[] args) {
System.out.print(m1());;
}
public static int m1() {
try {
System.out.print("A");
return 10;
} catch(Exception e) {
System.out.println("Z");
} finally {
System.out.print("B");
if(true) return 20;
}
System.out.print("C");
return 30;
}
}
a) AB10
b) AB20
c) ABC30
d) Compile-time error or Any Exception
View Answer
Answer:- b) AB20The finally block return statement will replace all existing return values. The statements after the finally block will not be executed. Since the finally block contains a return statement inside the if block therefore the compiler doesn’t give compile time error because if block executes only when condition becomes true. The compiler only takes care of syntaxes, it doesn’t check whether the if block condition is true or not.
Q8) Find the output of the below program?
public class Test {
public static void main(String[] args) {
System.out.print(m1());;
}
public static int m1() {
try {
System.out.print("A");
return 10;
} catch(Exception e) {
System.out.println("Z");
} finally {
System.out.print("B");
if(false) return 20;
}
System.out.print("C");
return 30;
}
}
a) AB10
b) AB20
c) ABC30
d) Compile-time error or Any Exception
View Answer
Answer:- a) AB10The condition of if block inside finally block is false therefore existing returned value 10 will be returned to the main() method.
Q9) Find the output of the below program?
public class Test {
public static void main(String[] args) {
try {
int a = 10;
} catch(Exception e) {
System.out.println(a);
}
}
}
a) 10
b) 0
c) Compile-time error
d) Exception
View Answer
Answer:- c) Compile-time errorVariables declared in one block are accessible only inside that block. In this program, a variable is declared in try block therefore it is not accessible in catch block.
Q10) Find the output of the below program?
public class Test {
public static void main(String[] args) {
throw new ArithmeticException("divided by zero");
}
}
a) ArithmeticException: divided by zero
b) ArithmeticException: / by zero
c) Compile-time error
d) None of these
View Answer
Answer:- a) ArithmeticException: divided by zeroIf 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!