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
Which statement is TRUE about exception propagation in Java?
What will be the behavior if an exception is thrown in the finally block?
Which statement about try-with-resources (introduced in Java 7) is correct?
Consider this code snippet. What will happen? try { int x = ; } catch (NullPointerException e) { System.out.println("Caught NPE"); }
What is the output of this code? public class ExceptionDemo { public static void main(String[] args) { try { int arr[] = new int[2]; arr[5] = 10; } catch (Exception e) { System.out.println("Exception caught"); } finally { System.out.println("Finally block"); } } }
What happens if you use 'return' statement inside a finally block?
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"); } } }
What is the output of exception chaining in Java?
Which method is used to get the exception that caused the current exception in Java?
Which exception is thrown when a thread tries to acquire a monitor that is already locked?
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()); } }
Which of the following statements is true regarding exception constructors?
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()); } } }
Which of the following best describes a custom exception in Java?
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"); } } }
Consider code that implements auto-closeable resources. What is the advantage of try-with-resources statement?
What is the correct way to create a custom exception with cause chaining?
Which interface must a class implement to work with try-with-resources?
In multi-catch block (Java 7+), which operator is used to catch multiple exceptions in a single catch clause?
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"); }