MCQs on Metaprogramming and Advanced Patterns | Lua

Writing Code that Generates Code

  1. What is metaprogramming in Lua?
    • A. Writing functions that modify code
    • B. Writing code that generates other code
    • C. Debugging code dynamically
    • D. Writing libraries for external use
  2. Which function allows you to generate dynamic code in Lua?
    • A. eval()
    • B. loadstring()
    • C. dofile()
    • D. loadfile()
  3. What is the role of the loadstring() function in Lua?
    • A. Executes a Lua script from a file
    • B. Compiles a Lua code string into a function
    • C. Loads a binary file
    • D. Saves a function as a string
  4. What can loadstring() help create in Lua?
    • A. A static variable
    • B. A function at runtime
    • C. A table initializer
    • D. An object
  5. Which of the following is a typical use of metaprogramming in Lua?
    • A. To create fixed classes
    • B. To create reusable libraries
    • C. To generate repetitive code blocks dynamically
    • D. To handle syntax errors
  6. In Lua, how would you define a dynamic function name?
    • A. Using function keyword only
    • B. Using loadstring() to define a function
    • C. Using local inside function
    • D. Defining functions at the top of the script
  7. What does the function setmetatable() do in Lua metaprogramming?
    • A. Creates dynamic functions
    • B. Modifies existing functions
    • C. Sets the environment for a function
    • D. Changes the behavior of tables
  8. What does the Lua function getmetatable() return?
    • A. The metadata of a variable
    • B. The environment of a function
    • C. The function’s source code
    • D. The metatable of a table
  9. What is the primary advantage of code generation in Lua?
    • A. It simplifies debugging
    • B. It makes code more reusable
    • C. It helps in increasing code size
    • D. It enhances code readability

Dynamic Function Creation

  1. What is dynamic function creation in Lua?
  • A. Creating a function whose implementation is fixed
  • B. Creating a function during runtime
  • C. Writing a function in a text file
  • D. Using a function to manipulate variables
  1. Which Lua construct is often used for dynamically creating functions?
  • A. function keyword
  • B. loadstring()
  • C. io.open()
  • D. require()
  1. How can you pass a dynamically created function as a parameter in Lua?
  • A. As a string
  • B. By calling it inside a do block
  • C. As a function reference
  • D. By using setmetatable()
  1. When would dynamic function creation be useful in Lua?
  • A. When the function body is predefined
  • B. When the function needs to handle various behaviors at runtime
  • C. When the code requires static analysis
  • D. When optimizing for performance
  1. What is the benefit of creating functions dynamically in Lua?
  • A. Increases code repetition
  • B. Reduces code readability
  • C. Allows for flexible function behaviors
  • D. Decreases performance
  1. What is the result of calling a dynamically created Lua function with loadstring()?
  • A. An error is thrown
  • B. The function is executed immediately
  • C. The function is compiled and returned
  • D. The code is printed to the screen
  1. Which Lua statement is used to dynamically create functions from strings?
  • A. load()
  • B. loadstring()
  • C. dofile()
  • D. loadfile()
  1. What would you expect from this Lua code:
    local f = loadstring("return function() return 42 end")()?
  • A. A syntax error
  • B. A function returning 42
  • C. A number 42 printed
  • D. A table with 42
  1. How can dynamic functions improve the flexibility of a Lua program?
  • A. By avoiding the need for function arguments
  • B. By allowing for runtime function customization
  • C. By increasing the number of predefined functions
  • D. By simplifying the code structure

Advanced Table Manipulation Patterns

  1. What is a metatable used for in Lua?
  • A. To define the method for manipulating strings
  • B. To modify table behavior and operations
  • C. To create dynamic arrays
  • D. To store constant values
  1. How do you override the + operator for tables in Lua?
  • A. Using the __add metamethod
  • B. Using setmetatable()
  • C. Using table.insert()
  • D. Using loadstring()
  1. What is the __index metamethod used for in Lua?
  • A. To set default values for undefined table keys
  • B. To return the type of the table
  • C. To calculate the length of the table
  • D. To create new table entries
  1. Which Lua function helps you create a table with a metatable?
  • A. table.concat()
  • B. setmetatable()
  • C. getmetatable()
  • D. table.insert()
  1. In Lua, what is a weak table?
  • A. A table that cannot be modified
  • B. A table where references to objects are weak
  • C. A table that stores functions
  • D. A table with null values
  1. What is the advantage of using weak tables in Lua?
  • A. It ensures memory is never freed
  • B. It prevents table references from being garbage collected
  • C. It allows automatic memory management
  • D. It stores non-primitive data types
  1. What is the purpose of __call metamethod in Lua tables?
  • A. To enable tables to be invoked as functions
  • B. To set the table’s metatable
  • C. To calculate the table’s length
  • D. To perform table assignments
  1. How would you handle unknown keys in a Lua table using metatables?
  • A. Define a __default metamethod
  • B. Use __index metamethod
  • C. Use __newindex metamethod
  • D. Use table.insert()
  1. What is the result of this Lua code:
    local t = {} setmetatable(t, {__index = function(t, key) return "default" end}) print(t.someKey)?
  • A. Prints nil
  • B. Prints default
  • C. Prints someKey
  • D. Prints an error
  1. What is the purpose of the __newindex metamethod in Lua?
  • A. To define the behavior of accessing table values
  • B. To define the behavior of adding new keys
  • C. To calculate table length
  • D. To delete table entries
  1. What does the function table.insert() do in Lua?
  • A. Adds an element to the beginning of a table
  • B. Removes an element from a table
  • C. Adds an element to the end of a table
  • D. Modifies a table entry
  1. Which operation would you use to define an advanced table manipulation that combines tables?
  • A. __add metamethod
  • B. table.concat()
  • C. table.merge()
  • D. table.copy()

Answer Table

QnoAnswer (Option with the text)
1B. Writing code that generates other code
2B. loadstring()
3B. Compiles a Lua code string into a function
4B. A function at runtime
5C. To generate repetitive code blocks dynamically
6B. Using loadstring() to define a function
7D. Changes the behavior of tables
8D. The metatable of a table
9B. It makes code more reusable
10B. Creating a function during runtime
11B. loadstring()
12C. As a function reference
13B. When the function needs to handle various behaviors at runtime
14C. Allows for flexible function behaviors
15C. The function is compiled and returned
16B. loadstring()
17B. A function returning 42
18B. By allowing for runtime function customization
19B. To modify table behavior and operations
20A. Using the __add metamethod
21A. To set default values for undefined table keys
22B. setmetatable()
23B. A table where references to objects are weak
24C. It allows automatic memory management
25A. To enable tables to be invoked as functions
26B. Use __index metamethod
27B. Prints default
28B. To define the behavior of adding new keys
29C. Adds an element to the end of a table
30A. __add metamethod

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