Home Subjects Java Programming

Java Programming

Java OOP, collections, multithreading

270 Q 10 Topics Take Test
Advertisement
Difficulty: All Easy Medium Hard 131–140 of 270
Topics in Java Programming
What will happen when the following code is executed?
try {
int[] arr = {1, 2, 3};
System.out.println(arr[5]);
} catch(ArrayIndexOutOfBoundsException e) {
System.out.println("Exception caught");
}
A Compilation error
B Runtime error without output
C Exception caught
D No output
Correct Answer:  C. Exception caught
EXPLANATION

Accessing arr[5] when array has only 3 elements throws ArrayIndexOutOfBoundsException which is caught and 'Exception caught' is printed.

Take Test
What is the correct syntax for declaring a method that throws a checked exception?
A public void method() throws IOException {}
B public void method() catch IOException {}
C public void method() try IOException {}
D public void method() exceptions IOException {}
Correct Answer:  A. public void method() throws IOException {}
EXPLANATION

The 'throws' keyword is used in method declaration to indicate that the method might throw checked exceptions. Syntax: method() throws ExceptionType {}

Take Test
Which exception is thrown when accessing an array element with an invalid index?
A IndexOutOfBoundsException
B ArrayIndexOutOfBoundsException
C ArrayAccessException
D IndexException
Correct Answer:  B. ArrayIndexOutOfBoundsException
EXPLANATION

ArrayIndexOutOfBoundsException is thrown when trying to access an array element with an index that is out of the valid range (0 to length-1).

Take Test
What is the output of the following code?
int x = 10;
try {
x = x / 0;
} catch (ArithmeticException e) {
x = 20;
} finally {
x = 30;
}
System.out.println(x);
A 10
B 20
C 30
D Error: compilation fails
Correct Answer:  C. 30
EXPLANATION

The finally block always executes last and sets x to 30. The output will be 30 because finally block assignments override catch block assignments.

Take Test
In Java exception handling, which of the following statements is true about the finally block?
A finally block executes only if an exception is thrown
B finally block executes regardless of whether an exception occurs or not
C finally block executes only if no exception is thrown
D finally block executes before the catch block
Correct Answer:  B. finally block executes regardless of whether an exception occurs or not
EXPLANATION

The finally block in Java always executes whether an exception is caught or not, except when System.exit() is called or JVM terminates abnormally.

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

Throwable is the superclass. Exception and Error are direct subclasses, and RuntimeException is a subclass of Exception.

Take Test
Which method is used to print the stack trace of an exception?
A printStackTrace()
B getStackTrace()
C printStack()
D Both A and B are correct
Correct Answer:  D. Both A and B are correct
EXPLANATION

printStackTrace() prints to System.err, while getStackTrace() returns an array of StackTraceElement objects. Both exist and serve different purposes.

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

Multiple catch blocks with the same exception type cause compilation error. Java doesn't allow duplicate catch blocks for identical exception types.

Take Test
What is the output of the following code?
public class ExceptionTest {
public static void main(String[] args) {
try {
int x = 10 / 0;
} catch (ArithmeticException e) {
System.out.println("Caught");
} finally {
System.out.println("Finally");
}
}
}
A Caught Finally
B Finally
C Caught
D Program terminates with exception
Correct Answer:  A. Caught Finally
EXPLANATION

ArithmeticException is caught by the catch block printing 'Caught', then finally block executes printing 'Finally'.

Take Test
Which of the following statements about try-catch-finally blocks is correct?
A finally block is executed even if return statement is present in try block
B finally block is skipped if catch block throws an exception
C finally block cannot contain return statements
D finally block is optional only when catch block is present
Correct Answer:  A. finally block is executed even if return statement is present in try block
EXPLANATION

The finally block always executes regardless of whether a return, break, continue, or exception occurs in try or catch blocks, unless System.exit() is called.

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