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.
if statement in Elixir do? a) Executes a block of code if a condition is trueunless statement in Elixir? a) unless condition do ... endif not condition do ... endunless do condition endif condition else do ... endcond statement differ from if in Elixir? a) cond can have multiple conditionscond only checks for boolean valuescond is used for pattern matchingcond is only used inside functionsif-elseunlesscondcasecond block match in Elixir? a) An error is raisednil value is returnedcase statement in Elixir? a) To match against a valuecond? a) elsecatchdefaulttrueif statement when the condition is false in Elixir? a) The code block will not be executedelse block is executedcond? a) A boolean expressioncase statement in Elixir? a) It works similarly to a switch statement in other languagesreceive statement do? a) It waits for a message in a process’s mailboxreceive block in Elixir? a) receive do msg -> IO.puts(msg) endreceive msg do IO.puts(msg) endreceive do msg IO.puts(msg) endreceive msg endreceive is called in Elixir? a) The process raises an errorelse blockreceive block in Elixir? a) receive do {:ok, msg} -> IO.puts(msg) endreceive {:ok, msg} do IO.puts(msg) endreceive do msg {:ok, msg} -> IO.puts(msg) endreceive msg {:ok} -> IO.puts(msg) endreceive block when no matching message is found? a) It throws an exceptionreceive blocks, case, and condreceive block when multiple patterns are present? a) It executes the first matching patternafter keyword do in a receive block in Elixir? a) It allows you to set a timeout after which the block executesreceive block in Elixir be used with timeouts? a) By using receive timeoutafter in the receive blockprocess_timeoutfor comprehension in Elixir do? a) It loops over a collection and applies a block of code to each itemfor comprehension in Elixir? a) for item in collection do item * 2 endfor item in collection item * 2for item <- collection do item * 2 endfor item : collection do item * 2 endfor comprehension, how can you filter elements from a collection based on a condition? a) By using the when keywordelse keywordcase keywordcond keywordfor comprehension in Elixir? a) A list containing the results of the block applied to each itemfor comprehension? a) By using a tuplefor comprehensionsin keywordfor comprehension in Elixir iterate over maps? a) Yes, it can iterate over the keys or values of a mapfor comprehensions are only for listsinto keyword in Elixir’s for comprehension? a) It allows transforming the results into a different collection typefor comprehension in Elixir? a) You cannot handle side effects in a for comprehensionreceive block inside the comprehensionafter keywordfor comprehensions in Elixir? a) It makes code more concise and readablefor comprehension work with ranges in Elixir? a) It iterates over each element in the range| Qno | Answer |
|---|---|
| 1 | a) Executes a block of code if a condition is true |
| 2 | a) unless condition do ... end |
| 3 | a) cond can have multiple conditions |
| 4 | c) cond |
| 5 | d) The default clause is executed |
| 6 | a) To match against a value |
| 7 | d) true |
| 8 | a) The code block will not be executed |
| 9 | d) A pattern match |
| 10 | a) It works similarly to a switch statement in other languages |
| 11 | a) It waits for a message in a process’s mailbox |
| 12 | a) receive do msg -> IO.puts(msg) end |
| 13 | b) The process suspends execution until a message arrives |
| 14 | a) receive do {:ok, msg} -> IO.puts(msg) end |
| 15 | b) It waits indefinitely until a matching message is found |
| 16 | a) Matching values against predefined patterns to simplify logic |
| 17 | b) It can be used in receive blocks, case, and cond |
| 18 | a) It executes the first matching pattern |
| 19 | a) It allows you to set a timeout after which the block executes |
| 20 | b) By adding after in the receive block |
| 21 | a) It loops over a collection and applies a block of code to each item |
| 22 | c) for item <- collection do item * 2 end |
| 23 | a) By using the when keyword |
| 24 | a) A list containing the results of the block applied to each item |
| 25 | d) By combining ranges with the in keyword |
| 26 | a) Yes, it can iterate over the keys or values of a map |
| 27 | a) It allows transforming the results into a different collection type |
| 28 | b) By calling functions within the comprehension |
| 29 | a) It makes code more concise and readable |
| 30 | a) It iterates over each element in the range |