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
- How are classes commonly simulated in Lua?
- a) Using functions
- b) Using tables
- c) Using metatables
- d) Using global variables
- 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.
- 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)
- 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
- 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
- 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.
- In Lua, which operator uses the
__index metamethod to access a key in a table?
- a)
+
- b)
-
- c)
. (dot operator)
- d)
: (colon operator)
- Which metamethod is used to customize equality checks for Lua objects?
- a)
__eq
- b)
__add
- c)
__concat
- d)
__call
- 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
- 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
- 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
- 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
- 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
- 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.
- Which metamethod allows you to customize how Lua performs addition for an object?
- a)
__add
- b)
__index
- c)
__eq
- d)
__call
- 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)
- 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.
- 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
- 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
- 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
- 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
- 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.
- 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.
- 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.
- Which method is typically used to initialize object properties in Lua?
- a)
new()
- b)
init()
- c)
__init()
- d)
setup()
- 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.
- 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.
- 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.
- 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.
- 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
| Qno | Answer (Option with Text) |
|---|
| 1 | b) Using tables |
| 2 | a) They define the behavior of tables. |
| 3 | a) setmetatable(table, metatable) |
| 4 | b) To define default behavior for missing keys |
| 5 | a) Define a function that returns a table |
| 6 | a) The table can be called like a function. |
| 7 | c) . (dot operator) |
| 8 | a) __eq |
| 9 | a) Infinite recursion |
| 10 | a) Use the : operator |
| 11 | b) By assigning __index to the parent table |
| 12 | a) The ability to reuse the same function names |
| 13 | a) Define a method with the same name |
| 14 | b) The method is searched in the parent table |
| 15 | a) __add |
| 16 | a) Use ParentTable.method(self, args) |
| 17 | d) Lua does not have a specific keyword |
| 18 | a) Creates a new object instance |
| 19 | a) Undefined behavior without explicit handling |
| 20 | a) Use shared metatables |
| 21 | a) Use the : syntax when defining the function |
| 22 | a) It refers to the current object instance. |
| 23 | a) : implicitly passes self to the function |
| 24 | a) The method will not have access to the object instance. |
| 25 | a) new() |
| 26 | a) Assign the function directly to the object. |
| 27 | a) Use the __newindex metamethod. |
| 28 | a) They enable custom behavior and method delegation. |
| 29 | a) Use setmetatable() with the class’s metatable. |
| 30 | a) The method is called with the object instance as the first argument. |
Post Views: 39