In this post, we have provided Java Exception Handling multiple-choice questions to test your knowledge about exception handling in Java.
Learn Java exception handling at https://www.javaguides.net/p/java-exception-handling-tutorial.html
We would suggest you, try these code snippets in eclipse IDE and understand how the program works (However, the answer with the explanation given at end of this post). These questions may ask in interviews or similar questions may appear in interviews so prepare yourself.
The answer and explanation of each question have given at end of this post.
Q1 - Which exception will the following throw?
public class Test {
public static void main(String[] args) {
Object obj = new Integer(3);
String str = (String) obj;
System.out.println(str);
}
}
A. ArrayIndexOutOfBoundsException
B. ClassCastException
C. IllegalArgumentException
D. NumberFormatException
E. None of the above.
Q2 - Which of the following are checked exceptions? (Choose all that apply)
A. Exception
B. IllegalArgumentException
C. IOException
D. NullPointerException
E. NumberFormatException
F. StackOverflowError
Q3 - What is the output of the following Java program?
public class Test { public static void main(String[] args) { int x = 0; int y = 1; int z = y / x; } }
A. Compiler Error
B. Compiles and runs fine
C. Compiles fine but throws ArithmeticException exception
D. None of above
Q4 - Which of the following exceptions are thrown by the JVM? (Choose all that apply)
A. ArrayIndexOutOfBoundsException
B. ExceptionInInitializerError
C. java.io.IOException
D. NullPointerException
E. NumberFormatException
Q5 - What is the output of the following program?
public class Laptop {
public void start() {
try {
System.out.print("Starting up ");
throw new Exception();
} catch (Exception e) {
System.out.print("Problem ");
System.exit(0);
} finally {
System.out.print("Shutting down ");
}
}
public static void main(String[] args) {
new Laptop().start();
}
}
A. Starting up
B. Starting up Problem
C. Starting up Problem Shutting down
D. Starting up Shutting down
E. The code does not compile.
F. An uncaught exception is thrown
Answers
Q1
B. ClassCastException
Q2
A and C
Q3
C. Compiles fine but throws ArithmeticException exception
Exception in thread "main" java.lang.ArithmeticException: / by zero
at Test.main(Test.java:6)
Q4
A, B, D
Q5
B. Starting up Problem
Related Posts
- Java String Quiz
- Java Arrays Quiz
- Java Loops Quiz
- Java OOPS Quiz
- Java OOPS Quiz - Part 1
- Java OOPS Quiz - Part 2
- Java Exception Handling Quiz
- Java Collections Quiz
- Java Generics Quiz
- JDBC Quiz
- Java Lambda Expressions Quiz
- Java Functional Interfaces Quiz
- Java Streams API Quiz
- Java Date Time Quiz
- Java 8 Quiz
Free Spring Boot Tutorial | Full In-depth Course | Learn Spring Boot in 10 Hours
Watch this course on YouTube at Spring Boot Tutorial | Fee 10 Hours Full Course