MCQs on Advanced Processes and OTP | Elixir

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

  1. 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
  2. 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)
  3. Which callback is required when defining a GenServer?
    • a) init/1
    • b) start/1
    • c) call/2
    • d) run/1
  4. 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)
  5. 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

  1. 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
  2. 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
  3. Which strategy is used in Elixir to restart a child process if it crashes?
    • a) :permanent
    • b) :temporary
    • c) :transient
    • d) :normal
  4. What is the default restart strategy for a supervisor in Elixir?
    • a) :permanent
    • b) :transient
    • c) :temporary
    • d) :one_for_one
  5. 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)

  1. 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
  2. Which OTP module would you use to implement an event-based system?
    • a) GenServer
    • b) GenEvent
    • c) GenStage
    • d) GenProcess
  3. 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
  4. 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
  5. 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

  1. 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
  2. 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
  3. 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.
  4. 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
  5. 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
  6. 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
  7. 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.
  8. 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()
  9. 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)
  10. 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.
  11. 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.
  12. 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.
  13. Which process type in OTP allows for self-healing systems by restarting a crashed child process?
    • a) GenServer
    • b) Supervisor
    • c) GenEvent
    • d) Task
  14. Which callback handles unhandled messages in a GenServer?
    • a) handle_cast/2
    • b) handle_info/2
    • c) handle_call/3
    • d) handle_message/2
  15. 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

QnoAnswer (Option with Text)
1a) To create a generic process for handling state
2a) GenServer.start_link(MyModule, initial_state)
3a) init/1
4b) GenServer.call(MyServer, :message)
5b) GenServer.stop(MyServer)
6a) To supervise and monitor tasks and processes
7b) use Supervisor
8a) :permanent
9d) :one_for_one
10a) init/1
11b) GenEvent
12b) GenEvent
13b) Building data pipelines with demand-driven backpressure
14b) By slowing down the producer process
15b) GenEvent handles event-based notifications, while GenStage handles backpressure in data pipelines
16b) Using the :state parameter in callbacks
17b) handle_cast/2
18b) The process will exit and may restart depending on the supervisor’s strategy.
19a) Using handle_info/2
20c) Using databases or file systems for external storage
21a) In a variable within the process
22a) The state is locked, and requests are queued.
23c) Supervisor.restart_child()
24b) GenServer.call(MyServer, :get_state)
25a) Use GenServer to handle state and split tasks into multiple calls.
26a) Each process manages its own state independently.
27b) An error is raised.
28b) Supervisor
29b) handle_info/2
30a) It stores side-effect operations such as logging.

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