MCQs on Error Handling | Ruby

Learn the essential concepts of error handling in Ruby, including exceptions, the use of begin, rescue, and ensure blocks. These MCQs will help you understand Ruby’s error-handling mechanisms.


1. Understanding Errors and Exceptions

  1. In Ruby, which of the following is used to handle errors and exceptions?
    • A) try
    • B) handle
    • C) rescue
    • D) catch
  2. What is the difference between an error and an exception in Ruby?
    • A) Errors are always critical, while exceptions are recoverable
    • B) Errors are recoverable, while exceptions are always fatal
    • C) Exceptions are more difficult to handle than errors
    • D) Errors and exceptions are the same in Ruby
  3. Which class does all exceptions in Ruby inherit from?
    • A) Exception
    • B) Error
    • C) StandardError
    • D) RuntimeError
  4. What is the default behavior when an exception is not caught in Ruby?
    • A) It prints an error message and continues execution
    • B) It stops the program execution
    • C) It logs the exception to a file
    • D) It ignores the exception silently
  5. What type of exception is raised if a method is called with wrong arguments in Ruby?
    • A) NoMethodError
    • B) ArgumentError
    • C) TypeError
    • D) RuntimeError

2. Using begin, rescue, and ensure

  1. What is the purpose of the begin block in Ruby error handling?
    • A) To define the code that might raise an exception
    • B) To define the code to be executed after an exception is raised
    • C) To catch exceptions raised by other methods
    • D) To terminate the program if an error occurs
  2. What does the rescue block do in Ruby?
    • A) It ensures that the code runs after the exception
    • B) It handles exceptions raised in the begin block
    • C) It prints a message when no exceptions occur
    • D) It raises a new exception after handling the old one
  3. Which of the following is the correct syntax for rescuing an exception?
    • A) rescue => ExceptionType
    • B) rescue ExceptionType
    • C) begin rescue ExceptionType end
    • D) rescue from ExceptionType
  4. What does the ensure block do in Ruby?
    • A) It runs the code only if an exception was raised
    • B) It runs the code after both begin and rescue
    • C) It runs only if the program has not executed successfully
    • D) It raises a new exception after handling the current one
  5. In which order are begin, rescue, and ensure blocks executed in Ruby?
    • A) begin -> rescue -> ensure
    • B) begin -> ensure -> rescue
    • C) rescue -> begin -> ensure
    • D) ensure -> begin -> rescue

3. Advanced Error Handling

  1. What happens if an exception is raised inside the ensure block?
    • A) The program terminates without handling the exception
    • B) The exception is passed to the rescue block
    • C) The exception is ignored and the program continues
    • D) The exception is raised after ensure execution completes
  2. Can the rescue block handle multiple types of exceptions in Ruby?
    • A) No, it can only handle one exception at a time
    • B) Yes, using multiple rescue blocks
    • C) Yes, by specifying multiple exceptions in a single rescue statement
    • D) No, each exception needs its own begin block
  3. What happens if no exception is raised in the begin block?
    • A) The rescue block is executed
    • B) The ensure block is executed
    • C) The program stops without executing the ensure block
    • D) The program throws a NoExceptionError
  4. What does rescue => e do in Ruby?
    • A) It assigns the exception to the variable e
    • B) It prints the exception message
    • C) It causes the program to terminate
    • D) It re-raises the exception
  5. How can you re-raise an exception after handling it in the rescue block?
    • A) Using raise with no arguments
    • B) Using re_raise keyword
    • C) Using retry statement
    • D) Using throw

