MCQs on Introduction to Processes | Elixir

Elixir is a powerful functional programming language that leverages lightweight concurrent processes for building scalable and fault-tolerant systems. Processes play a central role in Elixir, enabling efficient multitasking and communication. This set of 30 multiple-choice questions (MCQs) will cover Elixir processes, including their creation, message passing, and state management using the Agent module.

MCQs on Introduction to Processes in Elixir

What are Processes in Elixir?

  1. What is a process in Elixir?
    • A) A unit of computation that can run independently
    • B) A variable that holds data
    • C) A loop that executes sequential tasks
    • D) A type of data structure
  2. Which of the following best describes the purpose of processes in Elixir?
    • A) To run tasks concurrently and handle multiple operations
    • B) To store data permanently
    • C) To manage memory allocation
    • D) To execute commands sequentially
  3. What makes Elixir processes lightweight compared to traditional OS-level processes?
    • A) They run in separate threads
    • B) They are managed by the Erlang VM and have minimal overhead
    • C) They are directly controlled by the operating system
    • D) They run in the same thread
  4. How are processes isolated from each other in Elixir?
    • A) Each process has its own memory space and state
    • B) Processes share memory space
    • C) Processes run in the same thread
    • D) Processes are not isolated
  5. What does the Elixir VM (BEAM) provide for processes?
    • A) Automatic garbage collection and process isolation
    • B) Direct interaction with hardware
    • C) Manual memory management
    • D) Thread-based execution

Starting Processes with spawn

  1. What is the purpose of the spawn function in Elixir?
    • A) To create a new process
    • B) To start a new thread
    • C) To kill an existing process
    • D) To manage process state
  2. How do you start a simple process in Elixir using spawn?
    • A) spawn(fn -> IO.puts("Hello") end)
    • B) spawn(Hello)
    • C) spawn(IO.puts("Hello"))
    • D) start_process(fn -> IO.puts("Hello") end)
  3. What does the spawn function return when it successfully creates a process?
    • A) A process identifier (PID)
    • B) A boolean value
    • C) A reference to a new thread
    • D) The process status
  4. How does spawn work in Elixir when used with a function?
    • A) It runs the function asynchronously in a new process
    • B) It runs the function synchronously in the current process
    • C) It creates a new thread to execute the function
    • D) It creates a new process and waits for a return value
  5. Which of the following is true about the spawn function in Elixir?
    • A) It allows multiple processes to run concurrently
    • B) It stops all existing processes
    • C) It only works in a single-threaded environment
    • D) It needs manual memory management

Sending and Receiving Messages

  1. What is the primary communication method between processes in Elixir?
    • A) Message passing
    • B) Shared memory
    • C) Direct function calls
    • D) Using global variables
  2. Which Elixir function is used to send a message to another process?
    • A) send/2
    • B) post_message/2
    • C) message_send/2
    • D) send_message/2
  3. What is the role of the receiving process when a message is sent in Elixir?
    • A) It processes the message asynchronously
    • B) It waits for a function to execute
    • C) It directly alters the sender’s state
    • D) It ignores the message
  4. What is the function used to receive messages in Elixir?
    • A) receive/1
    • B) get_message/1
    • C) listen/1
    • D) receive_message/1
  5. What happens if a process does not receive any messages in Elixir?
    • A) The process waits indefinitely
    • B) The process crashes
    • C) The process terminates
    • D) The process performs a timeout

