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 – 6 | Java try-catch MCQ – 3 | 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.println("Hello");
} catch(ArithmeticException e) { }
}
}
a) Hello
b) Compile-time error
c) Exception will be raised
d) Compiled Successfully, but no output
View Answer
Answer:- a) HelloThe ArithmeticException is an unchecked exception therefore if we are catching an ArithmeticException type exception then the compiler will not give any error if the try block will not raise ArithmeticException. It will be compiled successfully and give “Hello” as output.
Q2) Find the output of the below program?
import java.io.*;
public class Test {
public static void main(String[] args) {
try {
System.out.println("Hello");
} catch(IOException e) { }
}
}
a) Hello
b) Compile-time error
c) Exception will be raised
d) Compiled Successfully, but no output
View Answer
Answer:- b) Compile-time errorThe IOException is a fully checked exception therefore we should catch it only when try-block throws IOException. In this program, we will get a compile time error:- exception IOException is never thrown in the body of the corresponding try statement.
Q3) Find the output of the below program?
public class Test {
public static void main(String[] args) {
try {
System.out.println("Hello");
} catch(Exception e) { }
}
}
a) Hello
b) Compile-time error
c) Exception will be raised
d) Compiled Successfully, but no output
View Answer
Answer:- a) HelloThe Exception is a partially checked exception therefore if we are catching an Exception type exception then the compiler will not give any error when the try block will not raise an Exception type exception.
Q4) Find the output of the below program?
public class Test {
public static void main(String[] args) {
try {
System.out.println("Hello");
} catch(InterruptedException e) { }
}
}
a) Hello
b) Compile-time error
c) Exception will be raised
d) Compiled Successfully, but no output
View Answer
Answer:- b) Compile-time errorThe InterruptedException is also a fully checked exception therefore we should catch it only when try block throw InterruptedException. In this program, we will get a compile time error:- exception InterruptedException is never thrown in the body of the corresponding try statement.
Q5) Find the output of the below program?
public class Test {
public static void main(String[] args) {
try {
System.out.println("Hello");
} catch(Error e) { }
}
}
a) Hello
b) Compile-time error
c) Exception will be raised
d) Compiled Successfully, but no output
View Answer
Answer:- a) HelloThe Error is a partially checked exception therefore if we are catching Error type exception then compiler will not give any error when the try block will not raise Error type exception.
Note:- Within the try block if there is no chance of raising an exception then we can’t write a catch block for that exception otherwise we will get a compile-time error:- Exception <exception-name> is never thrown in the body of the corresponding try statement. But this rule is only applicable for fully checked exceptions. The only possible partially checked exceptions in Java are:- Exception, Throwable.
Q6) We can develop checked exceptions deriving from?
a) java.lang.Exception
b) java.lang.Error
c) java.lang.RuntimeException
d) java.lang.Throwable
View Answer
Answer:- a) java.lang.ExceptionTo develop a checked exception in Java the class should be extended from java.lang.Exception class. In its respective post, we discussed why we should not use Error, RuntimeException, and Throwable class to develop custom exceptions.
Also see:- Develop User-defined Custom exception, Java Custom Exception Example
try with resources
Q7) Find the output of the below program?
import java.io.*;
public class Test {
public static void main(String[] args) {
try(FileWriter fw = new FileWriter("output.txt")) {
fw = new FileWriter("file1.txt");
} catch(IOException e) {
System.out.println("A");
}
}
}
a) IOException
b) A
c) Compile-time error
d) None of these
View Answer
Answer:- c) Compile-time errorAll resources reference variables are implemented as final therefore within the try block we can’t perform re-assignment otherwise we will get a compile time error.
Multi catch block
Q8) Which exception will be raised in the below program?
public class Test {
public static void main(String[] args) {
try {
String s = null;
System.out.println(s.length());
System.out.println(9/0);
} catch(ArithmeticException | NullPointerException e) {
e.printStackTrace();
}
}
}
a) ArithmeticException
b) NullPointerException
c) Both ArithmeticException & NullPointerException
d) Compile-time error
View Answer
Answer:- b) NullPointerExceptionIn this program, s.length() is called on a null reference variable. Hence it gives NullPointerException.
Q9) What will be the output of the below program?
public class Test {
public static void main(String[] args) {
try {
System.out.println(9/0);
} catch(ArithmeticException | ArithmeticException e) {
e.printStackTrace();
}
}
}
a) ArithmeticException
b) NullPointerException
c) Both ArithmeticException & NullPointerException
d) Compile-time error
View Answer
Answer:- d) Compile-time errorSame exception can’t be placed in multi-catch statements. It gives a compile time error:- Alternatives in a multi-catch statement cannot be related by subclassing.
Q10) What will be the output of the below program?
public class Test {
public static void main(String[] args) {
try {
System.out.println(9/0);
} catch(ArithmeticException | Exception e) {
e.printStackTrace();
}
}
}
a) ArithmeticException
b) NullPointerException
c) Both ArithmeticException & NullPointerException
d) Compile-time error
View Answer
Answer:- d) Compile-time errorIn multi-catch blocks there should not be any relation between exception types (either child to parent, parent to child or same type) otherwise we will get compile time errors. Above program gives compile time error:- Alternatives in a multi-catch statement cannot be related by subclassing.
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!