Elixir programming offers powerful constructs for working with advanced data types and pattern matching. It allows pattern matching on lists, tuples, maps, and structs, enabling efficient and clean code. Concepts like recursion, tail call optimization, and working with nested data structures further enhance Elixir’s capabilities. Additionally, the Map and Keyword modules provide useful functionalities for manipulating data collections. Below are 30 multiple-choice questions (MCQs) to test your understanding of Elixir’s advanced data types and pattern matching.
1. Pattern Matching with Lists, Tuples, Maps, and Structs
Which of the following is true about pattern matching in Elixir? a) It only works for lists b) It works for lists, tuples, maps, and structs c) It works only with tuples d) It works only for numeric values
How do you pattern match a list with an element and the rest of the list in Elixir? a) [head | tail] b) [head] | tail c) {head | tail} d) head, tail[]
How would you pattern match a tuple with two elements in Elixir? a) {x, y} b) {x; y} c) [x, y] d) {x, y, z}
What is the result of the pattern match {a, b} = {1, 2} in Elixir? a) a is 2, b is 1 b) a is 1, b is 2 c) The match will fail d) The result is {a, b}
How do you pattern match a map with a specific key in Elixir? a) %{key: value} b) %{key => value} c) {key: value} d) {key, value}
Which of these is the correct way to pattern match a struct in Elixir? a) %Struct{field: value} b) %{Struct, field: value} c) %Struct{field} d) Struct{field: value}
How does Elixir handle non-matching pattern matches? a) It throws an exception b) It returns nil c) It proceeds with the next match d) It raises a MatchError
Which of the following data types can be used for pattern matching in Elixir? a) Lists only b) Tuples and structs only c) Maps, lists, tuples, and structs d) None of the above
How do you pattern match a map with an optional key in Elixir? a) %{key: value, optional_key: value} b) %{optional_key? => value} c) %{optional_key => value} d) %{key} = %{optional_key}
How do you match the first element of a list while ignoring the rest in Elixir? a) [head | _] b) [head] c) {head, _} d) [_ | head]
2. Recursion and Tail Call Optimization
What is recursion in Elixir? a) A process that repeats a task a fixed number of times b) A function that calls itself c) A type of loop d) A method to call another function
Which of the following is true about tail recursion in Elixir? a) It is less efficient than normal recursion b) It optimizes the function to avoid stack overflow c) It reduces the memory used by variables d) It cannot be used in Elixir
How does Elixir handle recursion to prevent stack overflow? a) By using tail call optimization b) By using an iterative approach c) By limiting the number of recursive calls d) By splitting the recursion into smaller tasks
Which of these is an example of a tail-recursive function in Elixir? a) def factorial(n) do if n == 0, do: 1, else: n * factorial(n - 1) end b) def factorial(n, acc \\ 1) do if n == 0, do: acc, else: factorial(n - 1, n * acc) end c) def factorial(n) do factorial(n - 1) end d) def factorial(n) do n * factorial(n - 1) end
Why is tail call optimization important in Elixir? a) It reduces memory consumption b) It prevents stack overflow in recursion c) It speeds up recursive functions d) All of the above
What happens if a recursive function is not tail-recursive in Elixir? a) It will cause stack overflow for large inputs b) It will always work without issue c) It will run slower but still work d) It will throw a runtime error
In Elixir, which of the following can cause a stack overflow? a) Tail-recursive functions b) Non-tail-recursive functions with too many recursive calls c) Iterative loops d) Pattern matching
How can Elixir’s recursion be optimized? a) By making it tail-recursive b) By using loops instead c) By splitting it into multiple processes d) By reducing the number of function calls
What is the main advantage of using tail-recursive functions over normal recursion in Elixir? a) They allow the function to keep its stack frame and avoid consuming too much memory b) They simplify the code c) They improve performance in non-recursive functions d) They allow functions to return multiple values
How can you test if a function in Elixir is tail-recursive? a) Check if it uses pattern matching b) Check if the recursive call is the last operation in the function c) Check if it uses an iterative approach d) There is no way to test
3. Working with Tuples and Nested Data Structures
How do you access the second element of a tuple in Elixir? a) tuple[1] b) tuple.1 c) tuple(2) d) tuple[2]
What is a tuple used for in Elixir? a) Storing a collection of elements of different types b) Storing a collection of elements of the same type c) Storing key-value pairs d) Storing only integers
How do you pattern match a tuple in Elixir? a) {a, b} b) [a, b] c) tuple(a, b) d) (a, b)
Which of the following is a correct way to create a nested tuple in Elixir? a) {1, {2, 3}} b) (1, {2, 3}) c) [1, {2, 3}] d) {1: {2, 3}}
How do you update a value inside a nested tuple in Elixir? a) By using pattern matching b) By reassigning the entire tuple c) By modifying the tuple directly d) Tuples cannot be updated in Elixir
How do you access a nested element in a tuple like {1, {2, 3}}? a) tuple.1.2 b) tuple[1][2] c) tuple.1[2] d) tuple[1].2
What is the best use case for using a tuple in Elixir? a) When the data structure needs to be immutable and fixed-size b) When the data structure needs to be dynamic and resizable c) When you need to store key-value pairs d) When you need to perform operations on large sets of data
Can you perform pattern matching on nested data structures in Elixir? a) Yes, you can pattern match on nested tuples, lists, and maps b) No, pattern matching is only allowed on flat structures c) Only tuples can be nested and pattern matched d) No, nested pattern matching is not supported
Which of these is a valid pattern match for the tuple {1, {2, 3}} in Elixir? a) {1, {x, y}} b) {x, {1, 2}} c) {1, 2, 3} d) {x, y}
How can you work with nested maps in Elixir? a) By pattern matching and updating the map keys b) By flattening the map c) By using tuples inside maps d) By using lists inside maps
Answer Key:
Qno
Answer
1
b) It works for lists, tuples, maps, and structs
2
a) `[head
3
a) {x, y}
4
b) a is 1, b is 2
5
a) %{key: value}
6
a) %Struct{field: value}
7
d) It raises a MatchError
8
c) Maps, lists, tuples, and structs
9
a) %{key: value, optional_key: value}
10
a) `[head
11
b) A function that calls itself
12
b) It optimizes the function to avoid stack overflow
13
a) By using tail call optimization
14
b) def factorial(n, acc \\ 1) do if n == 0, do: acc, else: factorial(n - 1, n * acc) end
15
b) It prevents stack overflow in recursion
16
a) It will cause stack overflow for large inputs
17
b) Non-tail-recursive functions with too many recursive calls
18
a) By making it tail-recursive
19
d) All of the above
20
b) Check if the recursive call is the last operation in the function
21
b) tuple.1
22
a) Storing a collection of elements of different types
23
a) {a, b}
24
a) {1, {2, 3}}
25
b) By reassigning the entire tuple
26
b) tuple[1][2]
27
a) When the data structure needs to be immutable and fixed-size
28
a) Yes, you can pattern match on nested tuples, lists, and maps