MCQs on Functions and Modules | Elixir

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)

  1. What keyword is used to define a public function in Elixir?
    • a) def
    • b) defp
    • c) public
    • d) func
  2. 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
  3. 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
  4. 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
  5. 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
  6. 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
  7. 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
  8. 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

  1. 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
  2. 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
  3. 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
  4. 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
  5. 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
  6. Which keyword is used to mark a function as private in Elixir?
    • a) private
    • b) defp
    • c) defprivate
    • d) internal
  7. 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

  1. 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
  2. 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
  3. 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
  4. 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
  5. 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
  6. 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
  7. 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
  8. 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
  9. 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
  10. Which operator is used for pattern matching in Elixir?
    • a) =
    • b) ==
    • c) :=
    • d) ->

Creating Modules

  1. What keyword is used to define a module in Elixir?
    • a) module
    • b) mod
    • c) defmodule
    • d) create_module
  2. 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
  3. 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
  4. 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
  5. 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:

QnoAnswer
1a) def
2b) def my_function do end
3a) Defines a private function
4a) def my_function(arg1, arg2) do end
5b) def my_function(arg1, arg2) do … end
6b) Define public functions
7a) def my_function(arg \ default_value) do end
8b) defp
9b) def defines public functions, defp defines private functions
10c) An error is raised
11a) Using the module’s name directly
12c) No, private functions are strictly internal to the module
13b) They are only accessible inside the module in which they are defined
14b) defp
15a) Define it with defp
16b) To define different behaviors for the same function based on arguments
17b) By defining the function with different argument patterns
18a) A conditional expression that controls the execution of a function
19a) def my_function(arg) when arg > 0 do … end
20b) def my_function(arg when is_integer(arg)) do … end
21a) def my_function({arg1, arg2}) do … end
22c) An error occurs due to the conflicting clauses
23a) def even_function(n) when rem(n, 2) == 0 do … end
24a) def my_function(arg) when is_bitstring(arg) or is_number(arg) do … end
25a) =
26c) defmodule
27a) defmodule MyModule do end
28c) By calling the function directly with the module name
29a) By using the import keyword
30b) It includes another module and its functionality

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