Welcome to the Java Exception Handling Coding Quiz. In this quiz, we present 10 coding MCQ questions to test your coding knowledge on the Java Exception Handling topic. Each question has a correct and brief explanation.
1. What is the result of executing this Java code snippet?
public class Test {
public static void main(String[] args) {
try {
int[] numbers = {1, 2, 3};
System.out.println(numbers[5]);
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Array index out of bounds!");
}
}
}
Answer:
Explanation:
The code tries to access an index that is out of bounds for the array numbers. This triggers the ArrayIndexOutOfBoundsException, which is caught and handled in the catch block.
2. What does this Java code snippet output?
public class Test {
public static void main(String[] args) {
try {
int x = 0;
int y = 5 / x;
} catch (Exception e) {
System.out.println("Exception occurred");
}
}
}
Answer:
Explanation:
Dividing by zero throws an ArithmeticException, which is caught by the catch block since it catches all Exception types.
3. Identify the output of the following code:
public class Test {
public static void main(String[] args) {
try {
badMethod();
System.out.println("A");
} catch (Exception ex) {
System.out.println("B");
} finally {
System.out.println("C");
}
System.out.println("D");
}
public static void badMethod() {
throw new Error();
}
}
Answer:
Explanation:
The given code snippet throws an Error from the badMethod() method. It's important to note the difference between Error and Exception in Java:
Error represents serious problems that a reasonable application should not try to catch, while Exception is a condition that your application might want to catch.
Here's the flow of the code based on its behavior:
1. The main method calls badMethod(), which immediately throws an Error.
2. Because the thrown object is an Error and not an Exception, the catch block designed to catch an Exception will not catch this Error.
3. The finally block executes after the try block exits (whether the try block exits normally or abruptly due to an exception or error). So, "C" is printed.
4. After executing the finally block, since the Error is not caught by the catch block, it propagates up the call stack, and the rest of the code in the main method after the try-catch-finally block (i.e., System.out.println("D");) does not execute.
5. The program terminates abnormally due to the uncaught Error.
Therefore, the correct output is: C
4. What will be printed by this Java code?
public class Test {
public static void main(String[] args) {
try {
System.out.println("Hello, world!");
} finally {
System.out.println("Finally executing...");
}
}
}
Answer:
Explanation:
The try block is executed normally, and then the finally block is executed, resulting in both statements being printed.
5. What does this code snippet output?
public class Test {
public static void main(String[] args) {
try {
String s = null;
System.out.println(s.length());
} catch (NullPointerException e) {
System.out.println("Caught NullPointerException");
} catch (Exception e) {
System.out.println("Caught Exception");
}
}
}
Answer:
Explanation:
Accessing the length of a null string throws a NullPointerException, which is caught by the first catch block.
6. What is the result of executing this code?
public class Test {
public static void main(String[] args) {
try {
int[] arr = new int[-5];
} catch (NegativeArraySizeException e) {
System.out.println("Negative array size");
}
}
}
Answer:
Explanation:
Trying to create an array with a negative size throws a NegativeArraySizeException, which is caught and handled.
7. What will the following Java code snippet output?
public class Test {
public static void main(String[] args) {
try {
throw new RuntimeException();
} catch (RuntimeException e) {
System.out.println("RuntimeException caught");
} catch (Exception e) {
System.out.println("Exception caught");
}
}
}
Answer:
Explanation:
The RuntimeException is caught by the first catch block that matches its type.
8. What does the following code snippet print?
public class Test {
public static void main(String[] args) {
String str = null;
try {
System.out.println(str.length());
} catch (NullPointerException e) {
System.out.println("Null Pointer Exception");
} finally {
System.out.println("Finally block executed");
}
}
}
Answer:
Explanation:
A NullPointerException is thrown and caught, then the finally block executes, resulting in both messages being printed.
9. Determine the output of this Java code:
class MyException extends Exception {}
public class Test {
public static void main(String[] args) {
try {
throw new MyException();
} catch (MyException e) {
System.out.println("Custom exception caught");
}
}
}
Answer:
Explanation:
MyException is a custom exception class. It's thrown and immediately caught in the catch block.
10. What is the result of the following code snippet?
public class Test {
public static void main(String[] args) {
try {
riskyMethod();
} catch (Exception e) {
System.out.println("Exception caught");
}
}
static void riskyMethod() throws Exception {
throw new Exception("Problem");
}
}
Answer:
Explanation:
riskyMethod throws an Exception with the message "Problem", which is caught by the catch block in the main method.
Comments
Post a Comment
Leave Comment