MCQs on Processes and Concurrency | Elixir

Elixir is known for its robust support for concurrency, allowing developers to build scalable and fault-tolerant applications. The language’s process-based model allows for easy parallel execution. In this set of 30 multiple-choice questions, we’ll explore managing processes with GenServer, handling state, process lifecycle, and the importance of supervision trees in fault tolerance.


30 Multiple-Choice Questions on Elixir Processes and Concurrency

1. What is the primary purpose of a GenServer in Elixir?

  • A) To manage web requests
  • B) To create a new thread for every request
  • C) To manage state and handle asynchronous calls
  • D) To handle HTTP routing

2. Which of the following is NOT a function provided by GenServer?

  • A) handle_call
  • B) handle_cast
  • C) handle_event
  • D) init

3. What does GenServer.start_link/3 do?

  • A) Starts a new process and links it to the caller
  • B) Starts a new process without linking it
  • C) Creates a new GenServer but does not start it
  • D) Initializes the state for a GenServer

4. In the GenServer lifecycle, what is the purpose of the init function?

  • A) To terminate the process
  • B) To initialize state and return the state
  • C) To send messages to the client
  • D) To handle synchronous calls

5. What does the handle_call/3 function in a GenServer do?

  • A) Handles asynchronous messages
  • B) Handles synchronous requests and returns a reply
  • C) Initializes the state
  • D) Sends messages to another process

6. Which function in GenServer is used for handling asynchronous requests?

  • A) handle_cast
  • B) handle_call
  • C) handle_event
  • D) handle_sync

7. What does the GenServer.stop/1 function do?

  • A) Stops the process immediately
  • B) Stops the process after handling pending messages
  • C) Terminates the caller process
  • D) Notifies all processes to stop

8. What type of communication model is used in GenServer for handling messages?

  • A) Shared memory
  • B) Message passing
  • C) Direct function calls
  • D) Blocking queues

9. What is the primary responsibility of the handle_cast/2 function in a GenServer?

  • A) To handle synchronous messages
  • B) To send a response back to the client
  • C) To handle asynchronous messages and update state
  • D) To initialize the state of the GenServer

10. What does the GenServer.call/2 function do?

  • A) Sends an asynchronous message to a GenServer
  • B) Sends a synchronous message and waits for a reply
  • C) Starts a new process
  • D) Calls an external API

11. What is the role of the state in GenServer?

  • A) It holds the current process’s memory state
  • B) It holds data that the GenServer processes use
  • C) It is used to track the number of messages processed
  • D) It holds global variables

12. In a GenServer, what does the :ok atom usually signify?

  • A) Success, with no further action needed
  • B) Failure, but continuing to handle the next request
  • C) An unhandled error in the process
  • D) Initialization of the GenServer

13. Which of the following statements is true about the init callback in GenServer?

  • A) It can return an error to stop the GenServer
  • B) It must return a :stop response to shut down
  • C) It always starts a new process
  • D) It is only called when the server is shutting down

14. What happens when a GenServer process crashes?

  • A) The process automatically restarts
  • B) The process stops without any intervention
  • C) The system terminates the entire application
  • D) The system ignores the failure and continues

15. What is a supervision tree in Elixir?

  • A) A data structure for handling concurrency
  • B) A collection of processes that manage the life cycle of other processes
  • C) A way to handle database transactions
  • D) A built-in tool for error handling

16. How does a supervisor handle a failing child process?

  • A) It restarts the child process
  • B) It sends an error notification to the parent
  • C) It terminates the child process without taking any further action
  • D) It ignores the failure

17. What is the purpose of the :restart strategy in a supervisor?

  • A) To terminate the child process immediately
  • B) To restart a child process if it crashes
  • C) To restart the entire system
  • D) To prevent the process from being restarted

18. In Elixir, what is the benefit of separating state and logic in processes?

  • A) It improves performance
  • B) It allows for better fault tolerance and scalability
  • C) It reduces memory usage
  • D) It simplifies code readability

