Home Subjects Java Programming

Java Programming

Java OOP, collections, multithreading

958 Q 10 Topics Take Test
Advertisement
Difficulty: All Easy Medium Hard 551–560 of 958
Topics in Java Programming
Which exception is thrown when trying to access a method of a null object?
A NullAccessException
B NullPointerException
C NullReferenceException
D EmptyObjectException
Correct Answer:  B. NullPointerException
EXPLANATION

NullPointerException is thrown when attempting to call a method or access a property on a null object reference in Java.

Test
Q.552 Medium Exception Handling
What is the output of multiple catch blocks?
try {
int[] arr = {1, 2, 3};
System.out.println(arr[5]);
} catch(ArrayIndexOutOfBoundsException e) {
System.out.println("Index");
} catch(Exception e) {
System.out.println("Exception");
}
A Index
B Exception
C Both Index and Exception
D No output
Correct Answer:  A. Index
EXPLANATION

ArrayIndexOutOfBoundsException is thrown and caught by the first catch block. The second catch block is not executed. Only the most specific matching catch block executes.

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

Runnable is a functional interface, not related to exception hierarchy. Error and Exception are direct subclasses of Throwable. RuntimeException is a subclass of Exception.

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

The exception is caught and prints "Caught". The finally block always executes after try-catch, printing "Finally". Output: Caught Finally

Test
Can you have a try block without a catch block in Java?
A No, catch block is mandatory
B Yes, if finally block is present
C Yes, if return statement is present
D No, you need at least one catch or finally
Correct Answer:  B. Yes, if finally block is present
EXPLANATION

A try block must be followed by either a catch block or a finally block (or both). A try block alone is not valid syntax.

Test
Which exception is thrown when a method receives an illegal or inappropriate argument?
A IllegalStateException
B IllegalArgumentException
C InvalidParameterException
D ParameterException
Correct Answer:  B. IllegalArgumentException
EXPLANATION

IllegalArgumentException is thrown to indicate that a method has been passed an illegal or inappropriate argument. It is an unchecked exception.

Test
Q.557 Medium Exception Handling
What is the output of the following code?
int x = 10;
try {
x = x / 0;
} catch(ArithmeticException e) {
x = x + 5;
} finally {
x = x * 2;
}
System.out.println(x);
A 10
B 30
C 20
D 40
Correct Answer:  B. 30
EXPLANATION

ArithmeticException is caught, x becomes 10+5=15. Finally block executes, x becomes 15*2=30. Finally block always executes regardless of exception.

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

IOException is a checked exception that must be caught or declared in the method signature. NullPointerException, ArrayIndexOutOfBoundsException, and ArithmeticException are unchecked exceptions.

Test
Q.559 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.

Test
Q.560 Hard Multithreading
Consider a ThreadLocal variable initialized in a thread pool executor with 10 threads. If the same thread is reused from the pool for a different task, what is the state of its ThreadLocal variable?
A It is automatically reset to its initial value
B It retains the value from the previous task execution
C It becomes null
D It throws ThreadLocalException
Correct Answer:  B. It retains the value from the previous task execution
EXPLANATION

ThreadLocal values persist across task executions in the same thread. ThreadPools reuse threads, so previous ThreadLocal values remain unless explicitly removed. This can cause data leakage. Developers must call remove() to clean up.

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