Home Subjects Java Programming

Java Programming

Java OOP, collections, multithreading

958 Q 10 Topics Take Test
Advertisement
Difficulty: All Easy Medium Hard 521–530 of 958
Topics in Java Programming
In Java exception handling, what is the purpose of the finally block?
A To catch exceptions that were not caught in catch blocks
B To execute code regardless of whether an exception occurs or not
C To declare new exceptions
D To rethrow exceptions
Correct Answer:  B. To execute code regardless of whether an exception occurs or not
EXPLANATION

The finally block executes unconditionally after try and/or catch blocks complete, making it ideal for resource cleanup. It runs even if try or catch blocks return.

Test
What is the correct syntax for declaring multiple exceptions in a method signature?
A public void method() throws IOException, SQLException { }
B public void method() throws (IOException, SQLException) { }
C public void method() throws IOException | SQLException { }
D public void method() throw IOException, SQLException { }
Correct Answer:  A. public void method() throws IOException, SQLException { }
EXPLANATION

Multiple exceptions are declared using comma-separated syntax in the throws clause. Parentheses and pipe operators are not valid syntax for this purpose.

Test
Which of the following exceptions is a checked exception in Java?
A NullPointerException
B SQLException
C ArrayIndexOutOfBoundsException
D ArithmeticException
Correct Answer:  B. SQLException
EXPLANATION

SQLException is a checked exception that must be caught or declared. NullPointerException, ArrayIndexOutOfBoundsException, and ArithmeticException are unchecked exceptions (RuntimeException subclasses).

Test
Q.524 Medium Exception Handling
Which statement is TRUE about exception propagation in Java?
A Exceptions propagate only upward in call stack
B Exceptions propagate downward in call stack
C Exceptions don't propagate
D Exceptions propagate both ways
Correct Answer:  A. Exceptions propagate only upward in call stack
EXPLANATION

When an exception is thrown and not caught, it propagates up the call stack until a matching catch block is found or the program terminates.

Test
What will be printed?
public class ExceptionTest {
public static void main(String[] args) {
try {
testMethod();
} catch (Exception e) {
System.out.println("Caught");
}
}

public static void testMethod() {
try {
throw new RuntimeException("Error");
} finally {
System.out.println("Finally");
}
}
}
A Finally Caught
B Caught Finally
C Finally
D Caught
Correct Answer:  A. Finally Caught
EXPLANATION

Finally block executes first (prints 'Finally'), then the exception propagates to outer catch block which prints 'Caught'.

Test
Q.526 Medium Exception Handling
Which of the following cannot be used with try-with-resources?
A FileInputStream
B BufferedReader
C String
D Scanner
Correct Answer:  C. String
EXPLANATION

Try-with-resources requires resources to implement AutoCloseable interface. String doesn't implement it, so it cannot be used.

Test
Q.527 Medium Exception Handling
Consider this code:
public static void main(String[] args) {
try {
System.out.println("A");
return;
} finally {
System.out.println("B");
}
}
What is the output?
A A B
B A
C B A
D B
Correct Answer:  A. A B
EXPLANATION

Even with return in try block, finally block executes. So 'A' prints first, then 'B' before returning.

Test
Q.528 Medium Exception Handling
What is the difference between throw and throws?
A throw creates exception, throws declares it
B throws creates exception, throw declares it
C Both are same
D throw is for checked, throws for unchecked
Correct Answer:  A. throw creates exception, throws declares it
EXPLANATION

'throw' keyword actually throws an exception object. 'throws' keyword declares that a method throws a checked exception.

Test
Q.529 Medium Exception Handling
Analyze the code:
try {
// code
} catch (IOException | SQLException e) {
// handle
}
What is this syntax called?
A Multi-catch or union catch
B Chained exception handling
C Nested exception handling
D Parallel exception handling
Correct Answer:  A. Multi-catch or union catch
EXPLANATION

This is called multi-catch or union catch syntax (Java 7+) allowing multiple exception types to be caught in one block using pipe operator.

Test
Q.530 Medium Exception Handling
What will happen if a finally block contains a return statement?
A It will override any return from try or catch block
B It will cause a compilation error
C It will be ignored
D It will execute but not return
Correct Answer:  A. It will override any return from try or catch block
EXPLANATION

Return in finally block overrides returns in try or catch blocks. The finally return value is what gets returned.

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