MCQs on Metatables and Metamethods | Lua

1. Introduction to Metatables

  1. What is a metatable in Lua?
    • a) A special table that modifies the behavior of another table
    • b) A type of array
    • c) A function used for table creation
    • d) A variable type used in Lua
  2. How can you set a metatable for a Lua table?
    • a) setmetatable(table, metatable)
    • b) table.metatable = metatable
    • c) table.set(metatable)
    • d) metatable.table = table
  3. Which function is used to get the metatable of a table in Lua?
    • a) getmetatable()
    • b) table.get()
    • c) table.metatable()
    • d) metatable.get()
  4. What happens if you try to access a table without setting its metatable?
    • a) It behaves normally without any modifications
    • b) Lua will throw an error
    • c) It is automatically assigned a default metatable
    • d) It behaves as a string
  5. What is the primary purpose of metatables in Lua?
    • a) To extend the capabilities of tables by modifying their behavior
    • b) To store function definitions
    • c) To define table values
    • d) To keep track of table length
  6. Which of the following is true about metatables?
    • a) They can only be used for arrays
    • b) They allow for modification of table behaviors for operations like indexing
    • c) They must be set globally
    • d) They cannot be used with user-defined types
  7. Can metatables be shared across different tables?
    • a) Yes, multiple tables can reference the same metatable
    • b) No, each table must have its own unique metatable
    • c) Yes, but only within the same block of code
    • d) No, metatables are for internal use only
  8. How can you set a table to inherit from another table in Lua?
    • a) By using the __index metamethod
    • b) By using the setmetatable() function
    • c) By using the __newindex metamethod
    • d) By referencing the parent table directly
  9. What does the setmetatable() function do in Lua?
    • a) Assigns a metatable to a table
    • b) Initializes a new table
    • c) Retrieves the metatable of a table
    • d) Removes the metatable from a table
  10. Can a metatable be assigned to a table dynamically during runtime?
    • a) Yes, metatables can be changed during runtime
    • b) No, metatables must be set at table creation
    • c) Yes, but only once
    • d) No, metatables are fixed

2. Common Metamethods

  1. Which metamethod is used to customize table indexing in Lua?
    • a) __index
    • b) __newindex
    • c) __add
    • d) __sub
  2. What is the purpose of the __add metamethod in Lua?
    • a) It defines how two tables are added together
    • b) It allows adding a value to a table
    • c) It defines how two numbers are added together
    • d) It adds a new index to a table
  3. What does the __index metamethod do?
    • a) It handles the default behavior when a non-existing index is accessed
    • b) It defines how elements are iterated through in a table
    • c) It limits the number of elements a table can have
    • d) It creates new indexes for a table
  4. How does the __newindex metamethod differ from __index?
    • a) __index is used for getting values, __newindex is used for setting values
    • b) __index is used for assigning values
    • c) __newindex cannot be customized
    • d) There is no difference
  5. Which metamethod is used to define how the table behaves when it is added to another table or value?
    • a) __add
    • b) __sub
    • c) __mul
    • d) __div
  6. What is the use of the __call metamethod in Lua?
    • a) It allows a table to be called as a function
    • b) It defines the behavior when a table is indexed
    • c) It handles arithmetic operations
    • d) It allows setting a table as a function
  7. Which metamethod is used to handle table subtraction?
    • a) __sub
    • b) __add
    • c) __mul
    • d) __div
  8. What does the __tostring metamethod do?
    • a) It defines how a table is converted to a string
    • b) It defines how a table is indexed
    • c) It defines how the table is added to another table
    • d) It checks the type of the table
  9. What will happen if you try to use an operator like + on a table without defining the __add metamethod?
    • a) Lua will throw an error
    • b) Lua will perform a default operation
    • c) Lua will call the __index metamethod
    • d) Lua will ignore the operator
  10. What does the __metatable metamethod do?
    • a) It prevents a table’s metatable from being modified
    • b) It allows changing the metatable of a table
    • c) It defines the behavior when a table is called
    • d) It sets the default behavior for tables

3. Overloading Operators with Metamethods

  1. How can you overload the == operator for a table in Lua?
    • a) By defining the __eq metamethod
    • b) By defining the __compare metamethod
    • c) By defining the __equal metamethod
    • d) By defining the __ne metamethod
  2. Which metamethod should you define to overload the * operator in Lua?
    • a) __mul
    • b) __div
    • c) __pow
    • d) __mod
  3. Can you overload the .. operator in Lua using metamethods?
    • a) Yes, by defining the __concat metamethod
    • b) No, the .. operator cannot be overloaded
    • c) Yes, by defining the __add metamethod
    • d) Yes, by defining the __tostring metamethod
  4. Which of the following metamethods is used to handle the >= operator in Lua?
    • a) __le
    • b) __ge
    • c) __gt
    • d) __lt
  5. What happens if you try to use the .. operator on a table without defining the __concat metamethod?
    • a) Lua will throw an error
    • b) Lua will concatenate the table as a string
    • c) Lua will convert the table into a number
    • d) Lua will treat the table as an empty string
  6. Which of the following is a valid operator for which you can define metamethods?
    • a) __lt (less than)
    • b) __for
    • c) __if
    • d) __else
  7. How can you overload the <= operator for a table in Lua?
    • a) By defining the __le metamethod
    • b) By defining the __eq metamethod
    • c) By defining the __lt metamethod
    • d) By defining the __compare metamethod
  8. What does the __div metamethod define in Lua?
    • a) The behavior of division on a table
    • b) The behavior of multiplication on a table
    • c) The behavior of subtraction on a table
    • d) The behavior of addition on a table
  9. How can you define a custom operator for a table in Lua?
    • a) By using metamethods
    • b) By using global variables
    • c) By using functions only
    • d) By using the local keyword
  10. Can you modify the behavior of comparison operators like == and ~= using metamethods in Lua?
    • a) Yes, by defining __eq and __neq
    • b) No, comparison operators cannot be overloaded
    • c) Yes, by defining __lt and __gt
    • d) No, comparison is not allowed with tables

Answer Key

QnoAnswer (Option with the text)
1a) A special table that modifies the behavior of another table
2a) setmetatable(table, metatable)
3a) getmetatable()
4a) It behaves normally without any modifications
5a) To extend the capabilities of tables by modifying their behavior
6b) They allow for modification of table behaviors for operations like indexing
7a) Yes, multiple tables can reference the same metatable
8a) By using the __index metamethod
9a) Assigns a metatable to a table
10a) Yes, metatables can be changed during runtime
11a) __index
12a) It defines how two tables are added together
13a) It handles the default behavior when a non-existing index is accessed
14a) __index is used for getting values, __newindex is used for setting values
15a) __add
16a) It allows a table to be called as a function
17a) __sub
18a) It defines how a table is converted to a string
19a) Lua will throw an error
20a) It prevents a table’s metatable from being modified
21a) By defining the __eq metamethod
22a) __mul
23a) Yes, by defining the __concat metamethod
24a) __le
25a) Lua will throw an error
26a) __lt (less than)
27a) By defining the __le metamethod
28a) The behavior of division on a table
29a) By using metamethods
30a) Yes, by defining __eq and __neq

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