MCQs on Modules and Functionality | Elixir

Elixir is a powerful and dynamic programming language that emphasizes functional programming. It offers easy ways to create custom modules, define functions, and document code. Understanding the keywords like use, import, and alias, and distinguishing between private and public functions, are essential for mastering Elixir. This quiz covers these key concepts to help you better understand Elixir’s modularity and functionality.

MCQs: Modules and Functionality | Elixir Programming

Writing Custom Modules

  1. How do you define a custom module in Elixir?
    • a) defmodule MyModule do
    • b) module MyModule
    • c) create module MyModule
    • d) module MyModule do
  2. What is the correct way to define a function inside a module in Elixir?
    • a) def function_name do ... end
    • b) function function_name do ... end
    • c) def my_function { ... }
    • d) create function_name do ... end
  3. How would you call a function my_function in the module MyModule?
    • a) MyModule.my_function()
    • b) my_function.MyModule()
    • c) my_function()
    • d) call MyModule.my_function()
  4. In Elixir, how do you define a module that includes other modules?
    • a) defmodule MyModule use AnotherModule do
    • b) defmodule MyModule do use AnotherModule end
    • c) defmodule MyModule extend AnotherModule do
    • d) module MyModule with AnotherModule
  5. Which keyword is used to create a module in Elixir?
    • a) module
    • b) defmodule
    • c) create module
    • d) begin module

Documentation with @moduledoc, @doc, @spec

  1. What is the purpose of @moduledoc in Elixir?
    • a) It defines a function’s purpose.
    • b) It documents the entire module.
    • c) It specifies the module name.
    • d) It defines the version of the module.
  2. How do you document a function using @doc in Elixir?
    • a) @doc "Function description"
    • b) def function_name do @doc "Description" end
    • c) @doc function_name
    • d) def function_name do |doc|
  3. What is the purpose of @spec in Elixir?
    • a) To specify a module’s interface
    • b) To specify the type of function arguments and return value
    • c) To specify function names
    • d) To set the function’s version
  4. What is the correct syntax to add documentation to a function in Elixir?
    • a) def my_function, do: ... @doc "Function description"
    • b) def my_function @doc "Function description" do ... end
    • c) def my_function() @doc "Function description"
    • d) @doc "Function description" def my_function do ... end
  5. How does @spec help in Elixir?
    • a) It ensures the function adheres to its documentation.
    • b) It validates the type of arguments and return values.
    • c) It documents the parameters of the function.
    • d) It tests the module’s functions.

Private vs Public Functions

  1. In Elixir, how do you define a public function?
  • a) def my_function do ... end
  • b) private def my_function do ... end
  • c) def public my_function do ... end
  • d) export def my_function do ... end
  1. Which keyword is used to define a private function in Elixir?
  • a) private def my_function do ... end
  • b) def my_function do ... end
  • c) defp my_function do ... end
  • d) module def my_function do ... end
  1. Can a private function in Elixir be called outside the module?
  • a) Yes, it can be called using a special syntax.
  • b) Yes, but only from other modules.
  • c) No, private functions cannot be accessed outside the module.
  • d) No, unless using the use keyword.
  1. Which function is accessible from outside the module in Elixir?
  • a) Functions defined with defp
  • b) Functions defined with def
  • c) Functions defined with private def
  • d) Functions inside the @spec
  1. How can you call a private function from within the same module in Elixir?
  • a) MyModule.my_private_function()
  • b) my_private_function()
  • c) call my_private_function()
  • d) You cannot call private functions even within the same module.

