iGET

Java Programming - MCQ Practice Questions

Practice free Java Programming multiple-choice questions with detailed answers and explanations. Perfect for competitive exam preparation.

958 questions | 100% Free

Q.221Medium

What happens if you use 'return' statement inside a finally block?

Q.222Medium

What will be printed? public class Test { public static void main(String[] args) { try { System.out.println("In try"); return; } catch (Exception e) { System.out.println("In catch"); } finally { System.out.println("In finally"); } } }

Q.223Medium

What is the output of exception chaining in Java?

Q.224Medium

Which method is used to get the exception that caused the current exception in Java?

Q.225Medium

Which exception is thrown when a thread tries to acquire a monitor that is already locked?

Q.226Medium

Consider the following code. What will be printed? public class FinallyTest { public static int getValue() { try { return 10; } finally { System.out.println("Finally"); } } public static void main(String[] args) { System.out.println(getValue()); } }

Q.227Medium

Which of the following statements is true regarding exception constructors?

Q.228Medium

What is the output of multi-level exception chaining? public class ChainTest { public static void main(String[] args) { try { try { throw new IOException("IO Error"); } catch (IOException e) { throw new RuntimeException("Runtime Error", e); } } catch (RuntimeException e) { System.out.println(e.getCause().getClass().getSimpleName()); } } }

Q.229Medium

Which of the following best describes a custom exception in Java?

Q.230Medium

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.231Medium

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

Q.232Medium

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

Q.233Medium

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

Q.234Medium

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

Q.235Medium

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.236Medium

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

Q.237Medium

Which of the following will NOT result in a NullPointerException?

Q.238Medium

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

Q.239Medium

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

Q.240Medium

Which of the following is true about try-with-resources (Java 7+)?