Home Subjects Java Programming

Java Programming

Java OOP, collections, multithreading

270 Q 10 Topics Take Test
Advertisement
Difficulty: All Easy Medium Hard 141–150 of 270
Topics in Java Programming
Consider a method signature: public void readFile() throws IOException. Which statement is correct?
A This method will definitely throw IOException
B This method may throw IOException that caller must handle
C IOException will never occur in this method
D The caller can ignore IOException
Correct Answer:  B. This method may throw IOException that caller must handle
EXPLANATION

throws declaration indicates the method may throw IOException. The caller is responsible for handling it with try-catch or declaring throws. The method doesn't always throw it.

Take Test
Which exception is thrown when attempting to access an index outside the bounds of an array?
A IndexOutOfRangeException
B ArrayIndexOutOfBoundsException
C ArrayIndexException
D OutOfBoundsException
Correct Answer:  B. ArrayIndexOutOfBoundsException
EXPLANATION

ArrayIndexOutOfBoundsException is thrown when attempting to access array elements with invalid indices. It's an unchecked exception extending RuntimeException.

Take Test
What happens when an unchecked exception is not caught?
A Compilation fails
B The exception propagates up the call stack until caught or program terminates
C The exception is automatically converted to checked exception
D The finally block is skipped
Correct Answer:  B. The exception propagates up the call stack until caught or program terminates
EXPLANATION

Unchecked exceptions (RuntimeException subclasses) don't require explicit handling. If not caught, they propagate up the call stack, eventually terminating the thread if not caught anywhere.

Take Test
Which exception is thrown when a method argument is null but shouldn't be?
A NullArgumentException
B NullPointerException
C InvalidArgumentException
D ArgumentNullException
Correct Answer:  B. NullPointerException
EXPLANATION

NullPointerException is thrown when attempting to use an object reference that hasn't been instantiated. Java doesn't have NullArgumentException.

Take Test
What is the difference between 'throw' and 'throws' keywords in Java?
A throw is used to declare exceptions, throws is used to throw them
B throw creates an exception object and throws it, throws declares that a method may throw exceptions
C They are synonyms and can be used interchangeably
D throws is used only with try-catch blocks
Correct Answer:  B. throw creates an exception object and throws it, throws declares that a method may throw exceptions
EXPLANATION

throw explicitly throws an exception object; throws declares in method signature that exceptions may be thrown. Example: throw new IOException() vs public void method() throws IOException.

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

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

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

Take Test
What will be the output?
public class Demo {
public static void main(String[] args) {
try {
System.out.println("A");
throw new Exception("Test");
System.out.println("B");
} catch (Exception e) {
System.out.println("C");
}
}
}
A A C B
B A C
C A B C
D A
Correct Answer:  B. A C
EXPLANATION

After 'A' is printed, exception is thrown. Statement 'B' is never executed. Catch block prints 'C'.

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

NumberFormatException is thrown by Integer.parseInt(), Double.parseDouble() when string format is invalid.

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