The use, import, and alias Keywords

  1. What does the use keyword do in Elixir?
  • a) It allows importing functions from another module.
  • b) It loads macros and other code from a module.
  • c) It defines an alias for another module.
  • d) It imports a specific function into the current module.
  1. How does the import keyword work in Elixir?
  • a) It imports the whole module’s functions and macros into the current module.
  • b) It only imports functions and ignores macros.
  • c) It imports a specific function from a module.
  • d) It creates an alias for the module.
  1. What does the alias keyword do in Elixir?
  • a) It imports a module’s functions.
  • b) It defines a shorter name for a module.
  • c) It creates an alias for a specific function.
  • d) It loads external modules into the program.
  1. How would you import a specific function my_function from a module MyModule in Elixir?
  • a) import MyModule.my_function
  • b) import MyModule, only: [my_function: 0]
  • c) use MyModule, my_function
  • d) alias MyModule.my_function
  1. What is the difference between use and import in Elixir?
  • a) use loads a module, while import only brings in functions.
  • b) use imports specific functions, while import loads the entire module.
  • c) use defines aliases, and import loads macros.
  • d) There is no difference.
  1. What will the following Elixir code do: use MyModule?
  • a) It imports all functions from MyModule into the current module.
  • b) It creates a shortcut for MyModule.
  • c) It loads the macros defined in MyModule.
  • d) It imports only the my_function from MyModule.
  1. What does the alias keyword allow you to do in Elixir?
  • a) Create a shortcut for a long module name.
  • b) Import specific functions from a module.
  • c) Use a function from another module directly.
  • d) Define a module within another module.
  1. How do you use the use keyword correctly in Elixir?
  • a) use MyModule inside a module.
  • b) use MyModule my_function to call the function.
  • c) use MyModule, only: [my_function: 0]
  • d) use MyModule do for defining functions.
  1. Which keyword would you use to bring in functions from another module into the current scope in Elixir?
  • a) alias
  • b) use
  • c) import
  • d) def
  1. What happens when you use import MyModule in Elixir?
  • a) All functions and macros from MyModule are available without needing to prefix with the module name.
  • b) It imports only the my_function from MyModule.
  • c) It creates an alias for MyModule.
  • d) It imports the module but ignores macros.
  1. How can you alias a module called MyLongModuleName in Elixir?
  • a) alias MyLongModuleName as MyModule
  • b) alias MyLongModuleName
  • c) import MyLongModuleName
  • d) use MyLongModuleName
  1. Which of the following can be imported in Elixir?
  • a) A single function from a module
  • b) All functions from a module
  • c) A macro from a module
  • d) All of the above
  1. What is the result of using the use keyword in Elixir?
  • a) It defines an alias for a module.
  • b) It includes the functionality of a module, typically macros.
  • c) It imports functions into the module.
  • d) It imports specific functions from a module.
  1. Which of the following keywords is used to define a shortcut for a module?
  • a) use
  • b) import
  • c) alias
  • d) def
  1. Can you alias a module and still call its functions directly?
  • a) Yes, after aliasing, you can call functions without using the full module name.
  • b) No, aliasing only defines a shortcut for the module.
  • c) Yes, but only with a specific syntax.
  • d) No, aliasing has no effect.

Answer Table

QnoAnswer (Option with Text)
1a) defmodule MyModule do
2a) def function_name do ... end
3a) MyModule.my_function()
4b) defmodule MyModule do use AnotherModule end
5b) defmodule
6b) It documents the entire module.
7a) @doc "Function description"
8b) To specify the type of function arguments and return value
9d) @doc "Function description" def my_function do ... end
10b) It validates the type of arguments and return values.
11a) def my_function do ... end
12c) defp my_function do ... end
13c) No, private functions cannot be accessed outside the module.
14b) Functions defined with def
15b) You cannot call private functions even within the same module.
16b) It loads macros and other code from a module.
17a) It imports the whole module’s functions and macros into the current module.
18b) It defines a shorter name for a module.
19b) import MyModule, only: [my_function: 0]
20a) use loads a module, while import only brings in functions.
21a) It imports all functions from MyModule into the current module.
22a) Create a shortcut for a long module name.
23a) use MyModule inside a module.
24c) import
25a) All functions and macros from MyModule are available without needing to prefix with the module name.
26a) alias MyLongModuleName as MyModule
27d) All of the above
28b) It includes the functionality of a module, typically macros.
29c) alias
30a) Yes, after aliasing, you can call functions without using the full module name.

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