Elixir programming language is known for its strong concurrency model, and its powerful features allow developers to build reliable and fault-tolerant systems. This quiz covers advanced topics like GenServer, custom process managers, OTP patterns, and managing process states and side effects, essential for mastering Elixir’s concurrency model and process management.
MCQs: Advanced Processes and OTP | Elixir Programming
Deep Dive into GenServer
What is the purpose of GenServer in Elixir?
a) To create a generic process for handling state
b) To define a process for handling user input
c) To manage asynchronous tasks
d) To execute processes in parallel
How do you start a GenServer in Elixir?
a) GenServer.start_link(MyModule, initial_state)
b) start_gen_server(MyModule, initial_state)
c) start_gen(MyModule, initial_state)
d) GenServer.new(MyModule, initial_state)
Which callback is required when defining a GenServer?
a) init/1
b) start/1
c) call/2
d) run/1
How do you send a synchronous message to a GenServer?
a) GenServer.cast(MyServer, :message)
b) GenServer.call(MyServer, :message)
c) GenServer.push(MyServer, :message)
d) GenServer.send(MyServer, :message)
Which function would you use to stop a GenServer in Elixir?
a) GenServer.exit(MyServer)
b) GenServer.stop(MyServer)
c) GenServer.kill(MyServer)
d) GenServer.terminate(MyServer)
Custom Process Managers and Supervisors
What is the primary role of a supervisor in Elixir’s OTP?
a) To supervise and monitor tasks and processes
b) To store process states
c) To execute commands in parallel
d) To manage the configuration files
How do you define a custom supervisor in Elixir?
a) defmodule MySupervisor do ... end
b) use Supervisor
c) defmodule MySupervisor do use Supervisor
d) supervisor MySupervisor do ... end
Which strategy is used in Elixir to restart a child process if it crashes?
a) :permanent
b) :temporary
c) :transient
d) :normal
What is the default restart strategy for a supervisor in Elixir?
a) :permanent
b) :transient
c) :temporary
d) :one_for_one
Which callback is used to define a child specification in a supervisor?
a) init/1
b) handle_call/3
c) start_child/2
d) init/2
Advanced OTP Patterns (e.g., GenEvent, GenStage)
What is the purpose of GenEvent in Elixir’s OTP?
a) To handle asynchronous events
b) To manage state across multiple processes
c) To define a supervisor for events
d) To monitor multiple processes
Which OTP module would you use to implement an event-based system?
a) GenServer
b) GenEvent
c) GenStage
d) GenProcess
What is GenStage primarily used for in Elixir?
a) Managing a pool of workers
b) Building data pipelines with demand-driven backpressure
c) Handling network requests
d) Managing state across nodes
How does GenStage handle backpressure in a data pipeline?
a) By discarding excess events
b) By slowing down the producer process
c) By storing events in a queue
d) By increasing the number of consumers
What is the key difference between GenEvent and GenStage?
a) GenStage is for managing concurrent processes, while GenEvent is for managing state.
b) GenEvent handles event-based notifications, while GenStage handles backpressure in data pipelines.
c) GenEvent is deprecated, and GenStage is used for message passing.
d) GenStage is for event-driven architectures, while GenEvent is for parallelism.
Handling Process State and Side Effects
How is process state managed in a GenServer?
a) Using global variables
b) Using the :state parameter in callbacks
c) Using ETS tables
d) Using the :memory storage
What is the default method to update process state in a GenServer?
a) handle_info/2
b) handle_cast/2
c) handle_call/3
d) handle_state/1
What happens if a GenServer’s state is mutated improperly?
a) The state will be discarded, and the process will restart.
b) The process will exit and may restart depending on the supervisor’s strategy.
c) The state will remain unchanged, and the program continues.
d) It will cause a crash but the application will recover.
How can side effects (e.g., sending messages, logging) be handled in GenServer callbacks?
a) Using handle_info/2
b) By calling external processes directly
c) By using external libraries
d) By handling them in a separate process
How can a process handle state persistence across system restarts in Elixir?
a) Using state transfer protocols
b) Using the :persistent_term module
c) Using databases or file systems for external storage
d) By saving the state in the supervisor
How is state stored in a GenServer?
a) In a variable within the process
b) In a global registry
c) In a cache
d) In the process heap
What happens if a GenServer processes a request while its state is being modified?
a) The state is locked, and requests are queued.
b) The state is reset before processing the request.
c) The process crashes to avoid inconsistencies.
d) The request is ignored.
How do you restart a process manually from within its GenServer callback?
a) GenServer.restart()
b) Process.restart()
c) Supervisor.restart_child()
d) GenServer.terminate()
How do you check the state of a GenServer from another process?
a) GenServer.status(MyServer)
b) GenServer.call(MyServer, :get_state)
c) GenServer.get(MyServer)
d) GenServer.lookup(MyServer)
What is the recommended way to handle a long-running process that maintains state?
a) Use GenServer to handle state and split tasks into multiple calls.
b) Use a Task for long-running processes.
c) Use a GenEvent for event-driven architectures.
d) Use a plain process with no supervisor.
How does Elixir’s actor model impact state management in processes?
a) Each process manages its own state independently.
b) State is shared across all processes.
c) State must be stored in a database for persistence.
d) State is automatically synchronized across all nodes.
What happens if you attempt to send a message to a GenServer that is not running?
a) The message is ignored.
b) An error is raised.
c) The process is automatically restarted.
d) The message is queued.
Which process type in OTP allows for self-healing systems by restarting a crashed child process?
a) GenServer
b) Supervisor
c) GenEvent
d) Task
Which callback handles unhandled messages in a GenServer?
a) handle_cast/2
b) handle_info/2
c) handle_call/3
d) handle_message/2
What is the role of :side_effects in process state management?
a) It stores side-effect operations such as logging.
b) It allows processes to handle external state changes.
c) It is used to define asynchronous tasks.
d) It stores the process state.
Answer Table
Qno
Answer (Option with Text)
1
a) To create a generic process for handling state
2
a) GenServer.start_link(MyModule, initial_state)
3
a) init/1
4
b) GenServer.call(MyServer, :message)
5
b) GenServer.stop(MyServer)
6
a) To supervise and monitor tasks and processes
7
b) use Supervisor
8
a) :permanent
9
d) :one_for_one
10
a) init/1
11
b) GenEvent
12
b) GenEvent
13
b) Building data pipelines with demand-driven backpressure
14
b) By slowing down the producer process
15
b) GenEvent handles event-based notifications, while GenStage handles backpressure in data pipelines
16
b) Using the :state parameter in callbacks
17
b) handle_cast/2
18
b) The process will exit and may restart depending on the supervisor’s strategy.
19
a) Using handle_info/2
20
c) Using databases or file systems for external storage
21
a) In a variable within the process
22
a) The state is locked, and requests are queued.
23
c) Supervisor.restart_child()
24
b) GenServer.call(MyServer, :get_state)
25
a) Use GenServer to handle state and split tasks into multiple calls.
26
a) Each process manages its own state independently.
27
b) An error is raised.
28
b) Supervisor
29
b) handle_info/2
30
a) It stores side-effect operations such as logging.