MCQs on Modules and Packages | Lua

MCQs on Modules and Packages

  1. What is the primary purpose of modules in Lua?
    • A) To organize code into reusable functions
    • B) To store global variables
    • C) To handle user input
    • D) To create user interfaces
  2. Which function is used to load a Lua module?
    • A) load()
    • B) import()
    • C) require()
    • D) module()
  3. How do you define a module in Lua using module()?
    • A) module("module_name")
    • B) module = {}
    • C) module("module_name", {})
    • D) module = "module_name"
  4. What does the require() function do in Lua?
    • A) It loads a module and executes it if not already loaded.
    • B) It imports external libraries.
    • C) It initializes a module.
    • D) It prints the module name.
  5. Which of the following is not part of Lua’s standard package system?
    • A) package.path
    • B) package.loaded
    • C) package.module
    • D) package.preload
  6. What does package.path define in Lua?
    • A) The location where modules are stored
    • B) The paths for loading modules
    • C) The name of the current module
    • D) The environment variables for Lua
  7. How do you import a module named mymodule using require()?
    • A) require("mymodule")
    • B) import("mymodule")
    • C) load("mymodule")
    • D) use("mymodule")
  8. What does package.loaded contain in Lua?
    • A) A table of all loaded modules
    • B) The path to each module
    • C) The code for each module
    • D) The variables used in modules
  9. What happens when you call require() on an already loaded module?
    • A) It reloads the module and re-executes it.
    • B) It returns nil.
    • C) It skips loading the module and returns true.
    • D) It causes an error.
  10. In Lua, how can you make a module’s function accessible outside the module?
  • A) Assign it to the module table.
  • B) Use the export() function.
  • C) Use require() to call it.
  • D) Use the global keyword.
  1. Which of these statements correctly creates a module in Lua?
  • A) module = {}
  • B) module("mymodule", {})
  • C) module("mymodule")
  • D) module = function() end
  1. What is the function of package.preload in Lua?
  • A) It defines custom modules that are loaded before the default package system.
  • B) It stores preloaded variables.
  • C) It loads all modules simultaneously.
  • D) It initializes a global namespace.
  1. How can you add custom module search paths in Lua?
  • A) By modifying package.path
  • B) By using the import() function
  • C) By calling require()
  • D) By modifying package.loaded
  1. What does the following Lua code do: package.path = package.path .. ";/usr/local/lua/?.lua"?
  • A) It adds a new search path for Lua modules.
  • B) It deletes the existing search paths.
  • C) It installs new packages in the system.
  • D) It creates a new module.
  1. What type of value does require() return after loading a module?
  • A) A table with module functions
  • B) A string containing the module name
  • C) An integer representing module size
  • D) A boolean indicating success
  1. What does package.loaded["module_name"] check in Lua?
  • A) If the module is loaded
  • B) The location of the module
  • C) The module’s functions
  • D) If the module is up to date
  1. Which of the following is true about Lua modules?
  • A) They can only be defined in the main script.
  • B) A module must be a table.
  • C) Modules are automatically reloaded each time.
  • D) Modules can only contain functions, not variables.
  1. How can you access a function defined inside a Lua module?
  • A) By calling the function directly.
  • B) By using the require() function.
  • C) By using package.get() function.
  • D) By accessing it through the module table.
  1. What is the role of module() function in Lua?
  • A) It defines a new module and optionally initializes it.
  • B) It imports a module into the global namespace.
  • C) It loads an external Lua script.
  • D) It initializes a package variable.
  1. What does require("mymodule") return when the module mymodule contains a table with a function foo()?
  • A) It returns nil.
  • B) It returns a reference to the foo() function.
  • C) It returns the module table, where foo is a field.
  • D) It runs the foo() function immediately.
  1. What would happen if you redefine package.path in Lua?
  • A) It will load the wrong module.
  • B) It will change the default paths for module searching.
  • C) It will create a new package system.
  • D) It will cause an error.
  1. How does Lua’s require() function handle circular dependencies?
  • A) It causes an infinite loop.
  • B) It prevents loading the second module.
  • C) It handles them automatically by reusing previously loaded modules.
  • D) It throws an error.
  1. What is the default behavior when a Lua module is first required?
  • A) It gets loaded into memory.
  • B) It runs without being stored.
  • C) It creates a new file in the system.
  • D) It overwrites existing files.
  1. How would you add a custom search directory for modules in Lua?
  • A) Modify package.search
  • B) Modify package.path
  • C) Modify require()
  • D) Use import()
  1. Which of the following is NOT true about Lua’s module system?
  • A) Modules must be loaded with require().
  • B) Modules can be created by any user-defined table.
  • C) Lua’s module system automatically handles circular references.
  • D) Modules must always be explicitly reloaded.
  1. What is one common use case for Lua’s module system?
  • A) Storing global variables permanently.
  • B) Organizing reusable code in separate files.
  • C) Managing large datasets in a single file.
  • D) Encrypting functions.
  1. What is a typical first step in creating a Lua module?
  • A) Define a table that will hold the module’s functions.
  • B) Import other modules.
  • C) Write the module code directly to the console.
  • D) Initialize a global variable.
  1. What function would you use to create an alias for a module in Lua?
  • A) alias()
  • B) module()
  • C) require()
  • D) load()
  1. What would you use to ensure a module is loaded only once in Lua?
  • A) Use package.loaded.
  • B) Use require() twice.
  • C) Use load() with a custom check.
  • D) Use module() with a conditional.
  1. How does Lua handle modules that have already been loaded?
  • A) It reloads the module every time.
  • B) It returns the module from package.loaded.
  • C) It reinitializes the module.
  • D) It causes an error.

Answer Table

QnoAnswer
1A) To organize code into reusable functions
2C) require()
3C) module("module_name", {})
4A) It loads a module and executes it if not already loaded.
5C) package.module
6B) The paths for loading modules
7A) require("mymodule")
8A) A table of all loaded modules
9C) It skips loading the module and returns true.
10A) Assign it to the module table.
11B) module("mymodule", {})
12A) It defines custom modules that are loaded before the default package system.
13A) By modifying package.path
14A) It adds a new search path for Lua modules.
15A) A table with module functions
16A) If the module is loaded
17B) A module must be a table.
18D) By accessing it through the module table.
19A) It defines a new module and optionally initializes it.
20C) It returns the module table, where foo is a field.
21B) It will change the default paths for module searching.
22C) It handles them automatically by reusing previously loaded modules.
23A) It gets loaded into memory.
24B) Modify package.path
25D) Modules must always be explicitly reloaded.
26B) Organizing reusable code in separate files.
27A) Define a table that will hold the module’s functions.
28B) module()
29A) Use package.loaded.
30B) It returns the module from package.loaded.

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