Elixir is a dynamic, functional programming language that is widely used for building scalable and maintainable applications. Understanding Elixir’s basic data types and structures, such as numbers, strings, booleans, lists, tuples, maps, atoms, keywords, and pattern matching, is essential for mastering the language. This quiz covers key concepts to help you learn Elixir’s foundational elements.
MCQs: Basic Data Types and Structures | Elixir Programming
Numbers, Strings, Booleans
Which of the following is a valid way to define a number in Elixir?
a) num = 5
b) num = "5"
c) num = 5.0
d) Both a and c
What is the result of the following Elixir expression: 3 + 5 * 2?
a) 16
b) 13
c) 11
d) 10
How do you define a string in Elixir?
a) "Hello"
b) 'Hello'
c) Hello
d) String("Hello")
Which of the following is used to represent a boolean value in Elixir?
a) true and false
b) yes and no
c) 1 and 0
d) on and off
What is the result of true and false in Elixir?
a) true
b) false
c) nil
d) error
Lists, Tuples, Maps
How do you define a list in Elixir?
a) [1, 2, 3]
b) {1, 2, 3}
c) list(1, 2, 3)
d) list([1, 2, 3])
Which of the following is the correct way to define a tuple in Elixir?
a) {1, 2, 3}
b) [1, 2, 3]
c) (1, 2, 3)
d) tuple(1, 2, 3)
How do you access an element in a list in Elixir?
a) list[0]
b) list.0
c) List.fetch(list, 0)
d) list(0)
What is the result of this Elixir expression: Map.put(%{a: 1}, :b, 2)?
a) %{a: 1, b: 2}
b) %{a: 2, b: 1}
c) {:ok, %{a: 1, b: 2}}
d) %{b: 2, a: 1}
How do you define a map with string keys in Elixir?
a) %{ "key1" => "value1", "key2" => "value2" }
b) %{:key1 => "value1", :key2 => "value2" }
c) %{key1: "value1", key2: "value2"}
d) %{"key1": "value1", "key2": "value2"}
Atoms and Keywords
What is an atom in Elixir?
a) A string
b) A constant with a name
c) A type of variable
d) A data structure
Which of the following is an example of an atom in Elixir?
a) :ok
b) true
c) 1
d) "ok"
How do you define a keyword list in Elixir?
a) [{key1, value1}, {key2, value2}]
b) [key1: value1, key2: value2]
c) {key1: value1, key2: value2}
d) key1: value1, key2: value2
What does the keyword :ok represent in Elixir?
a) A variable
b) A boolean
c) A success value
d) A string literal
What is the purpose of using atoms in Elixir?
a) To represent variables
b) To represent named constants
c) To represent values in maps
d) To represent numbers
Pattern Matching Basics
What is pattern matching in Elixir?
a) Comparing strings
b) Matching variables to values or structures
c) Finding elements in lists
d) Sorting data structures
How does pattern matching work with tuples in Elixir?
a) It checks if the tuple has the same number of elements.
b) It compares the first element of the tuple.
c) It matches the structure and values of the tuple.
d) It performs a deep comparison.
What will the following Elixir pattern match return: {:ok, value} = {:ok, 10}?
a) {:ok, 10}
b) value = 10
c) true
d) error
How do you match an element in a list using pattern matching in Elixir?
a) [head | tail] = [1, 2, 3]
b) list(1, 2, 3)
c) list.head()
d) list[0]
What is the result of the following Elixir expression: {:ok, result} = {:error, "failure"}?
a) {:error, "failure"}
b) result = "failure"
c) error
d) It will raise a MatchError.
What does the expression x = 5 do in Elixir when used in a pattern match?
a) Assigns 5 to x
b) Compares x with 5
c) Matches x with the value 5
d) Creates a new variable x
Which operator is used to define a pattern matching condition in Elixir?
a) =
b) ==
c) ===
d) =>
What is the result of this pattern match: [head | tail] = [1, 2, 3]?
a) head = 1, tail = [2, 3]
b) head = 2, tail = [1, 3]
c) head = 3, tail = [1, 2]
d) head = 1, tail = [1, 2, 3]
What does pattern matching with maps allow you to do in Elixir?
a) Match values in a list
b) Match keys and values in a map
c) Assign values to map keys
d) Match based on string values
How does Elixir handle pattern matching in function clauses?
a) It uses a default match for all cases.
b) It selects the clause whose pattern matches the input.
c) It raises an error if no clause matches.
d) It performs an exhaustive comparison of all clauses.
Which of the following can be matched using pattern matching in Elixir?
a) Lists
b) Tuples
c) Maps
d) All of the above
What happens when a pattern match fails in Elixir?
a) The program continues with the next match.
b) It raises a MatchError exception.
c) It assigns nil to the variables.
d) It skips the pattern match.
How can you match specific values in a tuple using pattern matching?
a) {:ok, result} = {:ok, 10}
b) {:ok, 10} = {:ok, result}
c) Both a and b
d) None of the above
Which Elixir feature allows you to use pattern matching for control flow?
a) if
b) case
c) try
d) unless
In Elixir, what will the pattern match {:ok, value} = {:ok, "hello"} result in?
a) value = "hello"
b) value = :ok
c) error
d) value = "world"
Answer Key
Qno
Answer
1
d) Both a and c
2
b) 13
3
a) "Hello"
4
a) true and false
5
b) false
6
a) [1, 2, 3]
7
a) {1, 2, 3}
8
c) List.fetch(list, 0)
9
a) %{a: 1, b: 2}
10
a) %{ "key1" => "value1", "key2" => "value2" }
11
b) A constant with a name
12
a) :ok
13
b) [key1: value1, key2: value2]
14
c) A success value
15
b) To represent named constants
16
b) Matching variables to values or structures
17
c) It matches the structure and values of the tuple.
18
b) value = 10
19
a) `[head
20
d) It will raise a MatchError.
21
a) Assigns 5 to x
22
a) =
23
a) head = 1, tail = [2, 3]
24
b) Match keys and values in a map
25
b) It selects the clause whose pattern matches the input.