MCQs on Metaprogramming | Elixir

Metaprogramming in Elixir allows for the creation of powerful and dynamic code through macros. Macros let developers manipulate code at compile time, providing a way to generate and transform code dynamically. This concept allows for more efficient and reusable code, but also introduces complexity like macro hygiene. Understanding how to use macros properly is essential for writing clean, maintainable Elixir code. Below are 30 multiple-choice questions (MCQs) designed to test your knowledge of Elixir’s metaprogramming features, including macros, code generation, and best practices.

1. Introduction to Macros in Elixir

  1. What is a macro in Elixir used for? a) To execute code at runtime
    b) To perform operations on data
    c) To generate code at compile time
    d) To create variables
  2. In Elixir, macros are written inside which construct? a) defmacro
    b) def
    c) function
    d) do
  3. What is the key difference between a function and a macro in Elixir? a) Functions are evaluated at compile time, macros are evaluated at runtime
    b) Macros are evaluated at compile time, functions are evaluated at runtime
    c) Functions only work with numbers, macros work with strings
    d) There is no difference between them
  4. Which of the following best describes the purpose of macros in Elixir? a) They optimize the runtime performance of code
    b) They allow developers to write code that writes other code
    c) They create new functions during runtime
    d) They simplify the debugging process
  5. How does Elixir handle the expansion of macros? a) By evaluating them at runtime
    b) By compiling them during the build process
    c) By executing them immediately
    d) By generating them at the start of the program
  6. Which module is commonly used for handling macros in Elixir? a) Macro
    b) Code
    c) GenServer
    d) Kernel
  7. What type of data can macros operate on? a) Only numbers
    b) Only strings
    c) Code itself (AST)
    d) Only lists
  8. What does the quote macro do in Elixir? a) It runs the code inside it immediately
    b) It generates code that is expanded at compile time
    c) It prints the code
    d) It stores code for later execution
  9. What is the role of unquote in Elixir macros? a) It runs code that is quoted
    b) It returns the quoted code as is
    c) It unquotes code and evaluates it during macro expansion
    d) It prevents the expansion of quoted code
  10. How do macros differ from normal functions in Elixir? a) Macros are evaluated at compile time, while functions are evaluated at runtime
    b) Macros execute faster than functions
    c) Macros cannot be used inside other functions
    d) Functions can manipulate the abstract syntax tree (AST)

2. Writing and Using Macros

  1. Which keyword is used to define a macro in Elixir? a) def
    b) macro
    c) defmacro
    d) create
  2. What is the correct syntax for using a macro in Elixir? a) macro call_macro()
    b) use macro(:name)
    c) defmacro :name do ... end
    d) defmacro name() do ... end
  3. How are macros invoked in Elixir code? a) By calling the macro as a regular function
    b) By using quote inside a function
    c) By including the macro definition inside the calling function
    d) By using the macro with do blocks
  4. What is the purpose of using __using__ inside a macro? a) To import modules into other modules dynamically
    b) To call a macro within a function
    c) To define a module at runtime
    d) To prevent a macro from being expanded
  5. Which of these best describes the function of the defmacro keyword? a) It is used to define functions at runtime
    b) It is used to define macros that operate on ASTs at compile time
    c) It allows for the creation of new variable types
    d) It is used to add error handling
  6. How would you define a macro that adds two numbers in Elixir? a) defmacro add(x, y) do x + y end
    b) defmacro add(x, y) do {:ok, x + y} end
    c) defmacro add(x, y) do x * y end
    d) defmacro add(x, y) do x + y end
  7. What would happen if you try to call a macro before it is defined in Elixir? a) The program will throw an error at runtime
    b) The macro will be ignored and skipped
    c) The program will throw a compile-time error
    d) The program will run normally
  8. What does quote return in Elixir macros? a) A list
    b) A function
    c) The abstract syntax tree (AST)
    d) The original code
  9. When is the code inside a macro executed? a) When the macro is defined
    b) When the macro is called at runtime
    c) When the macro is expanded at compile time
    d) When the macro is quoted
  10. Which function is used to invoke a quoted expression in Elixir? a) unquote
    b) call
    c) quote
    d) execute

3. Code Generation with Macros

  1. What is one main advantage of using macros for code generation in Elixir? a) They allow code to be written in multiple languages
    b) They enable code to be created dynamically at compile time
    c) They help in debugging errors
    d) They speed up runtime execution
  2. How can macros help in code generation? a) By evaluating expressions at runtime
    b) By creating new code dynamically during compilation
    c) By writing code that interacts with the database
    d) By speeding up code execution
  3. Which of the following is a typical use case for macros in Elixir? a) Writing SQL queries
    b) Generating repetitive code patterns
    c) Interacting with external libraries
    d) Creating visual representations of data
  4. In Elixir, how do you insert dynamic expressions into generated code using macros? a) By using quote
    b) By using unquote
    c) By using defmacro
    d) By using IO.inspect
  5. How do you prevent code generated by macros from being evaluated prematurely? a) By using the quote macro
    b) By using defmacro
    c) By delaying execution with IO.inspect
    d) By defining variables outside the macro
  6. What does the macro defmacro allow you to do in Elixir? a) Generate functions at runtime
    b) Generate code at compile time
    c) Generate modules at runtime
    d) Generate constants
  7. Can macros generate multiple functions in Elixir? a) Yes, they can generate any number of functions
    b) No, they can only generate one function
    c) Yes, but they only generate functions with the same signature
    d) No, macros cannot generate functions
  8. Which of the following is an example of code generation using macros in Elixir? a) defmacro __before_compile__ do ... end
    b) defmacro hello() do IO.puts("Hello, world!") end
    c) defmacro generate() do def hello() do "Hello" end end
    d) defmacro hello() do {:ok, "Hello"} end
  9. How can you dynamically generate a function with multiple arguments in Elixir using macros? a) Use quote to generate the function code
    b) Use defmacro to create the function code dynamically
    c) Use pattern matching within the macro
    d) Both a and b are correct
  10. What role does macro expansion play in code generation? a) It runs the code immediately
    b) It replaces the macro calls with the generated code
    c) It transforms the code into machine code
    d) It delays code execution

Answer Key:

QnoAnswer
1c) To generate code at compile time
2a) defmacro
3b) Macros are evaluated at compile time, functions at runtime
4b) They allow developers to write code that writes other code
5b) By compiling them during the build process
6a) Macro
7c) Code itself (AST)
8b) It generates code that is expanded at compile time
9c) It unquotes code and evaluates it during macro expansion
10a) Macros are evaluated at compile time, while functions are evaluated at runtime
11c) defmacro
12d) defmacro name() do ... end
13a) By calling the macro as a regular function
14a) To import modules into other modules dynamically
15b) It is used to define macros that operate on ASTs at compile time
16d) defmacro add(x, y) do x + y end
17c) The program will throw a compile-time error
18c) The abstract syntax tree (AST)
19c) When the macro is expanded at compile time
20a) unquote
21b) They enable code to be created dynamically at compile time
22b) By creating new code dynamically during compilation
23b) Generating repetitive code patterns
24b) By using unquote
25a) By using the quote macro
26b) Generate code at compile time
27a) Yes, they can generate any number of functions
28c) defmacro generate() do def hello() do "Hello" end end
29d) Both a and b are correct
30b) It replaces the macro calls with the generated code

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