Home Subjects Java Programming

Java Programming

Java OOP, collections, multithreading

212 Q 10 Topics Take Test
Advertisement
Difficulty: All Easy Medium Hard 111–120 of 212
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.

Take Test
What is suppressed exception in Java (introduced in Java 7)?
A An exception that occurs but is hidden from the programmer
B Exceptions thrown during resource closure in try-with-resources are added to suppressed list of primary exception
C An exception that doesn't need to be caught
D An exception that only occurs in finally blocks
Correct Answer:  B. Exceptions thrown during resource closure in try-with-resources are added to suppressed list of primary exception
EXPLANATION

In try-with-resources, if an exception occurs in try and another in resource close(), the second is added as suppressed exception to the first using addSuppressed(), preserving all exception information.

Take Test
What is the execution order in try-catch-finally for nested exception handling?
A Inner try → Inner catch → Inner finally → Outer try → Outer catch → Outer finally
B Outer try → Outer catch → Inner try → Inner catch → Inner finally → Outer finally
C All finally blocks execute before any catch blocks
D Execution order depends on compiler optimization
Correct Answer:  B. Outer try → Outer catch → Inner try → Inner catch → Inner finally → Outer finally
EXPLANATION

Nested exception handling follows inner-to-outer order. Inner try-catch-finally completes fully, then control passes to outer blocks. Finally blocks execute in their respective scope order.

Take Test
Which of the following cannot throw a checked exception without being declared in throws clause?
A Constructor
B Instance initializer block
C Static initializer block
D All of the above
Correct Answer:  D. All of the above
EXPLANATION

Checked exceptions in constructors, instance initializers, and static initializers must be handled with try-catch or declared in throws (constructors only). This is enforced by Java compiler.

Take Test
Which of the following is NOT a characteristic of Exception class in Java?
A It extends Throwable
B It is always checked exception
C Its subclasses may be checked or unchecked
D It can be instantiated directly
Correct Answer:  B. It is always checked exception
EXPLANATION

Exception class has both checked subclasses (like IOException) and unchecked subclasses (like RuntimeException). Not all exceptions extending Exception are checked.

Take 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'.

Take Test
Analyze the nested try-catch:
try {
try {
int x = 5/0;
} catch(NullPointerException e) {
System.out.println("Inner");
}
} catch(ArithmeticException e) {
System.out.println("Outer");
}
A Inner
B Outer
C Inner Outer
D Neither prints
Correct Answer:  B. Outer
EXPLANATION

ArithmeticException is thrown by 5/0. Inner catch only catches NullPointerException, so it propagates to outer catch which handles ArithmeticException. Output: Outer

Take Test
What happens if an exception is thrown in the finally block?
A It suppresses the original exception
B It is ignored
C It replaces the original exception
D It causes a compilation error
Correct Answer:  C. It replaces the original exception
EXPLANATION

If an exception occurs in the finally block, it will replace any exception thrown in the try or catch block, and the new exception will be propagated.

Take Test
Analyze the code:
try {
return 5;
} finally {
return 10;
}
What will be returned?
A 5
B 10
C Error
D Null
Correct Answer:  B. 10
EXPLANATION

The finally block always executes, and if it contains a return statement, it overrides the return value from try or catch block. So 10 is returned.

Take Test
Q.120 Hard Multithreading
A high-frequency trading system uses AtomicInteger for concurrent counter updates. However, performance degrades as more threads access the same counter. What is the primary cause and best solution?
A False sharing due to cache line contention; use LongAdder instead
B Integer overflow; switch to AtomicLong
C Insufficient synchronization; add synchronized keyword
D Thread starvation; increase thread priority
Correct Answer:  A. False sharing due to cache line contention; use LongAdder instead
EXPLANATION

AtomicInteger suffers from false sharing when multiple threads frequently update adjacent memory locations on the same CPU cache line. LongAdder uses striped updates across multiple cells, reducing contention. This is a 2024-25 performance optimization best practice for high-concurrency scenarios.

Take 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