MCQs on Memory Management and Performance Optimization | Lua

Garbage Collection in Lua

  1. What is the primary function of the garbage collector in Lua?
    • A) Free memory for unused variables
    • B) Automatically optimize code execution
    • C) Debugging errors in scripts
    • D) Monitor variable declarations
  2. Which Lua function manually triggers garbage collection?
    • A) collectgarbage()
    • B) gccollect()
    • C) garbageCollect()
    • D) collectMemory()
  3. What is the default behavior of Lua’s garbage collector?
    • A) It runs every time the script ends
    • B) It runs periodically based on memory usage
    • C) It runs only at user request
    • D) It never runs automatically
  4. In Lua, which type of garbage collection algorithm is used by default?
    • A) Reference counting
    • B) Tracing garbage collection
    • C) Mark and sweep
    • D) Copying garbage collection
  5. Which of the following Lua functions can be used to check the status of garbage collection?
    • A) collectstatus()
    • B) collectinfo()
    • C) collectgarbage("count")
    • D) gcstatus()
  6. What happens if the Lua garbage collector is disabled?
    • A) Memory leaks may occur
    • B) The script runs faster
    • C) Variables are automatically destroyed
    • D) Errors will stop the program
  7. What can you do to trigger a manual garbage collection cycle in Lua?
    • A) Call collectgarbage("collect")
    • B) Use collectgarbage("stop")
    • C) Use collectgarbage("pause")
    • D) None of the above
  8. Which command is used to set the maximum heap size for Lua’s garbage collector?
    • A) collectgarbage("maxheap", size)
    • B) collectgarbage("setheap", size)
    • C) collectgarbage("setmax", size)
    • D) collectgarbage("setpause", size)
  9. What does collectgarbage("setpause", value) do?
    • A) Sets how often the garbage collector will run
    • B) Defines when memory is freed
    • C) Sets the maximum memory threshold
    • D) Pauses garbage collection completely
  10. Which of the following is a common drawback of using Lua’s automatic garbage collection?
    • A) It may cause performance issues due to unpredictability
    • B) It leads to memory fragmentation
    • C) It requires manual memory management
    • D) It cannot handle circular references

Profiling and Optimizing Scripts

  1. What is the purpose of profiling in Lua?
    • A) To detect memory leaks
    • B) To measure and improve the performance of code
    • C) To optimize the appearance of the script
    • D) To compress the Lua bytecode
  2. Which Lua function can be used to obtain performance statistics during script execution?
    • A) debug.trace()
    • B) debug.profile()
    • C) debug.getinfo()
    • D) os.time()
  3. What is the role of os.clock() in Lua performance profiling?
    • A) Measures real-time elapsed since script start
    • B) Tracks memory usage over time
    • C) Measures CPU time used by the script
    • D) Returns the current system time
  4. What is the purpose of Lua’s require function in performance optimization?
    • A) To load libraries only when necessary
    • B) To automatically clean up memory
    • C) To improve the readability of code
    • D) To handle user input more efficiently
  5. Which Lua feature can help reduce the memory overhead in a script?
    • A) Using global variables
    • B) Using tables to store data efficiently
    • C) Reducing the number of loops
    • D) Avoiding local variables
  6. Which of the following tools can be used to profile Lua scripts?
    • A) LuaProf
    • B) LuaDebugger
    • C) LuaProfiler
    • D) None of the above
  7. What is the purpose of using the debug library in Lua?
    • A) To handle errors in a script
    • B) To profile and inspect code execution
    • C) To optimize garbage collection
    • D) To format strings efficiently
  8. How can you reduce the number of garbage collection cycles in Lua?
    • A) Use fewer tables in the script
    • B) Avoid creating new objects during execution
    • C) Force garbage collection to occur more often
    • D) Increase the number of local variables
  9. What is a major drawback of using tables as an optimization strategy in Lua?
    • A) Tables increase memory usage due to garbage collection overhead
    • B) Tables cause excessive CPU usage
    • C) Tables require extra code to manage memory manually
    • D) Tables slow down script execution in all cases
  10. Which of the following techniques can help to reduce Lua script execution time?
    • A) Writing shorter code
    • B) Avoiding complex loops and recursion
    • C) Using more global variables
    • D) Calling collectgarbage() frequently

