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
What does math.floor(3.7) return in Lua? a) 4 b) 3 c) 3.7 d) 0
Which function rounds a number up to the nearest integer? a) math.ceil b) math.floor c) math.round d) math.abs
What does string.len("Lua") return? a) 4 b) 3 c) 2 d) 0
How can you concatenate two strings in Lua? a) Using & b) Using + c) Using .. d) Using concat()
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
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
Which of the following functions can format a string in Lua? a) string.find b) string.format c) string.len d) string.upper
How do you generate a random number in Lua? a) math.random() b) math.rand() c) random() d) os.random()
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
What does string.match("hello", "h") return? a) “h” b) “hello” c) true d) nil
2. Time and Date Manipulation
Which Lua function gets the current date and time? a) os.date b) os.time c) os.now d) os.current
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
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")
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
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
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)
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
Which function measures the time taken to execute a block of code? a) os.clock() b) os.date() c) os.time() d) os.benchmark()
How do you retrieve a Unix timestamp in Lua? a) os.timestamp() b) os.unix() c) os.time() d) os.clock()
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
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
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()
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
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
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
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
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
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
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
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
Qno
Answer
1
b) 3
2
a) math.ceil
3
b) 3
4
c) Using ..
5
a) Inserts 10 at the second position
6
b) Returns the CPU time used by the program
7
b) string.format
8
a) math.random()
9
b) Removes the element at index 1
10
a) “h”
11
a) os.date
12
b) Current time in seconds since the epoch
13
b) os.date("%Y-%m-%d")
14
d) Lua does not have a native delay function
15
c) The difference in seconds between two times
16
b) os.time(table)
17
c) Day Month Date HH:MM:SS Year
18
a) os.clock()
19
c) os.time()
20
b) Returns a table representing the current date and time