ExamVeda
Login
Home
21
In Java, can a method declare both "throws" and "throw" statements?
Discuss
Answer & Solution
Answer: Option C
No explanation is given for this question. Let's Discuss on Board
22
What is the result of the following code snippet?

try {
String str = "Java";
int num = Integer.parseInt(str);
} catch (NumberFormatException e) {
System.out.println("NumberFormatException!");
} catch (Exception e) {
System.out.println("Exception!");
} finally {
System.out.println("Finally Block!");
}
Discuss
Answer & Solution
Answer: Option B
No explanation is given for this question. Let's Discuss on Board
23
In Java, can a method declare "throws" for a checked exception that is not thrown in the method body?
Discuss
Answer & Solution
Answer: Option C
No explanation is given for this question. Let's Discuss on Board
24
What is the result of the following code snippet?

try {
throw new NullPointerException();
} catch (ArithmeticException e) {
System.out.println("Arithmetic Exception!");
} catch (NullPointerException e) {
System.out.println("NullPointerException!");
}
Discuss
Answer & Solution
Answer: Option A
Solution:
In the given code snippet, a NullPointerException is explicitly thrown within the try block. When an exception is thrown, the Java runtime looks for a catch block that matches the type of the thrown exception.

In this case, since a NullPointerException is thrown, the catch block specifically catching NullPointerException will be executed. Therefore, the output of the code will be "NullPointerException!".

Hence, Option A: NullPointerException! is the correct result of the code snippet.
25
In Java, can a "finally" block have a "return" statement?
Discuss
Answer & Solution
Answer: Option A
No explanation is given for this question. Let's Discuss on Board
26
What is the result of the following code snippet?

try {
int[] arr = new int[5];
int value = arr[10];
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Exception Caught!");
} finally {
System.out.println("Finally Block!");
}
Discuss
Answer & Solution
Answer: Option D
No explanation is given for this question. Let's Discuss on Board
27
In Java, can a "try" block have multiple "catch" blocks?
Discuss
Answer & Solution
Answer: Option C
No explanation is given for this question. Let's Discuss on Board
28
What is the result of the following code snippet?

try {
throw new ArithmeticException();
} catch (Exception e) {
System.out.println("Exception!");
} catch (ArithmeticException e) {
System.out.println("Arithmetic Exception!");
}
Discuss
Answer & Solution
Answer: Option B
No explanation is given for this question. Let's Discuss on Board
29
In Java, can a "catch" block catch exceptions of a subclass type before catching exceptions of a superclass type?
Discuss
Answer & Solution
Answer: Option C
No explanation is given for this question. Let's Discuss on Board
30
What is the result of the following code snippet?

try {
throw new RuntimeException();
} catch (RuntimeException e) {
System.out.println("Runtime Exception!");
} catch (Exception e) {
System.out.println("Exception!");
}
Discuss
Answer & Solution
Answer: Option D
No explanation is given for this question. Let's Discuss on Board