MCQs on Object-Oriented Programming in Lua | Lua

Master Object-Oriented Programming (OOP) in Lua to build efficient, reusable code. Explore simulating classes with tables, utilizing metatables, implementing inheritance, polymorphism, objects, and methods effectively in Lua scripts.


MCQs: Object-Oriented Programming in Lua

Simulating Classes with Tables and Metatables

  1. How are classes commonly simulated in Lua?
    • a) Using functions
    • b) Using tables
    • c) Using metatables
    • d) Using global variables
  2. What role do metatables play in simulating OOP in Lua?
    • a) They define the behavior of tables.
    • b) They create global variables.
    • c) They execute conditional logic.
    • d) They manage memory allocation.
  3. Which function is used to assign a metatable to a table?
    • a) setmetatable(table, metatable)
    • b) assign_metatable(table, metatable)
    • c) usemetatable(table, metatable)
    • d) apply_metatable(table, metatable)
  4. What is the __index metamethod used for in Lua?
    • a) To handle errors in table indexing
    • b) To define default behavior for missing keys
    • c) To initialize variables
    • d) To lock the table structure
  5. How do you create a constructor in a Lua class simulation?
    • a) Define a function that returns a table
    • b) Define a global variable
    • c) Use new() function directly
    • d) Call init() function automatically
  6. What happens when the __call metamethod is defined in a metatable?
    • a) The table can be called like a function.
    • b) The table is made immutable.
    • c) The table becomes read-only.
    • d) The table can no longer use metamethods.
  7. In Lua, which operator uses the __index metamethod to access a key in a table?
    • a) +
    • b) -
    • c) . (dot operator)
    • d) : (colon operator)
  8. Which metamethod is used to customize equality checks for Lua objects?
    • a) __eq
    • b) __add
    • c) __concat
    • d) __call
  9. What is the result of setting a metatable’s __index to the table itself?
    • a) Infinite recursion
    • b) A cyclic reference error
    • c) A valid table lookup with fallback
    • d) No effect
  10. How can you define a method in a table to simulate an object’s behavior?
    • a) Use the : operator
    • b) Use the . operator
    • c) Use the [] operator
    • d) Use the => operator

Inheritance and Polymorphism

  1. How is inheritance typically achieved in Lua?
    • a) By copying methods from one table to another
    • b) By assigning __index in the child table to the parent table
    • c) By calling inherit() function
    • d) By using global variables
  2. What is polymorphism in the context of Lua programming?
    • a) The ability to reuse the same function names in different contexts
    • b) The process of defining global variables
    • c) The concept of memory allocation
    • d) The behavior of tables without metatables
  3. How do you override a parent method in Lua inheritance?
    • a) Define a method with the same name in the child table
    • b) Modify the parent table directly
    • c) Use super.method_name()
    • d) Use the __call metamethod
  4. What happens if a method is not found in the child table during inheritance?
    • a) Lua throws an error.
    • b) The method is searched in the parent table.
    • c) The method is ignored.
    • d) Lua exits the program.
  5. Which metamethod allows you to customize how Lua performs addition for an object?
    • a) __add
    • b) __index
    • c) __eq
    • d) __call
  6. How do you call a parent method explicitly in Lua inheritance?
    • a) Use ParentTable.method(self, args)
    • b) Use self:method(args)
    • c) Use ParentTable::method(self, args)
    • d) Use method(self, args)
  7. Which keyword is typically used for polymorphism in Lua?
    • a) local
    • b) self
    • c) super
    • d) Lua does not have a specific keyword for polymorphism.
  8. What does the :new() method typically do in Lua OOP?
    • a) Creates a new object instance
    • b) Deletes a table
    • c) Resets the state of a table
    • d) Sets the __index metamethod
  9. What is the result of using multiple inheritance in Lua?
    • a) Undefined behavior without explicit handling
    • b) Automatic conflict resolution
    • c) Error during execution
    • d) Multiple methods being overridden
  10. How do you ensure shared behavior among multiple Lua objects?
    • a) Use shared metatables
    • b) Copy methods manually
    • c) Avoid using metatables
    • d) Use global variables

Implementing Objects and Methods

  1. How do you define a method for an object in Lua?
    • a) Use the : syntax when defining the function
    • b) Use the . syntax when defining the function
    • c) Use the [] syntax when defining the function
    • d) Use the => syntax when defining the function
  2. What is the significance of the self parameter in Lua methods?
    • a) It refers to the current object instance.
    • b) It refers to the parent table.
    • c) It refers to the global table.
    • d) It refers to a new table.
  3. What is the difference between . and : in Lua?
    • a) : implicitly passes self to the function, whereas . does not.
    • b) : is for inheritance, and . is for polymorphism.
    • c) : works with metatables only, and . works with global tables.
    • d) There is no difference.
  4. What happens if you forget to include self in a method definition?
    • a) The method will not have access to the object instance.
    • b) Lua will throw an error.
    • c) The method will call the parent method.
    • d) Lua will implicitly include self.
  5. Which method is typically used to initialize object properties in Lua?
    • a) new()
    • b) init()
    • c) __init()
    • d) setup()
  6. How do you add a new method to an existing Lua object?
    • a) Assign the function directly to the object.
    • b) Use add_method() function.
    • c) Call append_method() on the object.
    • d) Create a new metatable.
  7. How can you prevent direct modification of an object’s properties in Lua?
    • a) Use the __newindex metamethod.
    • b) Use the __call metamethod.
    • c) Use the __index metamethod.
    • d) Set the table as immutable.
  8. What is the benefit of using metatables in Lua for object implementation?
    • a) They enable custom behavior and method delegation.
    • b) They increase execution speed.
    • c) They simplify syntax.
    • d) They ensure global access.
  9. How do you associate an object with a class in Lua?
    • a) Use setmetatable() with the class’s metatable.
    • b) Use attach() function.
    • c) Use link() function.
    • d) Use set() function.
  10. What is the output of calling an object method using the : operator?
    • a) The method is called with the object instance as the first argument.
    • b) The method is called without arguments.
    • c) The method is called with the parent table.
    • d) The method cannot be called with the : operator.

Answer Table

QnoAnswer (Option with Text)
1b) Using tables
2a) They define the behavior of tables.
3a) setmetatable(table, metatable)
4b) To define default behavior for missing keys
5a) Define a function that returns a table
6a) The table can be called like a function.
7c) . (dot operator)
8a) __eq
9a) Infinite recursion
10a) Use the : operator
11b) By assigning __index to the parent table
12a) The ability to reuse the same function names
13a) Define a method with the same name
14b) The method is searched in the parent table
15a) __add
16a) Use ParentTable.method(self, args)
17d) Lua does not have a specific keyword
18a) Creates a new object instance
19a) Undefined behavior without explicit handling
20a) Use shared metatables
21a) Use the : syntax when defining the function
22a) It refers to the current object instance.
23a) : implicitly passes self to the function
24a) The method will not have access to the object instance.
25a) new()
26a) Assign the function directly to the object.
27a) Use the __newindex metamethod.
28a) They enable custom behavior and method delegation.
29a) Use setmetatable() with the class’s metatable.
30a) The method is called with the object instance as the first argument.

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