Elixir is a powerful functional programming language that runs on the Erlang VM, known for its concurrency and fault-tolerant features. Understanding how to define functions, manage public and private functions, use function clauses, guards, and create modules is essential for writing efficient Elixir code. This set of 30 multiple-choice questions (MCQs) will help you test your knowledge and master these core Elixir programming concepts.
MCQs on Functions and Modules in Elixir:
Defining Functions (def and defp)
What keyword is used to define a public function in Elixir?
a) def
b) defp
c) public
d) func
Which of the following is the correct syntax for defining a public function with no arguments in Elixir?
a) def my_function() do end
b) def my_function do end
c) def my_function:() end
d) defp my_function do end
What does the defp keyword do in Elixir?
a) Defines a private function
b) Defines a public function
c) Defines a function with a default value
d) Defines a function for pattern matching
How would you define a function that takes two arguments in Elixir?
a) def my_function(arg1, arg2) do end
b) def my_function(arg1, arg2): do end
c) def my_function(arg1, arg2) end
d) defp my_function(arg1, arg2) do end
Which of the following is a valid function definition in Elixir?
a) def my_function(arg1, arg2) { … }
b) def my_function(arg1, arg2) do … end
c) def function(arg1, arg2) end
d) function my_function(arg1, arg2) end
What does the def keyword allow you to do in Elixir?
a) Define public variables
b) Define public functions
c) Define private functions
d) Define module attributes
How can you define a function with a default parameter in Elixir?
a) def my_function(arg \ default_value) do end
b) def my_function(arg = default_value) do end
c) defp my_function(arg \ default_value) do end
d) def my_function(arg | default_value) do end
Which keyword is used to define a function that is accessible only within the module?
a) def
b) defp
c) private
d) internal
Private and Public Functions
What is the main difference between def and defp in Elixir?
a) def defines private functions, defp defines public functions
b) def defines public functions, defp defines private functions
c) There is no difference between def and defp
d) def is for arguments, defp is for no arguments
What happens if you try to call a private function outside the module it was defined in?
a) The function is called with an error message
b) The function is called successfully
c) An error is raised
d) The function is ignored
How can you access a public function from another module?
a) Using the module’s name directly
b) By calling the function with import
c) By defining the function in the same module
d) By using defp
Can a private function be accessed from another module in Elixir?
a) Yes, if the module is imported
b) Yes, if the module is aliased
c) No, private functions are strictly internal to the module
d) Yes, if the function is exported
Which of the following is true about private functions?
a) They can be called from other modules using import
b) They are only accessible inside the module in which they are defined
c) They can be accessed by their name in any module
d) They are similar to public functions but cannot accept arguments
Which keyword is used to mark a function as private in Elixir?
a) private
b) defp
c) defprivate
d) internal
How do you ensure a function is only accessible within its module in Elixir?
a) Define it with defp
b) Use def with an alias
c) Use def and add public in the function definition
d) Use def in a private scope block
Function Clauses and Guards
What is the purpose of function clauses in Elixir?
a) To define functions for different modules
b) To define different behaviors for the same function based on arguments
c) To create private functions only
d) To define default parameters for functions
How do you define multiple clauses for the same function in Elixir?
a) By using the when keyword
b) By defining the function with different argument patterns
c) By calling the function in multiple modules
d) By using def only once
What is a guard clause in Elixir?
a) A conditional expression that controls the execution of a function
b) A way to specify default function arguments
c) A special kind of private function
d) A method for enforcing type safety in function arguments
Which of the following is an example of a guard in Elixir?
a) def my_function(arg) when arg > 0 do … end
b) def my_function(arg) do when arg > 0 … end
c) def my_function(arg) guard arg do … end
d) def my_function(arg) if arg > 0 do … end
How would you write a function clause for handling integer input in Elixir?
a) def my_function(1) do … end
b) def my_function(arg when is_integer(arg)) do … end
c) def my_function(int) do … end
d) def my_function(is_integer) do … end
How can you pattern match on a tuple in an Elixir function?
a) def my_function({arg1, arg2}) do … end
b) def my_function(arg1, arg2) do {arg1, arg2} end
c) def my_function(tuple) when tuple is_tuple do … end
d) def my_function(arg1, arg2, arg3) do {arg1, arg2, arg3} end
What is the result of defining multiple clauses with conflicting patterns in Elixir?
a) The first clause is always executed
b) The last clause is always executed
c) An error occurs due to the conflicting clauses
d) The clauses are combined and executed together
Which of the following is the correct way to define a function with a guard that checks if the argument is even in Elixir?
a) def even_function(n) when rem(n, 2) == 0 do … end
b) def even_function(n) guard rem(n, 2) == 0 do … end
c) def even_function(n) when is_even(n) do … end
d) def even_function(n) if rem(n, 2) == 0 do … end
How would you define a function that accepts either a string or a number as input in Elixir?
a) def my_function(arg) when is_bitstring(arg) or is_number(arg) do … end
b) def my_function(arg) when is_string(arg) or is_integer(arg) do … end
c) def my_function(arg) when arg is_string or arg is_integer do … end
d) def my_function(arg) if arg is_string or arg is_number do … end
Which operator is used for pattern matching in Elixir?
a) =
b) ==
c) :=
d) ->
Creating Modules
What keyword is used to define a module in Elixir?
a) module
b) mod
c) defmodule
d) create_module
Which command is used to create a new module in Elixir?
a) defmodule MyModule do end
b) create_module MyModule do end
c) module MyModule do end
d) module MyModule start end
How do you access functions from an external module in Elixir?
a) Using import keyword
b) Using use keyword
c) By calling the function directly with the module name
d) By calling the function with defmodule
How would you include another module within your module to use its functions?
a) By using the import keyword
b) By using the use keyword
c) By using the call keyword
d) By using the defmodule keyword
What is the purpose of the use keyword in Elixir?
a) It defines a new function
b) It includes another module and its functionality
c) It creates a new module
d) It prevents external module usage
Answers:
Qno
Answer
1
a) def
2
b) def my_function do end
3
a) Defines a private function
4
a) def my_function(arg1, arg2) do end
5
b) def my_function(arg1, arg2) do … end
6
b) Define public functions
7
a) def my_function(arg \ default_value) do end
8
b) defp
9
b) def defines public functions, defp defines private functions
10
c) An error is raised
11
a) Using the module’s name directly
12
c) No, private functions are strictly internal to the module
13
b) They are only accessible inside the module in which they are defined
14
b) defp
15
a) Define it with defp
16
b) To define different behaviors for the same function based on arguments
17
b) By defining the function with different argument patterns
18
a) A conditional expression that controls the execution of a function
19
a) def my_function(arg) when arg > 0 do … end
20
b) def my_function(arg when is_integer(arg)) do … end
21
a) def my_function({arg1, arg2}) do … end
22
c) An error occurs due to the conflicting clauses
23
a) def even_function(n) when rem(n, 2) == 0 do … end
24
a) def my_function(arg) when is_bitstring(arg) or is_number(arg) do … end
25
a) =
26
c) defmodule
27
a) defmodule MyModule do end
28
c) By calling the function directly with the module name
29
a) By using the import keyword
30
b) It includes another module and its functionality