iGET

Java Programming - MCQ Practice Questions

Java interviews and campus tests lean heavily on object oriented behaviour rather than API recall. This set covers inheritance and polymorphism, interfaces and abstract classes, the collections framework, exception handling, multithreading, string handling, and JVM basics such as garbage collection. Output based questions are included because they expose the gaps that theory questions hide.

951 questions | 100% Free

Q.461Medium

What will be the result of executing this code?
public class MultiCatchTest {
public static void main(String[] args) {
try {
int[] arr = {1, 2};
int x = arr[5];
} catch (ArrayIndexOutOfBoundsException | NullPointerException e) {
System.out.println("Array or Null Error");
}
}
}

Q.462Easy

Which method is used to print the stack trace of an exception?

Q.463Easy

Which of the following is NOT a subclass of Throwable?

Q.464Medium

Consider code that implements auto-closeable resources. What is the advantage of try-with-resources statement?

Q.465Hard

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");
}
}
}

Q.466Hard

Which of the following scenarios will NOT trigger a StackOverflowError?

Q.467Medium

What is the correct way to create a custom exception with cause chaining?

Q.468Hard

What happens when System.exit() is called inside try block?

Q.469Medium

Which interface must a class implement to work with try-with-resources?

Q.470Easy

In Java exception handling, which of the following statements is true about the finally block?

Q.471Easy

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);

Q.472Medium

In multi-catch block (Java 7+), which operator is used to catch multiple exceptions in a single catch clause?

Q.473Medium

What will be the output?
try {
throw new Exception("Test");
} catch(Exception e) {
System.out.println("Caught");
throw new RuntimeException("New");
} finally {
System.out.println("Finally");
}

Q.474Easy

Which exception is thrown when accessing an array element with an invalid index?

Q.475Easy

What is the correct syntax for declaring a method that throws a checked exception?

Q.476Medium

Consider the following exception hierarchy. Which statement will compile without error?
class MyException extends Exception {}
class MyRuntimeException extends RuntimeException {}

Q.477Easy

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");
}

Q.478Medium

Which of the following will NOT result in a NullPointerException?

Q.479Medium

In Java, which method of Throwable class is used to obtain the cause of an exception?

Q.480Medium

What will be printed when this code executes?
String str = "Java";
try {
int x = Integer.parseInt(str);
} catch(NumberFormatException e) {
System.out.println("Invalid number");
} catch(Exception e) {
System.out.println("General exception");
}