MCQs on Error Handling and Exceptions | Groovy

Groovy is a dynamic language that simplifies error handling through its robust exception mechanism. Understanding how to use try-catch blocks, create custom exceptions, and apply best practices for handling errors efficiently is crucial for building stable Groovy applications. Below are 30 multiple-choice questions (MCQs) covering these important topics.

MCQs on Error Handling and Exceptions in Groovy

Groovy’s Exception Mechanism

  1. What is the default superclass for all exceptions in Groovy?
    • A) Throwable
    • B) Exception
    • C) Error
    • D) RuntimeException
  2. In Groovy, which class is used to handle runtime exceptions?
    • A) Throwable
    • B) Exception
    • C) RuntimeException
    • D) Error
  3. Which of the following is true about checked exceptions in Groovy?
    • A) They must be declared or handled explicitly
    • B) They are never thrown
    • C) They are handled implicitly
    • D) They cannot be caught
  4. Groovy supports which type of exceptions?
    • A) Only runtime exceptions
    • B) Only checked exceptions
    • C) Both checked and runtime exceptions
    • D) No exceptions
  5. Which of the following is an example of a Groovy runtime exception?
    • A) NullPointerException
    • B) IOException
    • C) SQLException
    • D) FileNotFoundException

Try-Catch Blocks

  1. What is the purpose of the try block in Groovy?
    • A) To define the exception handler
    • B) To execute the code that might throw an exception
    • C) To define custom exceptions
    • D) To catch all exceptions
  2. In a try-catch block, what happens if no exceptions are thrown in the try block?
    • A) The catch block is executed
    • B) The finally block is executed
    • C) The program terminates
    • D) Nothing happens
  3. How many catch blocks can be used in a Groovy try-catch structure?
    • A) One
    • B) Unlimited
    • C) Two
    • D) Three
  4. What is the role of the finally block in Groovy exception handling?
    • A) It is executed only when an exception is caught
    • B) It runs after the catch block to clean up resources
    • C) It runs before the try block
    • D) It prevents exceptions from being thrown
  5. Which statement about catch blocks in Groovy is correct?
    • A) A catch block can catch exceptions of any type
    • B) A catch block must be specific to the exception type
    • C) Multiple catch blocks are not allowed
    • D) The catch block is not required

Custom Exceptions

  1. To define a custom exception in Groovy, which class should it inherit from?
    • A) RuntimeException
    • B) Exception
    • C) Throwable
    • D) Error
  2. What is the purpose of creating a custom exception in Groovy?
    • A) To handle common errors
    • B) To make debugging easier
    • C) To handle specific error cases unique to the application
    • D) To simplify the catch block
  3. How do you throw a custom exception in Groovy?
    • A) throw new MyException()
    • B) raise MyException()
    • C) catch MyException()
    • D) throwMyException
  4. Which of the following is correct for a custom exception in Groovy?
    • A) It can extend any exception class
    • B) It cannot be caught in a catch block
    • C) It must extend Throwable or Error
    • D) It must not have a constructor
  5. What type of exception is typically used for errors that occur due to invalid user input?
    • A) Custom exceptions
    • B) NullPointerException
    • C) IOException
    • D) IllegalArgumentException

Best Practices in Exception Handling

  1. Which of the following is a recommended practice for exception handling in Groovy?
    • A) Catch all exceptions using Exception
    • B) Use generic exception classes to simplify code
    • C) Catch specific exceptions to handle errors accurately
    • D) Ignore exceptions to improve performance
  2. What should be done in the finally block if an exception is caught in Groovy?
    • A) Return a default value
    • B) Clean up resources like closing files or database connections
    • C) Retry the failed operation
    • D) Terminate the program
  3. What is the purpose of the throw keyword in Groovy?
    • A) To create new exception classes
    • B) To catch exceptions
    • C) To explicitly throw an exception
    • D) To handle exceptions
  4. Which of the following is the best way to handle exceptions in a loop?
    • A) Catch exceptions inside the loop for each iteration
    • B) Allow exceptions to propagate
    • C) Use finally to handle exceptions after the loop
    • D) Ignore exceptions and continue
  5. How can you re-throw an exception after catching it in Groovy?
    • A) throw e
    • B) rethrow e
    • C) continue e
    • D) return e

Advanced Exception Handling

  1. Can Groovy exceptions be nested?
    • A) Yes, exceptions can be nested inside each other
    • B) No, exceptions cannot be nested
    • C) Only runtime exceptions can be nested
    • D) Only checked exceptions can be nested
  2. How does Groovy handle multiple catch blocks for different exception types?
    • A) The first matching catch block is executed
    • B) It executes all catch blocks
    • C) It ignores catch blocks
    • D) It executes the last catch block
  3. What is the correct way to catch multiple exception types in one catch block?
    • A) catch (IOException | SQLException e)
    • B) catch (IOException and SQLException)
    • C) catch (IOException, SQLException e)
    • D) catch (IOException SQLException e)
  4. Which of the following is a valid way to use Groovy’s try-catch with an exception variable?
    • A) try { // code } catch (Exception e) { // handle e }
    • B) try { // code } catch (e) { // handle e }
    • C) try { // code } except (e) { // handle e }
    • D) try { // code } catch (e: Exception) { // handle e }
  5. What does throw new IllegalArgumentException("Invalid input") do in Groovy?
    • A) Catches an exception
    • B) Prints an error message
    • C) Throws an exception with a custom message
    • D) Terminates the program

Handling Specific Exceptions

  1. Which exception should be thrown if a required resource (like a file) is not found?
    • A) FileNotFoundException
    • B) IOException
    • C) NullPointerException
    • D) IllegalStateException
  2. What is the purpose of using try-catch blocks in Groovy?
    • A) To prevent exceptions from being thrown
    • B) To execute code that may raise exceptions and handle them gracefully
    • C) To create custom exceptions
    • D) To catch all types of errors
  3. When should you catch a NullPointerException in Groovy?
    • A) When the code attempts to access or modify a null object reference
    • B) When the code attempts to open a file
    • C) When the code exceeds the stack size
    • D) When the code fails to handle an invalid input
  4. What is the purpose of Groovy’s throwable class?
    • A) It is the base class for all exceptions and errors
    • B) It allows for throwing custom exceptions
    • C) It handles runtime exceptions only
    • D) It is used to catch unchecked exceptions
  5. What should be included in a custom exception class constructor?
    • A) A message that describes the error
    • B) A default value
    • C) A specific exception type
    • D) The exception message and cause

Answers

QnoAnswer
1A) Throwable
2C) RuntimeException
3A) They must be declared or handled explicitly
4C) Both checked and runtime exceptions
5A) NullPointerException
6B) To execute the code that might throw an exception
7B) The finally block is executed
8B) Unlimited
9B) It runs after the catch block to clean up resources
10B) A catch block must be specific to the exception type
11B) Exception
12C) To handle specific error cases unique to the application
13A) throw new MyException()
14A) It can extend any exception class
15D) IllegalArgumentException
16C) Catch specific exceptions to handle errors accurately
17B) Clean up resources like closing files or database connections
18C) To explicitly throw an exception
19A) Catch exceptions inside the loop for each iteration
20A) throw e
21A) Yes, exceptions can be nested inside each other
22A) The first matching catch block is executed
23A) `catch (IOException
24A) try { // code } catch (Exception e) { // handle e }
25C) Throws an exception with a custom message
26A) FileNotFoundException
27B) To execute code that may raise exceptions and handle them gracefully
28A) When the code attempts to access or modify a null object reference
29A) It is the base class for all exceptions and errors
30A) A message that describes the error

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top