Exception handling is an important aspect of Java programming that allows you to handle and manage unexpected runtime errors. In this blog post, we present a Java Exception Handling Quiz comprising 10+ multiple-choice questions (MCQ). This quiz aims to assess your understanding of exception handling in Java, including the basics of exceptions, try-catch blocks, and exception propagation. Let's put your knowledge of Java exception handling to the test!.
Learn and Master Java Programming: Learn Java Programming with Examples
Learn everything about Java 8 features: Java 8 Tutorial and Examples
Check out 100+ quiz questions: 100+ Quiz Questions to Test Your Java, Spring Boot, Microservices, Hibernate, REST API Skills
1. What is an exception in Java?
Answer:
b) An unexpected event that occurs during program execution
Explanation:
2. What is the purpose of exception handling in Java?
Answer:
c) To gracefully handle and recover from exceptions
Explanation:
3. Which keyword is used to throw an exception explicitly in Java?
Answer:
a) throw
Explanation:
4. What is the role of the finally block in a try-catch-finally statement?
Answer:
b) To specify the code that is always executed, regardless of whether an exception occurs or not
Explanation:
5. What is exception propagation in Java?
Answer:
c) The process of passing an exception from one method to its calling method
Explanation:
6. 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);
}
}
B. ClassCastException
C. IllegalArgumentException
D. NumberFormatException
E. None of the above.
Answer:
B. ClassCastExceptionExplanation:
The second line tries to cast an Integer to a String. Since String does not extend Integer, this is not allowed and a ClassCastException is thrown.
7. Which of the following are checked exceptions? (Choose all that apply)
A. ExceptionB. IllegalArgumentException
C. IOException
D. NullPointerException
E. NumberFormatException
F. StackOverflowError
Answer:
A and CExplanation:
8. 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; } }
C. Compiles fine but throws ArithmeticException exception
D. None of above
Answer:
C. Compiles fine but throws ArithmeticException exceptionExplanation:
Exception in thread "main" java.lang.ArithmeticException: / by zero
at Test.main(Test.java:6)
9. Which of the following exceptions are thrown by the JVM? (Choose all that apply)
A. ArrayIndexOutOfBoundsExceptionB. ExceptionInInitializerError
C. java.io.IOException
D. NullPointerException
E. NumberFormatException
Answer:
A, B, DExplanation:
10. 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();
}
}
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
Answer:
B. Starting up ProblemExplanation:
Conclusion
Learn and Master Java Programming: Learn Java Programming with Examples
Learn everything about Java 8 features: Java 8 Tutorial and Examples
Check out 100+ quiz questions: 100+ Quiz Questions to Test Your Java, Spring Boot, Microservices, Hibernate, REST API Skills
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
Comments
Post a Comment
Leave Comment