MCQs on Basic Data Types and Structures | Elixir

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

  1. 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
  2. What is the result of the following Elixir expression: 3 + 5 * 2?
    • a) 16
    • b) 13
    • c) 11
    • d) 10
  3. How do you define a string in Elixir?
    • a) "Hello"
    • b) 'Hello'
    • c) Hello
    • d) String("Hello")
  4. 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
  5. What is the result of true and false in Elixir?
    • a) true
    • b) false
    • c) nil
    • d) error

Lists, Tuples, Maps

  1. 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])
  2. 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)
  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)
  4. 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}
  5. 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

  1. What is an atom in Elixir?
    • a) A string
    • b) A constant with a name
    • c) A type of variable
    • d) A data structure
  2. Which of the following is an example of an atom in Elixir?
    • a) :ok
    • b) true
    • c) 1
    • d) "ok"
  3. 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
  4. What does the keyword :ok represent in Elixir?
    • a) A variable
    • b) A boolean
    • c) A success value
    • d) A string literal
  5. 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

  1. 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
  2. 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.
  3. What will the following Elixir pattern match return: {:ok, value} = {:ok, 10}?
    • a) {:ok, 10}
    • b) value = 10
    • c) true
    • d) error
  4. 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]
  5. 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.
  6. 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
  7. Which operator is used to define a pattern matching condition in Elixir?
    • a) =
    • b) ==
    • c) ===
    • d) =>
  8. 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]
  9. 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
  10. 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.
  11. Which of the following can be matched using pattern matching in Elixir?
    • a) Lists
    • b) Tuples
    • c) Maps
    • d) All of the above
  12. 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.
  13. 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
  14. Which Elixir feature allows you to use pattern matching for control flow?
    • a) if
    • b) case
    • c) try
    • d) unless
  15. 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

QnoAnswer
1d) Both a and c
2b) 13
3a) "Hello"
4a) true and false
5b) false
6a) [1, 2, 3]
7a) {1, 2, 3}
8c) List.fetch(list, 0)
9a) %{a: 1, b: 2}
10a) %{ "key1" => "value1", "key2" => "value2" }
11b) A constant with a name
12a) :ok
13b) [key1: value1, key2: value2]
14c) A success value
15b) To represent named constants
16b) Matching variables to values or structures
17c) It matches the structure and values of the tuple.
18b) value = 10
19a) `[head
20d) It will raise a MatchError.
21a) Assigns 5 to x
22a) =
23a) head = 1, tail = [2, 3]
24b) Match keys and values in a map
25b) It selects the clause whose pattern matches the input.
26d) All of the above
27b) It raises a MatchError exception.
28c) Both a and b
29b) case
30a) value = "hello"

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