19. What is the default behavior when a GenServer crashes?

  • A) The parent process will terminate the application
  • B) The process will automatically be restarted
  • C) The process will continue running indefinitely
  • D) The system will ignore the failure

20. What does the :temporary restart strategy in a supervisor indicate?

  • A) The process should be restarted immediately after failure
  • B) The process should not be restarted after it fails
  • C) The process should be restarted after a delay
  • D) The process should never fail

21. Which function is used to handle synchronous requests in GenServer?

  • A) handle_sync
  • B) handle_cast
  • C) handle_call
  • D) handle_message

22. What would happen if a GenServer state is not updated properly?

  • A) The system will terminate immediately
  • B) The GenServer will crash with an error
  • C) The process will continue with outdated state
  • D) A new state will be generated automatically

23. How do you handle an error in a GenServer’s handle_call or handle_cast function?

  • A) By using the try and catch blocks
  • B) By returning {:error, reason}
  • C) By letting the error propagate to the caller
  • D) By raising a custom exception

24. Which of the following is true about a process in Elixir?

  • A) A process is a lightweight thread with its own memory space
  • B) Processes in Elixir share memory space for better performance
  • C) Processes in Elixir are always created by a supervisor
  • D) A process in Elixir can run only on a single core

25. What is the key difference between handle_call and handle_cast?

  • A) handle_call is used for synchronous requests, while handle_cast is for asynchronous requests
  • B) handle_call is for terminating the process, and handle_cast is for initialization
  • C) handle_call handles messages, while handle_cast handles errors
  • D) There is no difference between them

26. What is a “supervisor” in the context of Elixir?

  • A) A process that manages other processes, ensuring they restart on failure
  • B) A process that handles all incoming requests
  • C) A monitor that tracks system performance
  • D) A utility for managing databases

27. What happens if a child process in a supervisor tree crashes?

  • A) It is ignored and continues running
  • B) The supervisor will restart the child process
  • C) The parent process will automatically be killed
  • D) The entire system will shut down

28. How can a process in Elixir be isolated from other processes?

  • A) By using shared memory
  • B) By using process-based concurrency
  • C) By using threads
  • D) By using external services

29. What would happen if a GenServer fails to handle an incoming call or cast?

  • A) The system will ignore the error
  • B) The error will cause the process to crash
  • C) The caller will receive a default response
  • D) The error will be logged and then handled later

30. What is the significance of start_link in GenServer?

  • A) It starts a process and links it to the calling process
  • B) It initializes the state for a GenServer
  • C) It stops the GenServer process
  • D) It terminates the calling process

Answer Key

QnoAnswer (Option with the text)
1C) To manage state and handle asynchronous calls
2C) handle_event
3A) Starts a new process and links it to the caller
4B) To initialize state and return the state
5B) Handles synchronous requests and returns a reply
6A) handle_cast
7B) Stops the process after handling pending messages
8B) Message passing
9C) To handle asynchronous messages and update state
10B) Sends a synchronous message and waits for a reply
11B) It holds data that the GenServer processes use
12A) Success, with no further action needed
13A) It can return an error to stop the GenServer
14A) The process automatically restarts
15B) A collection of processes that manage the life cycle of other processes
16A) It restarts the child process
17B) To restart a child process if it crashes
18B) It allows for better fault tolerance and scalability
19B) The process will automatically be restarted
20B) The process should not be restarted after it fails
21C) handle_call
22C) The process will continue with outdated state
23B) By returning {:error, reason}
24A) A process is a lightweight thread with its own memory space
25A) handle_call is used for synchronous requests, while handle_cast is for asynchronous requests
26A) A process that manages other processes, ensuring they restart on failure
27B) The supervisor will restart the child process
28B) By using process-based concurrency
29B) The error will cause the process to crash
30A) It starts a process and links it to the calling process

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