MCQs on Error Handling and Fault Tolerance | Elixir

Elixir is a robust programming language designed for building scalable and fault-tolerant applications. In Elixir, error handling and fault tolerance are vital concepts that ensure your system remains resilient. This set of 30 multiple-choice questions (MCQs) focuses on supervisors, error handling mechanisms like try, catch, rescue, after, as well as dealing with exit signals.

MCQs on Error Handling and Fault Tolerance in Elixir

Supervisors and Restart Strategies

  1. What is the role of a supervisor in Elixir?
    • A) To manage the process lifecycle and handle failures
    • B) To create new processes
    • C) To send messages between processes
    • D) To monitor system memory usage
  2. Which of the following is a valid strategy for a supervisor in Elixir?
    • A) :one_for_one
    • B) :many_for_one
    • C) :all_for_all
    • D) All of the above
  3. What does the :one_for_one strategy do in Elixir?
    • A) Restart the crashed process only
    • B) Restart all processes in the supervisor tree
    • C) Restart the entire application
    • D) None of the above
  4. What is the default strategy for a supervisor in Elixir?
    • A) :one_for_one
    • B) :rest_for_one
    • C) :all_for_one
    • D) :simple_one_for_all
  5. When should you use the :rest_for_one strategy in Elixir?
    • A) When a child process fails and other dependent processes need to be restarted
    • B) When you need to restart all child processes in the tree
    • C) When the failure of one process should not affect others
    • D) None of the above

try, catch, rescue, and after in Concurrency

  1. What does the try block do in Elixir?
    • A) Defines a block of code to execute, which may raise an exception
    • B) Catches exceptions from other processes
    • C) Stops execution if an error occurs
    • D) Defines a loop for recurring tasks
  2. How does catch work in Elixir when used with try?
    • A) It handles errors raised by the try block
    • B) It interrupts the execution of the try block
    • C) It ignores exceptions in the try block
    • D) It sets up an infinite loop
  3. What does rescue do in Elixir?
    • A) It allows handling of specific errors raised in the try block
    • B) It exits the process when an error occurs
    • C) It logs the error without stopping the program
    • D) It raises a new error
  4. What is the purpose of the after block in Elixir’s error handling?
    • A) To execute code after the try block, whether or not an error occurs
    • B) To rescue exceptions raised in the try block
    • C) To raise custom exceptions
    • D) To start a new process after catching an error
  5. Which of the following is a correct syntax for using try, catch, rescue, and after in Elixir?
    • A) try do ... catch ... rescue ... after ... end
    • B) try do ... rescue ... catch ... after ... end
    • C) try do ... after ... catch ... rescue ... end
    • D) try do ... rescue ... after ... catch ... end

The let and raise Macros

  1. What is the purpose of the raise macro in Elixir?
    • A) To raise an exception explicitly
    • B) To start a new process
    • C) To log an error without interrupting execution
    • D) To restart a crashed process
  2. How do you specify the error message when using the raise macro in Elixir?
    • A) raise("Error message")
    • B) raise("Error message", :error_type)
    • C) raise :error_type, "Error message"
    • D) raise :error_type
  3. What is the function of the let macro in Elixir?
    • A) It binds values to variables within a scoped block
    • B) It raises an error for a failed condition
    • C) It executes code after an exception
    • D) It returns the result of an expression
  4. Which of the following is a valid example of using the raise macro in Elixir?
    • A) raise "Something went wrong"
    • B) raise :error, "Something went wrong"
    • C) raise "Error occurred" error
    • D) All of the above
  5. How would you handle a RuntimeError raised using the raise macro in Elixir?
    • A) Use try and rescue to catch the exception
    • B) Use catch to intercept the error
    • C) Handle the error with let block
    • D) Ignore it to continue execution

Working with Error and Exit Signals

  1. What happens when an Elixir process receives an exit signal?
    • A) The process terminates
    • B) The process handles the signal gracefully
    • C) The process restarts automatically
    • D) The process ignores the signal
  2. Which function is used to send an exit signal to a process in Elixir?
    • A) Process.exit/2
    • B) send_exit/2
    • C) kill_process/1
    • D) terminate/1
  3. How can a process in Elixir handle exit signals?
    • A) By trapping exits with Process.flag(:trap_exit, true)
    • B) By ignoring the signal
    • C) By terminating immediately
    • D) By using the catch_exit/1 function
  4. What is the purpose of the :trap_exit flag in Elixir?
    • A) To allow a process to handle exit signals
    • B) To stop a process from exiting
    • C) To ensure processes restart on failure
    • D) To trigger custom error handling routines
  5. What is the default behavior of processes with respect to exit signals in Elixir?
    • A) Processes terminate when they receive an exit signal
    • B) Processes handle exit signals silently
    • C) Processes restart automatically upon receiving an exit signal
    • D) Processes ignore all exit signals