The Agent module for state management

  1. What is the Agent module used for in Elixir?
    • A) To manage shared state in processes
    • B) To create processes manually
    • C) To send and receive messages
    • D) To handle asynchronous tasks
  2. How do you start an agent in Elixir?
    • A) Agent.start_link(fn -> initial_state end)
    • B) spawn(fn -> initial_state end)
    • C) Agent.new(fn -> initial_state end)
    • D) Agent.create(fn -> initial_state end)
  3. What does the Agent.update function do?
    • A) It modifies the state of an agent
    • B) It sends a message to the agent
    • C) It starts a new process
    • D) It stops an agent
  4. How can you access the state stored in an Agent in Elixir?
    • A) Agent.get(agent_pid, fn state -> state end)
    • B) Agent.retrieve(agent_pid)
    • C) Agent.state(agent_pid)
    • D) Agent.read(agent_pid)
  5. How does the Agent module help with state management in Elixir?
    • A) It allows state to be safely shared between processes
    • B) It stores data in global variables
    • C) It allows for direct memory access
    • D) It synchronizes data between threads

Advanced Process Management in Elixir

  1. Which of the following is a characteristic of processes in Elixir?
    • A) They are lightweight and isolated
    • B) They share memory space with other processes
    • C) They run in the same thread
    • D) They require manual memory management
  2. How does Elixir ensure process isolation?
    • A) Each process has its own memory and state
    • B) Processes share the same memory space
    • C) Processes communicate via shared variables
    • D) Processes access global variables
  3. What happens when a process in Elixir crashes?
    • A) It does not affect other processes
    • B) It causes the entire system to crash
    • C) It requires manual recovery
    • D) It resets the system
  4. What is the primary advantage of process-based concurrency in Elixir?
    • A) Efficient resource management and fault isolation
    • B) Unlimited access to shared memory
    • C) Sequential execution of tasks
    • D) Easy interaction with OS threads
  5. How are processes in Elixir monitored for failure?
    • A) By using a supervisor tree
    • B) By polling the processes periodically
    • C) By locking them with mutexes
    • D) By direct interaction with the OS

Concurrency and Fault Tolerance

  1. How does Elixir handle multiple processes concurrently?
    • A) It uses lightweight processes and the actor model
    • B) It uses a thread-based model
    • C) It runs all processes sequentially
    • D) It relies on shared memory
  2. What is the concept of “let it crash” in Elixir?
    • A) Allowing processes to crash and being recovered by a supervisor
    • B) Preventing any process from crashing
    • C) Ensuring processes run in a tight loop
    • D) Immediately restarting crashed processes
  3. Which Elixir feature enables building fault-tolerant systems?
    • A) Supervisors
    • B) Global state
    • C) Shared memory
    • D) Thread-based concurrency
  4. How can you make an Elixir process send periodic messages to itself?
    • A) By using send(self(), :message)
    • B) By using a receive block with a timer
    • C) By invoking a function recursively
    • D) By using spawn
  5. Which of the following is true about process communication in Elixir?
    • A) Messages are sent asynchronously between processes
    • B) Processes share memory directly
    • C) Processes always communicate synchronously
    • D) Processes cannot communicate with each other

Answers

QnoAnswer
1A) A unit of computation that can run independently
2A) To run tasks concurrently and handle multiple operations
3B) They are managed by the Erlang VM and have minimal overhead
4A) Each process has its own memory space and state
5A) Automatic garbage collection and process isolation
6A) To create a new process
7A) spawn(fn -> IO.puts("Hello") end)
8A) A process identifier (PID)
9A) It runs the function asynchronously in a new process
10A) It allows multiple processes to run concurrently
11A) Message passing
12A) send/2
13A) It processes the message asynchronously
14A) receive/1
15A) The process waits indefinitely
16A) To manage shared state in processes
17A) Agent.start_link(fn -> initial_state end)
18A) It modifies the state of an agent
19A) Agent.get(agent_pid, fn state -> state end)
20A) It allows state to be safely shared between processes
21A) They are lightweight and isolated
22A) Each process has its own memory and state
23A) It does not affect other processes
24A) Efficient resource management and fault isolation
25A) By using a supervisor tree
26A) It uses lightweight processes and the actor model
27A) Allowing processes to crash and being recovered by a supervisor
28A) Supervisors
29B) By using a receive block with a timer
30A) Messages are sent asynchronously between processes

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