MCQs on Control Flow and Conditionals | Elixir

Control flow and conditionals in Elixir are vital for handling different execution paths in programs. The language offers several powerful constructs, such as if, unless, cond, and case, for managing conditions. Additionally, pattern matching with receive in processes and the use of for comprehensions further enhance Elixir’s expressiveness. Below are 30 multiple-choice questions (MCQs) to assess your understanding of Elixir’s control flow and conditionals.

1. if, unless, cond, and case

  1. What does the if statement in Elixir do? a) Executes a block of code if a condition is true
    b) Executes a block of code if a condition is false
    c) Matches patterns
    d) Raises an exception
  2. Which of the following is the correct syntax for an unless statement in Elixir? a) unless condition do ... end
    b) if not condition do ... end
    c) unless do condition end
    d) if condition else do ... end
  3. How does the cond statement differ from if in Elixir? a) cond can have multiple conditions
    b) cond only checks for boolean values
    c) cond is used for pattern matching
    d) cond is only used inside functions
  4. Which construct is used in Elixir to check for multiple conditions in a concise way? a) if-else
    b) unless
    c) cond
    d) case
  5. What happens if none of the clauses in a cond block match in Elixir? a) An error is raised
    b) The first clause is executed
    c) A nil value is returned
    d) The default clause is executed
  6. What is the purpose of the case statement in Elixir? a) To match against a value
    b) To compare two values
    c) To execute code in parallel
    d) To execute a block only if a condition is true
  7. Which keyword is used to define an optional “else” clause in cond? a) else
    b) catch
    c) default
    d) true
  8. What is the behavior of the if statement when the condition is false in Elixir? a) The code block will not be executed
    b) The code block will execute with an error
    c) The else block is executed
    d) It causes the program to terminate
  9. In Elixir, which of these statements is not allowed within cond? a) A boolean expression
    b) A tuple
    c) A string
    d) A pattern match
  10. Which of the following is a feature of the case statement in Elixir? a) It works similarly to a switch statement in other languages
    b) It checks only for boolean conditions
    c) It throws an error if no clauses match
    d) It can only have two clauses

2. receive and pattern matching in processes

  1. In Elixir, what does the receive statement do? a) It waits for a message in a process’s mailbox
    b) It checks for incoming HTTP requests
    c) It matches a function’s parameters
    d) It sends a message to another process
  2. Which of the following is a valid way to define a receive block in Elixir? a) receive do msg -> IO.puts(msg) end
    b) receive msg do IO.puts(msg) end
    c) receive do msg IO.puts(msg) end
    d) receive msg end
  3. What happens if there are no messages in a process’s mailbox when receive is called in Elixir? a) The process raises an error
    b) The process suspends execution until a message arrives
    c) The process terminates
    d) The process executes the else block
  4. Which of these is an example of pattern matching in a receive block in Elixir? a) receive do {:ok, msg} -> IO.puts(msg) end
    b) receive {:ok, msg} do IO.puts(msg) end
    c) receive do msg {:ok, msg} -> IO.puts(msg) end
    d) receive msg {:ok} -> IO.puts(msg) end
  5. How does Elixir handle a receive block when no matching message is found? a) It throws an exception
    b) It waits indefinitely until a matching message is found
    c) It returns a default value
    d) It retries the message pattern match
  6. What is pattern matching in Elixir used for? a) Matching values against predefined patterns to simplify logic
    b) To execute a block of code multiple times
    c) To handle exceptions
    d) To create loops
  7. Which of the following is true about pattern matching in Elixir? a) It is only used in function definitions
    b) It can be used in receive blocks, case, and cond
    c) It works only for numeric values
    d) It only works with strings
  8. How does Elixir process messages in a receive block when multiple patterns are present? a) It executes the first matching pattern
    b) It executes all patterns
    c) It chooses the last pattern
    d) It randomly picks a pattern
  9. What does the after keyword do in a receive block in Elixir? a) It allows you to set a timeout after which the block executes
    b) It defines a fallback value
    c) It terminates the process
    d) It resumes execution from the last match
  10. How can a receive block in Elixir be used with timeouts? a) By using receive timeout
    b) By adding after in the receive block
    c) By setting the timeout globally
    d) By calling process_timeout

3. Using for comprehensions

  1. What does the for comprehension in Elixir do? a) It loops over a collection and applies a block of code to each item
    b) It runs a process asynchronously
    c) It filters a collection based on conditions
    d) It executes a block of code repeatedly until a condition is met
  2. Which of the following is the correct syntax for using a for comprehension in Elixir? a) for item in collection do item * 2 end
    b) for item in collection item * 2
    c) for item <- collection do item * 2 end
    d) for item : collection do item * 2 end
  3. In a for comprehension, how can you filter elements from a collection based on a condition? a) By using the when keyword
    b) By using the else keyword
    c) By using the case keyword
    d) By using the cond keyword
  4. What is the return value of a for comprehension in Elixir? a) A list containing the results of the block applied to each item
    b) A single value from the last iteration
    c) A boolean indicating success
    d) The sum of all items in the collection
  5. How can you collect results from multiple ranges in a single for comprehension? a) By using a tuple
    b) By using a list comprehension
    c) By nesting multiple for comprehensions
    d) By combining ranges with the in keyword
  6. Can a for comprehension in Elixir iterate over maps? a) Yes, it can iterate over the keys or values of a map
    b) No, for comprehensions are only for lists
    c) Yes, but only over the keys of a map
    d) No, maps cannot be used with comprehensions
  7. What is the purpose of the into keyword in Elixir’s for comprehension? a) It allows transforming the results into a different collection type
    b) It modifies the original collection
    c) It sets a limit on the number of iterations
    d) It groups results into tuples
  8. How do you handle side effects inside a for comprehension in Elixir? a) You cannot handle side effects in a for comprehension
    b) By calling functions within the comprehension
    c) By using the receive block inside the comprehension
    d) By using the after keyword
  9. What is the key benefit of using for comprehensions in Elixir? a) It makes code more concise and readable
    b) It provides parallel execution of tasks
    c) It automatically handles errors
    d) It generates documentation for loops
  10. How does the for comprehension work with ranges in Elixir? a) It iterates over each element in the range
    b) It skips every other element in the range
    c) It only processes the first element in the range
    d) It selects the range randomly

Answer Key:

QnoAnswer
1a) Executes a block of code if a condition is true
2a) unless condition do ... end
3a) cond can have multiple conditions
4c) cond
5d) The default clause is executed
6a) To match against a value
7d) true
8a) The code block will not be executed
9d) A pattern match
10a) It works similarly to a switch statement in other languages
11a) It waits for a message in a process’s mailbox
12a) receive do msg -> IO.puts(msg) end
13b) The process suspends execution until a message arrives
14a) receive do {:ok, msg} -> IO.puts(msg) end
15b) It waits indefinitely until a matching message is found
16a) Matching values against predefined patterns to simplify logic
17b) It can be used in receive blocks, case, and cond
18a) It executes the first matching pattern
19a) It allows you to set a timeout after which the block executes
20b) By adding after in the receive block
21a) It loops over a collection and applies a block of code to each item
22c) for item <- collection do item * 2 end
23a) By using the when keyword
24a) A list containing the results of the block applied to each item
25d) By combining ranges with the in keyword
26a) Yes, it can iterate over the keys or values of a map
27a) It allows transforming the results into a different collection type
28b) By calling functions within the comprehension
29a) It makes code more concise and readable
30a) It iterates over each element in the range

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