Home Subjects Java Programming

Java Programming

Java OOP, collections, multithreading

958 Q 10 Topics Take Test
Advertisement
Difficulty: All Easy Medium Hard 471–480 of 958
Topics in Java Programming
What will this code output?
int result = 0;
try {
result = 10 / 0;
} catch(Exception e) {
result = 20;
return result;
} finally {
result = 30;
}
System.out.println(result);
A 20
B 30
C 10
D Compilation error
Correct Answer:  A. 20
EXPLANATION

Return statement in catch block prepares the return value (20). Finally block executes but return happens after finally, so 20 is returned even though finally sets result to 30.

Test
Consider a scenario where multiple catch blocks are written. What is the correct order?
I. catch(FileNotFoundException e)
II. catch(IOException e)
III. catch(Exception e)
A I, II, III
B III, II, I
C II, I, III
D I, III, II
Correct Answer:  A. I, II, III
EXPLANATION

Catch blocks should be ordered from most specific to most general exception types. FileNotFoundException is more specific than IOException, which is more specific than Exception.

Test
Which exception is the parent class of all checked exceptions in Java (excluding RuntimeException)?
A Throwable
B Exception
C Error
D Checked
Correct Answer:  B. Exception
EXPLANATION

Exception class is the parent of all checked exceptions. RuntimeException extends Exception but is unchecked. Error and Throwable are higher in hierarchy.

Test
Q.474 Medium Exception Handling
What will be the output of this code?
try {
try {
throw new Exception("Inner");
} catch(Exception e) {
throw new RuntimeException("Outer");
}
} catch(RuntimeException e) {
System.out.println("Caught: " + e.getMessage());
}
A Caught: Inner
B Caught: Outer
C Compilation error
D No output
Correct Answer:  B. Caught: Outer
EXPLANATION

The inner try catches Exception and throws RuntimeException. The outer catch catches RuntimeException and prints 'Caught: Outer'.

Test
Q.475 Medium Exception Handling
Which of the following is true about try-with-resources (Java 7+)?
A It automatically closes resources that implement Closeable or AutoCloseable
B It eliminates the need for finally blocks completely
C It can only be used with file streams
D It requires explicit close() method calls
Correct Answer:  A. It automatically closes resources that implement Closeable or AutoCloseable
EXPLANATION

Try-with-resources automatically invokes close() on resources implementing AutoCloseable/Closeable. Syntax: try(Resource r = new Resource()) {...}

Test
Q.476 Medium Exception Handling
What will be printed when this code executes?
String str = "Java";
try {
int x = Integer.parseInt(str);
} catch(NumberFormatException e) {
System.out.println("Invalid number");
} catch(Exception e) {
System.out.println("General exception");
}
A Invalid number
B General exception
C Compilation error
D No output
Correct Answer:  A. Invalid number
EXPLANATION

Integer.parseInt("Java") throws NumberFormatException, which is caught by the first catch block. More specific exceptions should be caught before general ones.

Test
Q.477 Medium Exception Handling
In Java, which method of Throwable class is used to obtain the cause of an exception?
A getReason()
B getCause()
C getException()
D getError()
Correct Answer:  B. getCause()
EXPLANATION

The getCause() method returns the cause (underlying Throwable) of this Throwable. It's useful for exception chaining.

Test
Q.478 Medium Exception Handling
Which of the following will NOT result in a NullPointerException?
A String s = null; System.out.println(s.length());
B Object obj = null; obj.toString();
C Integer i = null; int x = i;
D String s = null; if(s == null) { System.out.println("Null"); }
Correct Answer:  D. String s = null; if(s == null) { System.out.println("Null"); }
EXPLANATION

Comparing with null using == operator doesn't throw NPE. The other options call methods or unbox null values, which causes NullPointerException.

Test
What will happen when the following code is executed?
try {
int[] arr = {1, 2, 3};
System.out.println(arr[5]);
} catch(ArrayIndexOutOfBoundsException e) {
System.out.println("Exception caught");
}
A Compilation error
B Runtime error without output
C Exception caught
D No output
Correct Answer:  C. Exception caught
EXPLANATION

Accessing arr[5] when array has only 3 elements throws ArrayIndexOutOfBoundsException which is caught and 'Exception caught' is printed.

Test
Q.480 Medium Exception Handling
Consider the following exception hierarchy. Which statement will compile without error?
class MyException extends Exception {}
class MyRuntimeException extends RuntimeException {}
A throw new MyException();
B int x = 10; x = x / 0; // No need to handle MyException
C Method m() throws MyRuntimeException { return null; }
D new MyException(); // Valid statement without try-catch
Correct Answer:  C. Method m() throws MyRuntimeException { return null; }
EXPLANATION

RuntimeException and its subclasses are unchecked, so they don't need to be caught or declared. Option A requires try-catch since MyException is checked.

Test
IGET
iget AI
Online · Ask anything about exams
Hi! 👋 I'm your iget AI assistant.

Ask me anything about exam prep, MCQ solutions, study tips, or strategies! 🎯
UPSC strategy SSC CGL syllabus Improve aptitude NEET Biology tips