Home Subjects Java Programming

Java Programming

Java OOP, collections, multithreading

958 Q 10 Topics Take Test
Advertisement
Difficulty: All Easy Medium Hard 481–490 of 958
Topics in Java Programming
What is the correct syntax for declaring a method that throws a checked exception?
A public void method() throws IOException {}
B public void method() catch IOException {}
C public void method() try IOException {}
D public void method() exceptions IOException {}
Correct Answer:  A. public void method() throws IOException {}
EXPLANATION

The 'throws' keyword is used in method declaration to indicate that the method might throw checked exceptions. Syntax: method() throws ExceptionType {}

Test
Which exception is thrown when accessing an array element with an invalid index?
A IndexOutOfBoundsException
B ArrayIndexOutOfBoundsException
C ArrayAccessException
D IndexException
Correct Answer:  B. ArrayIndexOutOfBoundsException
EXPLANATION

ArrayIndexOutOfBoundsException is thrown when trying to access an array element with an index that is out of the valid range (0 to length-1).

Test
Q.483 Medium Exception Handling
What will be the output?
try {
throw new Exception("Test");
} catch(Exception e) {
System.out.println("Caught");
throw new RuntimeException("New");
} finally {
System.out.println("Finally");
}
A Caught Finally followed by RuntimeException
B Caught RuntimeException Finally
C Finally Caught
D RuntimeException Caught Finally
Correct Answer:  A. Caught Finally followed by RuntimeException
EXPLANATION

The finally block executes after catch block. 'Caught' is printed, then finally block prints 'Finally', and then RuntimeException is thrown.

Test
Q.484 Medium Exception Handling
In multi-catch block (Java 7+), which operator is used to catch multiple exceptions in a single catch clause?
A &&
B ||
C |
D &
Correct Answer:  C. |
EXPLANATION

Java 7 introduced multi-catch using the pipe (|) operator: catch(IOException | SQLException e). This allows handling multiple exception types in one block.

Test
What is the output of the following code?
int x = 10;
try {
x = x / 0;
} catch (ArithmeticException e) {
x = 20;
} finally {
x = 30;
}
System.out.println(x);
A 10
B 20
C 30
D Error: compilation fails
Correct Answer:  C. 30
EXPLANATION

The finally block always executes last and sets x to 30. The output will be 30 because finally block assignments override catch block assignments.

Test
In Java exception handling, which of the following statements is true about the finally block?
A finally block executes only if an exception is thrown
B finally block executes regardless of whether an exception occurs or not
C finally block executes only if no exception is thrown
D finally block executes before the catch block
Correct Answer:  B. finally block executes regardless of whether an exception occurs or not
EXPLANATION

The finally block in Java always executes whether an exception is caught or not, except when System.exit() is called or JVM terminates abnormally.

Test
Q.487 Medium Exception Handling
Which interface must a class implement to work with try-with-resources?
A Closeable
B AutoCloseable
C Serializable
D Both A and B
Correct Answer:  D. Both A and B
EXPLANATION

AutoCloseable (Java 7+) is the primary interface for try-with-resources. Closeable extends AutoCloseable. Both work with try-with-resources.

Test
What happens when System.exit() is called inside try block?
A finally block still executes
B finally block does not execute
C catch block executes first
D Compilation error occurs
Correct Answer:  B. finally block does not execute
EXPLANATION

System.exit() terminates the JVM immediately. The finally block does not execute as the program terminates before reaching it.

Test
Q.489 Medium Exception Handling
What is the correct way to create a custom exception with cause chaining?
A public MyException(String msg) throws Throwable {}
B public MyException(String msg, Throwable cause) { super(msg, cause); }
C public MyException(Throwable cause) { this.cause = cause; }
D Custom exceptions cannot support cause chaining
Correct Answer:  B. public MyException(String msg, Throwable cause) { super(msg, cause); }
EXPLANATION

Proper cause chaining calls super(msg, cause) in constructor, leveraging Exception's constructor that accepts both message and cause.

Test
Which of the following scenarios will NOT trigger a StackOverflowError?
A Infinite recursive function calls without base case
B Very deep try-catch nesting (theoretically)
C Mutual recursion between multiple methods
D Creating too many local variables in a method
Correct Answer:  D. Creating too many local variables in a method
EXPLANATION

StackOverflowError occurs from stack overflow due to excessive method calls. Local variables contribute to stack but won't cause overflow like recursion.

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