Home Subjects Java Programming

Java Programming

Java OOP, collections, multithreading

958 Q 10 Topics Take Test
Advertisement
Difficulty: All Easy Medium Hard 491–500 of 958
Topics in Java Programming
What will be printed for this code?
public class ExceptionOrder {
public static void main(String[] args) {
try {
try {
throw new RuntimeException("Inner");
} catch (Exception e) {
throw new IOException("Outer");
}
} catch (IOException e) {
System.out.println("Caught IOException");
} catch (RuntimeException e) {
System.out.println("Caught RuntimeException");
}
}
}
A Caught IOException
B Caught RuntimeException
C Compilation error
D No output
Correct Answer:  C. Compilation error
EXPLANATION

IOException is a checked exception and must be declared in method signature or caught. The inner catch throws IOException which isn't caught immediately.

Test
Q.492 Medium Exception Handling
Consider code that implements auto-closeable resources. What is the advantage of try-with-resources statement?
A Automatic resource closure in order of declaration (LIFO)
B No need to call close() explicitly
C Suppressed exceptions are automatically added
D All of the above
Correct Answer:  D. All of the above
EXPLANATION

Try-with-resources (Java 7+) automatically closes resources, handles suppressed exceptions, and closes in LIFO order without explicit close() calls.

Test
Which of the following is NOT a subclass of Throwable?
A Exception
B Error
C RuntimeException
D Throwable itself
Correct Answer:  D. Throwable itself
EXPLANATION

Throwable is the superclass. Exception and Error are direct subclasses, and RuntimeException is a subclass of Exception.

Test
Which method is used to print the stack trace of an exception?
A printStackTrace()
B getStackTrace()
C printStack()
D Both A and B are correct
Correct Answer:  D. Both A and B are correct
EXPLANATION

printStackTrace() prints to System.err, while getStackTrace() returns an array of StackTraceElement objects. Both exist and serve different purposes.

Test
Q.495 Medium Exception Handling
What will be the result of executing this code?
public class MultiCatchTest {
public static void main(String[] args) {
try {
int[] arr = {1, 2};
int x = arr[5];
} catch (ArrayIndexOutOfBoundsException | NullPointerException e) {
System.out.println("Array or Null Error");
}
}
}
A Array or Null Error
B Compilation error - cannot use | in catch
C ArrayIndexOutOfBoundsException thrown
D No output - exception not caught
Correct Answer:  A. Array or Null Error
EXPLANATION

Multi-catch feature (Java 7+) allows handling multiple exceptions. ArrayIndexOutOfBoundsException is caught by the multi-catch block.

Test
Q.496 Medium Exception Handling
Which of the following best describes a custom exception in Java?
A Must extend RuntimeException directly
B Can extend Exception or RuntimeException based on whether it's checked or unchecked
C Must implement Serializable interface
D Cannot have any fields or methods
Correct Answer:  B. Can extend Exception or RuntimeException based on whether it's checked or unchecked
EXPLANATION

Custom exceptions should extend Exception (for checked) or RuntimeException (for unchecked) depending on usage requirements.

Test
Q.497 Medium Exception Handling
What is the output of multi-level exception chaining?
public class ChainTest {
public static void main(String[] args) {
try {
try {
throw new IOException("IO Error");
} catch (IOException e) {
throw new RuntimeException("Runtime Error", e);
}
} catch (RuntimeException e) {
System.out.println(e.getCause().getClass().getSimpleName());
}
}
}
A RuntimeException
B IOException
C Throwable
D Exception
Correct Answer:  B. IOException
EXPLANATION

getCause() returns the IOException that was wrapped. getClass().getSimpleName() returns 'IOException'.

Test
Q.498 Medium Exception Handling
Which of the following statements is true regarding exception constructors?
A Exception class has only no-argument constructor
B Exception class has constructor that accepts String message and Throwable cause
C Custom exceptions must override toString() method
D Exception constructors cannot call super() implicitly
Correct Answer:  B. Exception class has constructor that accepts String message and Throwable cause
EXPLANATION

Exception class provides multiple constructors including one with String message and another with String message and Throwable cause for exception chaining.

Test
Q.499 Medium Exception Handling
Consider the following code. What will be printed?
public class FinallyTest {
public static int getValue() {
try {
return 10;
} finally {
System.out.println("Finally");
}
}
public static void main(String[] args) {
System.out.println(getValue());
}
}
A Finally 10
B 10 Finally
C Finally
D 10
Correct Answer:  A. Finally 10
EXPLANATION

finally block executes before the return statement completes. System.out.println in finally executes first, then the return value 10 is printed.

Test
Q.500 Medium Exception Handling
Which exception is thrown when a thread tries to acquire a monitor that is already locked?
A InterruptedException
B IllegalMonitorStateException
C ThreadStateException
D Threads never throw exceptions during synchronization
Correct Answer:  B. IllegalMonitorStateException
EXPLANATION

IllegalMonitorStateException is thrown when an invalid monitor state operation occurs, such as calling notify() on an object not owned by the current thread.

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