Error Handling in Concurrency

  1. What does the try block do in Elixir when dealing with concurrent tasks?
    • A) It allows error handling in concurrent processes
    • B) It forces tasks to complete sequentially
    • C) It ignores exceptions raised in processes
    • D) It forces a process to exit immediately
  2. How does Elixir handle errors raised in concurrent tasks?
    • A) Through supervisors and fault-tolerant strategies
    • B) By ignoring the errors and continuing
    • C) By terminating the entire system
    • D) By notifying the user interface
  3. Which of the following is a correct example of using the rescue block in Elixir?
    • A) try do ... rescue RuntimeError -> ... end
    • B) try do ... catch RuntimeError -> ... end
    • C) try do ... after -> ... end
    • D) try do ... rescue -> ... end
  4. What happens if an exception is raised inside a try block without a rescue in Elixir?
    • A) The exception will propagate and the process will crash
    • B) The exception will be caught and handled
    • C) The process will log the exception but continue execution
    • D) The system will restart the process
  5. What is the purpose of the after block in a try-catch-rescue construct in Elixir?
    • A) It is executed whether or not an error occurs in the try block
    • B) It raises an error if the try block fails
    • C) It terminates the process after an error
    • D) It restarts the process after an error

Fault Tolerance and Error Recovery

  1. What is the concept of “let it crash” in Elixir?
    • A) Allowing processes to fail and letting supervisors handle recovery
    • B) Preventing processes from crashing
    • C) Ignoring any errors raised by processes
    • D) Always restarting processes after a failure
  2. How does Elixir ensure fault tolerance in a distributed system?
    • A) By using supervisors and a “let it crash” philosophy
    • B) By using global variables
    • C) By using memory synchronization
    • D) By preventing errors from occurring
  3. What happens to the child processes in a supervisor tree when a process crashes in Elixir?
    • A) The supervisor follows its restart strategy
    • B) All processes in the supervisor tree are stopped
    • C) The supervisor logs the crash but does not restart any processes
    • D) The entire system crashes
  4. Which of the following is used to monitor the state of processes in Elixir?
    • A) Process monitoring and supervisors
    • B) Error logs and event handlers
    • C) System health check functions
    • D) None of the above
  5. What is a key benefit of Elixir’s error handling model?
    • A) It provides a robust way to handle errors in distributed systems
    • B) It allows errors to go untracked for performance
    • C) It eliminates the need for custom error handling
    • D) It disables process restarts on failure

Answer Key

QnoAnswer
1A) To manage the process lifecycle and handle failures
2D) All of the above
3A) Restart the crashed process only
4A) :one_for_one
5A) When a child process fails and other dependent processes need to be restarted
6A) Defines a block of code to execute, which may raise an exception
7A) It handles errors raised by the try block
8A) It allows handling of specific errors raised in the try block
9A) To execute code after the try block, whether or not an error occurs
10A) try do ... catch ... rescue ... after ... end
11A) To raise an exception explicitly
12C) raise :error_type, "Error message"
13A) It binds values to variables within a scoped block
14D) All of the above
15A) Use try and rescue to catch the exception
16A) The process terminates
17A) Process.exit/2
18A) By trapping exits with Process.flag(:trap_exit, true)
19A) To allow a process to handle exit signals
20A) Processes terminate when they receive an exit signal
21A) It allows error handling in concurrent processes
22A) Through supervisors and fault-tolerant strategies
23A) try do ... rescue RuntimeError -> ... end
24A) The exception will propagate and the process will crash
25A) It is executed whether or not an error occurs in the try block
26A) Allowing processes to fail and letting supervisors handle recovery
27A) By using supervisors and a “let it crash” philosophy
28A) The supervisor follows its restart strategy
29A) Process monitoring and supervisors
30A) It provides a robust way to handle errors in distributed systems

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