Avoiding Common Performance Pitfalls

  1. What is a common performance issue when working with large tables in Lua?
    • A) Excessive use of memory due to table copying
    • B) High CPU usage due to inefficient looping
    • C) Garbage collection overhead due to object creation
    • D) Increased latency due to global variable access
  2. Which of the following actions can cause Lua to suffer from memory fragmentation?
    • A) Allocating and deallocating memory frequently
    • B) Using local variables instead of global ones
    • C) Calling functions less frequently
    • D) Repeated use of collectgarbage()
  3. What is the impact of using global variables excessively in Lua?
    • A) They can slow down the execution speed due to lookup time
    • B) They decrease memory consumption
    • C) They simplify the debugging process
    • D) They increase code readability
  4. Which of the following is an efficient approach to table manipulation in Lua?
    • A) Using table.insert within a loop
    • B) Using table.remove for large tables
    • C) Pre-allocating the table with a fixed size when possible
    • D) Using tables for all variable types
  5. How can you reduce the overhead of creating tables within loops in Lua?
    • A) Create a new table before each iteration
    • B) Reuse tables outside of the loop
    • C) Use global variables instead of tables
    • D) Avoid using tables in loops entirely
  6. What is a potential performance pitfall when using the string concatenation operator (..) in Lua?
    • A) It uses excessive CPU time when concatenating large strings
    • B) It causes memory leaks in every concatenation
    • C) It decreases the efficiency of garbage collection
    • D) It reduces the readability of the code
  7. Which technique helps minimize the impact of table resizing on performance?
    • A) Avoid resizing tables altogether
    • B) Resize tables frequently during script execution
    • C) Pre-allocate table size or use table size hints
    • D) Use only fixed-size tables
  8. What is the most common cause of memory leaks in Lua?
    • A) Not freeing tables from memory after use
    • B) Too many variables in a global scope
    • C) Not using coroutines properly
    • D) Excessive function calls
  9. What is one way to improve the performance of recursive functions in Lua?
    • A) Avoid using global variables in recursion
    • B) Convert recursion into an iterative solution
    • C) Increase the stack size
    • D) Call collectgarbage() before each recursion
  10. What is a good practice when optimizing a Lua script for better performance?
    • A) Profile the script first, then address performance issues
    • B) Always use global variables for faster lookup
    • C) Avoid using tables for data storage
    • D) Write code without considering optimization

Answers Table

QnoAnswer
1A) Free memory for unused variables
2A) collectgarbage()
3B) It runs periodically based on memory usage
4C) Mark and sweep
5C) collectgarbage("count")
6A) Memory leaks may occur
7A) Call collectgarbage("collect")
8A) collectgarbage("maxheap", size)
9A) Sets how often the garbage collector will run
10A) It may cause performance issues due to unpredictability
11B) To measure and improve the performance of code
12C) debug.getinfo()
13C) Measures CPU time used by the script
14A) To load libraries only when necessary
15B) Using tables to store data efficiently
16A) LuaProf
17B) To profile and inspect code execution
18B) Avoid creating new objects during execution
19A) Tables increase memory usage due to garbage collection overhead
20B) Avoiding complex loops and recursion
21A) Excessive use of memory due to table copying
22A) Allocating and deallocating memory frequently
23A) They can slow down the execution speed due to lookup time
24C) Pre-allocating the table with a fixed size when possible
25B) Reuse tables outside of the loop
26A) It uses excessive CPU time when concatenating large strings
27C) Pre-allocate table size or use table size hints
28A) Not freeing tables from memory after use
29B) Convert recursion into an iterative solution
30A) Profile the script first, then address performance issues

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