Exception Handling
11. Can a catch block exist without a try block?
-
A. Yes
-
B. No
No
No
12.What will be the output?
class Test {
public static void main(String[] args) {
try {
System.out.println("Try block executed");
} finally {
System.out.println("Finally block executed");
}
}
}
-
A. Try block executed
-
B. Try block executed Finally block executed
-
C. Compilation Error
-
D. No output
Try block executed Finally block executed
Try block executed Finally block executed
13.What happens if finally block contains return?
class Test {
public static void main(String[] args) {
System.out.println(testMethod());
}
static int testMethod() {
try {
return 10;
} finally {
return 20;
}
}
}
-
A. 10
-
B. 20
-
C. Compilation Error
-
D. Runtime Error
20
20
14.What happens if catch has a broader exception first?
class Test {
public static void main(String[] args) {
try {
int a = 5 / 0;
} catch (Exception e) {
System.out.println("General Exception caught");
} catch (ArithmeticException e) {
System.out.println("ArithmeticException caught");
}
}
}
-
A. Compilation Error
-
B. General Exception caught
-
C. ArithmeticException caught
-
D. No output
Compilation Error
Compilation Error
15.What will be the output?
class Test {
public static void main(String[] args) {
try {
System.exit(0);
} finally {
System.out.println("Finally block executed");
}
}
}
-
A. Finally block executed
-
B. No output
-
C. Compilation Error
-
D. Runtime Error
No output
No output