MCQs on Lua Standard Libraries | Lua

Lua’s standard libraries provide powerful tools for tasks like mathematical calculations, string manipulation, and time management. Mastering math, os, table, and string libraries is essential for efficient Lua programming.


1. Exploring Libraries: math, os, table, and string

  1. What does math.floor(3.7) return in Lua?
    a) 4
    b) 3
    c) 3.7
    d) 0
  2. Which function rounds a number up to the nearest integer?
    a) math.ceil
    b) math.floor
    c) math.round
    d) math.abs
  3. What does string.len("Lua") return?
    a) 4
    b) 3
    c) 2
    d) 0
  4. How can you concatenate two strings in Lua?
    a) Using &
    b) Using +
    c) Using ..
    d) Using concat()
  5. What does table.insert({1, 2, 3}, 2, 10) do?
    a) Inserts 10 at the second position
    b) Replaces the second element with 10
    c) Appends 10 at the end
    d) Deletes the second element
  6. What is the role of os.clock() in Lua?
    a) Returns the current date and time
    b) Returns the CPU time used by the program
    c) Returns the system uptime
    d) Returns the current hour
  7. Which of the following functions can format a string in Lua?
    a) string.find
    b) string.format
    c) string.len
    d) string.upper
  8. How do you generate a random number in Lua?
    a) math.random()
    b) math.rand()
    c) random()
    d) os.random()
  9. What does table.remove({1, 2, 3}, 1) do?
    a) Removes the last element
    b) Removes the element at index 1
    c) Clears the entire table
    d) Shifts elements to the right
  10. What does string.match("hello", "h") return?
    a) “h”
    b) “hello”
    c) true
    d) nil

2. Time and Date Manipulation

  1. Which Lua function gets the current date and time?
    a) os.date
    b) os.time
    c) os.now
    d) os.current
  2. What does os.time() return in Lua?
    a) Current date as a string
    b) Current time in seconds since the epoch
    c) Current time in milliseconds
    d) Current year
  3. How can you format the current date in “YYYY-MM-DD” format?
    a) os.format("%Y-%M-%D")
    b) os.date("%Y-%m-%d")
    c) os.time("%Y-%m-%d")
    d) os.now("%Y-%m-%d")
  4. Which Lua library function adds a delay?
    a) os.delay()
    b) os.sleep()
    c) os.time()
    d) Lua does not have a native delay function
  5. What does os.difftime(t1, t2) compute?
    a) The absolute difference between two numbers
    b) The difference in days between two dates
    c) The difference in seconds between two times
    d) The elapsed time of a program
  6. How would you convert a table representing a date back to seconds?
    a) os.to_time(table)
    b) os.time(table)
    c) os.convert(table)
    d) os.date(table)
  7. What is the default format of os.date()?
    a) YYYY-MM-DD
    b) MM/DD/YYYY
    c) Day Month Date HH:MM:SS Year
    d) Unix timestamp
  8. Which function measures the time taken to execute a block of code?
    a) os.clock()
    b) os.date()
    c) os.time()
    d) os.benchmark()
  9. How do you retrieve a Unix timestamp in Lua?
    a) os.timestamp()
    b) os.unix()
    c) os.time()
    d) os.clock()
  10. What is the result of os.date("*t")?
    a) Returns a formatted string of the current date
    b) Returns a table representing the current date and time
    c) Returns the Unix timestamp
    d) Returns the system time

3. Extending the Standard Library

  1. Can you add a custom function to the math library in Lua?
    a) Yes, using math.extend()
    b) No, the library is read-only
    c) Yes, by directly assigning a function to math
    d) No, Lua does not allow library modification
  2. How would you define a new string function in Lua?
    a) string.new()
    b) string:add()
    c) Add it directly to the string table
    d) string.define()
  3. Why would you extend a standard library in Lua?
    a) To override built-in functions
    b) To add custom functionality
    c) To improve runtime performance
    d) All of the above
  4. What is the recommended practice when extending standard libraries?
    a) Directly overwrite existing functions
    b) Use a prefix or namespace for new functions
    c) Avoid adding new functions to prevent conflicts
    d) Only modify functions globally
  5. How can you ensure compatibility when extending a library?
    a) Test with the latest Lua version
    b) Use separate modules for extensions
    c) Avoid changing existing function behaviors
    d) All of the above
  6. What happens if you redefine a function in a standard library?
    a) The program throws an error
    b) Lua uses the new function instead
    c) The original function is locked in memory
    d) The library resets to its default state
  7. Can Lua’s os library be extended with additional functions?
    a) No, it’s not allowed by Lua
    b) Yes, by adding functions directly to os
    c) Yes, but only with third-party libraries
    d) No, it requires recompiling Lua
  8. What is a potential risk of extending Lua’s standard libraries?
    a) Breaking compatibility with other scripts
    b) Slower performance
    c) Overwriting critical built-in functions
    d) All of the above
  9. Which technique helps avoid conflicts when extending libraries?
    a) Namespace functions in custom tables
    b) Directly modify library functions
    c) Overwrite unused functions only
    d) Avoid using standard libraries
  10. Why is it important to document extensions to standard libraries?
    a) To ensure they are widely used
    b) To clarify their purpose and usage
    c) To avoid accidental overwrites
    d) To simplify debugging

Answer Key

QnoAnswer
1b) 3
2a) math.ceil
3b) 3
4c) Using ..
5a) Inserts 10 at the second position
6b) Returns the CPU time used by the program
7b) string.format
8a) math.random()
9b) Removes the element at index 1
10a) “h”
11a) os.date
12b) Current time in seconds since the epoch
13b) os.date("%Y-%m-%d")
14d) Lua does not have a native delay function
15c) The difference in seconds between two times
16b) os.time(table)
17c) Day Month Date HH:MM:SS Year
18a) os.clock()
19c) os.time()
20b) Returns a table representing the current date and time
21c) Yes, by directly assigning a function to math
22c) Add it directly to the string table
23b) To add custom functionality
24b) Use a prefix or namespace for new functions
25d) All of the above
26b) Lua uses the new function instead
27b) Yes, by adding functions directly to os
28d) All of the above
29a) Namespace functions in custom tables
30b) To clarify their purpose and usage

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