Module 2: Defensive programming: Error handling

Knowledge check: Exceptions

Practice Assignment

1. True or False: Errors in Java indicate an abnormal condition that cannot be recovered and they are not meant to be handled.

  • True

  • False

2. Which of the following pieces of code, when added to line number 3, would cause a NullPointerException in line 4?

  • null
  • new String(“”)
  • new String()
  • “”

3. Which line of code causes an ArrayIndexOutOfBoundsException?

  • line 2
  • line 4
  • line 3
  • line 5

4. When the code below is run, the Exception object thrown gives this output.

According to this information, what is the line number in the code shown in the screenshot below that causes the exception?

  • line 9

  • line 12
  • line 14
  • line 8

5. Complete the code given in the blank in the code listing below that handles an Exception using the try-catch block.

  • FileNotFoundException e
  • ArrayIndexOutOfBoundsException e
  • NullPointerException e

  • ClassNotFoundException e

Knowledge check: Handling exceptions

Practice Assignment

6. You’ve written this code to handle the Exception caused by this code but your lead developer says you should catch specific Exceptions.

How should you correct this?

  • By replacing Exception in the catch block with NullPointerException.

  • By replacing Exception in the catch block with ArithmeticException.
  • By adding a 2nd Catch block with the FileNotFoundException.
  • By adding a 2nd catch block with the ArithmeticException.

7. How many catch blocks would you need to correctly handle Exceptions in this code?

  • 2 catch blocks
  • 3 catch blocks

  • 1 catch block
  • The code doesn’t need a catch block.

8. What would be the output of this code?

9. Which one of these code blocks would perform automatic resource clean up?

  • Both
  • Code Block 1
  • No resource clean up was performed in either code block.
  • Code Block 2

10. Apart from performing automatic cleaning up of resources, the try-with-resources involves writing less code.

  • True

  • False

Knowledge check: Preventative measures and best practice

Practice Assignment

11. You’re developing a financial application that processes transactions. During testing, you encounter an error when the system attempts to deduct an amount greater than the account balance. This exception needs to be caught and logged to maintain an audit trail. However, you also need to ensure that the calling method is aware of the issue so it can handle it appropriately.

How should you handle this scenario to ensure the exception is logged and rethrown for further handling?

  • Use the throw keyword inside the catch block.

  • Use the throws keyword inside the catch block
  • Use the throw keyword inside a try block.
  • Use the throws keyword inside the try block.

12. Which of the following statements are true regarding unchecked exceptions in Java? Select all that apply.

  • Unchecked exceptions are not checked at compile time.
  • Unchecked exceptions don’t require mandatory handling.
  • Unchecked exceptions arise out of programming errors.
  • IOException is an example of an unchecked exception.

13. Which of the following statements is true about Java's exception hierarchy?

  • You can catch an Error object.
  • You cannot catch a Throwable object.
  • Throwable is the base class for all errors and exceptions.

  • Exception is the superclass for all errors and exceptions.

14. Which of the following are the examples of checked exceptions? Select all that apply.

  • FileNotFoundException
  • ClassNotFoundException
  • ArithmeticException
  • RuntimeException

15. True or False: Catching all exceptions using a catch(Exception e) block makes it difficult to pinpoint the exact cause of an error.

  • True

  • False

Module quiz: Error handling

Graded Assignment

16. True or False: IOException is a checked exception in Java.

  • True

  • False

17. True or False: Checked exceptions should be added to a method signature to ensure the exception never causes the program to terminate.

  • True
  • False

18. True or False: An Error that occurs in a Java program should be handled using the error handling mechanism of Java.

  • True
  • False

19. True or False: The throws keyword is used in method declarations, while the throw keyword is used to throw an exception.

  • False
  • True

20. True or False: In the case of declaring multiple catch blocks, the catch block handling the exception of the subclass type must be declared first.

  • True
  • False

21. You're working on error handling in a Java program and need to ensure that exceptions are handled correctly. The code below currently catches all exceptions in a single catch block.

What is the best way to handle exceptions in this scenario?

  • The catch block specified in the code is correct and does not need to be modified.
  • Catching all exceptions in a single catch block is not advisable. Multiple catch blocks should be added here for better exception handling with specific exceptions.

  • Try catch block cannot work without a finally block.
  • Try catch block cannot work if not used as a try-with-resources block.

22. Which of the following statements is correct regarding the finally block in the exception handling mechanism in Java?

  • Every line of code written inside a finally block is always executed even if an exception occurs in the finally block.
  • A finally block is only executed in case the catch block situated directly above it is also executed.
  • The finally block can never throw an exception.
  • The finally statement requires a { } block.

23. Which of the following import statements is correct for using the Exception class, the RuntimeException class, or the Throwable class in a Java program when handling exceptions?

  • import java.lang.*;
  • A:import java.util.*;
  • import java.exception.*;
  • No specific import statement is required.

24. While handling exceptions in Java, if an exception matches two or more catch blocks in the program, which of the catch blocks would be executed first?

  • The catch block whose exception matches the exception caused in the program first.

  • The catch block that is written at the beginning.
  • All the catch blocks would be executed simultaneously.
  • The catch block whose exception matches the exception caused in the program last.

25. Select the correct option to complete the following: In Java’s exception handling mechanism, a try block can be followed by ________ catch block(s) and ________ finally block(s).

  • zero or one, one or more
  • one or more, one
  • zero or more, zero or one
  • zero or one, zero or one

Leave a Reply