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