Home Subjects Java Programming

Java Programming

Java OOP, collections, multithreading

270 Q 10 Topics Take Test
Advertisement
Difficulty: All Easy Medium Hard 151–160 of 270
Topics in Java Programming
In Java exception handling, what is the order of execution in try-finally-catch block?
A try → catch → finally
B try → finally → catch
C catch → try → finally
D finally → try → catch
Correct Answer:  A. try → catch → finally
EXPLANATION

The correct order is try block executes first, then catch block (if exception occurs), and finally block always executes at the end.

Take Test
What is the output of this code?
public class Test {
public static void main(String[] args) {
try {
int[] arr = {1, 2};
System.out.println(arr[5]);
} catch (Exception e) {
System.out.println("Caught");
}
}
}
A Caught
B ArrayIndexOutOfBoundsException
C Compilation error
D No output
Correct Answer:  A. Caught
EXPLANATION

ArrayIndexOutOfBoundsException is caught by the generic Exception catch block, printing 'Caught'.

Take Test
Which exception is thrown when a string cannot be converted to a number?
A NumberFormatException
B ConversionException
C ParseException
D StringConversionException
Correct Answer:  A. NumberFormatException
EXPLANATION

NumberFormatException is thrown when attempting to convert a string to a numeric type but the string does not have an appropriate format. It's an unchecked exception.

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

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

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

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

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

Take Test
Q.159 Easy Multithreading
A developer needs to ensure that exactly 5 threads complete their tasks before proceeding to the next phase. Which synchronization utility is most appropriate?
A Semaphore
B CountDownLatch
C Phaser
D Mutex
Correct Answer:  B. CountDownLatch
EXPLANATION

CountDownLatch is designed for one-time barrier scenarios where threads wait for a countdown to reach zero. With an initial count of 5, it perfectly suits this use case. Semaphore is reusable, Phaser handles multiple phases, and Mutex is not a Java class.

Take Test
Q.160 Easy Multithreading
Which of the following methods will cause a thread to release all locks it holds while waiting?
A wait()
B sleep()
C yield()
D join()
Correct Answer:  A. wait()
EXPLANATION

The wait() method causes a thread to release all acquired locks and enter a waiting state until notify() or notifyAll() is called. sleep(), yield(), and join() do not release locks.

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