MCQs on Advanced Data Types and Pattern Matching | Elixir

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

  1. 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
  2. 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[]
  3. 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}
  4. 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}
  5. 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}
  6. 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}
  7. 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
  8. 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
  9. 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}
  10. 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

  1. 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
  2. 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
  3. 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
  4. 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
  5. 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
  6. 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
  7. 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
  8. 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
  9. 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
  10. 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

  1. How do you access the second element of a tuple in Elixir? a) tuple[1]
    b) tuple.1
    c) tuple(2)
    d) tuple[2]
  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
  3. How do you pattern match a tuple in Elixir? a) {a, b}
    b) [a, b]
    c) tuple(a, b)
    d) (a, b)
  4. 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}}
  5. 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
  6. 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
  7. 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
  8. 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
  9. 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}
  10. 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:

QnoAnswer
1b) It works for lists, tuples, maps, and structs
2a) `[head
3a) {x, y}
4b) a is 1, b is 2
5a) %{key: value}
6a) %Struct{field: value}
7d) It raises a MatchError
8c) Maps, lists, tuples, and structs
9a) %{key: value, optional_key: value}
10a) `[head
11b) A function that calls itself
12b) It optimizes the function to avoid stack overflow
13a) By using tail call optimization
14b) def factorial(n, acc \\ 1) do if n == 0, do: acc, else: factorial(n - 1, n * acc) end
15b) It prevents stack overflow in recursion
16a) It will cause stack overflow for large inputs
17b) Non-tail-recursive functions with too many recursive calls
18a) By making it tail-recursive
19d) All of the above
20b) Check if the recursive call is the last operation in the function
21b) tuple.1
22a) Storing a collection of elements of different types
23a) {a, b}
24a) {1, {2, 3}}
25b) By reassigning the entire tuple
26b) tuple[1][2]
27a) When the data structure needs to be immutable and fixed-size
28a) Yes, you can pattern match on nested tuples, lists, and maps
29a) {1, {x, y}}
30a) By pattern matching and updating the map keys

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