4. Specific Exception Handling

  1. Which of the following exception types is raised when an undefined method is called in Ruby?
    • A) NoMethodError
    • B) ArgumentError
    • C) NameError
    • D) RuntimeError
  2. Which exception is raised when you attempt to divide by zero in Ruby?
    • A) ZeroDivisionError
    • B) ArgumentError
    • C) TypeError
    • D) StandardError
  3. What is the correct syntax to handle a ZeroDivisionError exception in Ruby?
    • A) rescue ZeroDivisionError => e
    • B) rescue ZeroDivisionError
    • C) catch ZeroDivisionError
    • D) raise ZeroDivisionError
  4. In Ruby, what is the class used for custom exception handling?
    • A) CustomError
    • B) StandardError
    • C) Exception
    • D) RuntimeError
  5. How do you define a custom exception in Ruby?
    • A) class CustomError < StandardError; end
    • B) class CustomError < Exception; end
    • C) def CustomError; end
    • D) class CustomError: StandardError; end

5. Practical Examples of Error Handling

  1. Which block is executed regardless of whether an exception is raised or not?
    • A) rescue
    • B) ensure
    • C) catch
    • D) raise
  2. Can you nest begin blocks in Ruby?
    • A) No, Ruby only allows a single begin block
    • B) Yes, but only one rescue block is allowed
    • C) Yes, you can nest multiple begin blocks
    • D) Yes, but only within the ensure block
  3. What is the output when the following Ruby code is executed?
rubyCopy codebegin  
  raise "Error!"  
rescue => e  
  puts "Caught an exception: #{e.message}"  
end
  • A) “Caught an exception: Error!”
  • B) “Error!”
  • C) nil
  • D) “Caught an exception: Exception”
  1. Which of the following would you use to retry a block of code after an exception?
    • A) retry
    • B) retry_block
    • C) repeat
    • D) catch
  2. What is the difference between raise and fail in Ruby?
    • A) raise creates a new exception, while fail prints an error message
    • B) fail is an alias for raise
    • C) raise can only be used with begin, while fail cannot
    • D) There is no difference
  3. Which exception class is the superclass for all Ruby exceptions?
    • A) Exception
    • B) StandardError
    • C) RuntimeError
    • D) NoMethodError
  4. How would you raise a custom exception with a message in Ruby?
    • A) raise CustomError.new(“Error message”)
    • B) raise “Error message”
    • C) raise CustomError, “Error message”
    • D) Both A and C
  5. Can exceptions be handled asynchronously in Ruby?
    • A) Yes, by using threads and rescue blocks
    • B) No, Ruby does not support asynchronous exception handling
    • C) Yes, but only with external libraries
    • D) No, exceptions must always be synchronous
  6. What would happen if the ensure block has an exception raised in Ruby?
    • A) The exception is ignored
    • B) The program terminates immediately
    • C) The exception is raised after the ensure block completes
    • D) The program continues executing with no interruption
  7. Which of the following is an example of handling multiple exceptions in a single rescue block?
    • A) rescue ZeroDivisionError, ArgumentError
    • B) rescue [ZeroDivisionError, ArgumentError]
    • C) rescue ZeroDivisionError then ArgumentError
    • D) Both A and B

Answers Table:

QnoAnswer
1C) rescue
2A) Errors are always critical, while exceptions are recoverable
3A) Exception
4B) It stops the program execution
5B) ArgumentError
6A) To define the code that might raise an exception
7B) It handles exceptions raised in the begin block
8B) rescue ExceptionType
9B) It runs the code after both begin and rescue
10A) begin -> rescue -> ensure
11A) The program terminates without handling the exception
12C) Yes, by specifying multiple exceptions in a single rescue statement
13B) The ensure block is executed
14A) It assigns the exception to the variable e
15A) Using raise with no arguments
16A) NoMethodError
17A) ZeroDivisionError
18A) rescue ZeroDivisionError => e
19C) Exception
20A) class CustomError < StandardError; end
21B) ensure
22C) Yes, you can nest multiple begin blocks
23A) “Caught an exception: Error!”
24A) retry
25B) fail is an alias for raise
26A) Exception
27D) Both A and C
28A) Yes, by using threads and rescue blocks
29C) The exception is raised after the ensure block completes
30D) Both A and B

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