MCQs on Basic Input/Output | Lua

1. Using print() for Output

  1. What does the print() function in Lua do?
    • a) Reads input from the user
    • b) Displays output to the screen
    • c) Writes data to a file
    • d) Terminates the program
  2. Which of the following is a valid syntax to use print()?
    • a) print("Hello, World!")
    • b) echo("Hello, World!")
    • c) display("Hello, World!")
    • d) log("Hello, World!")
  3. What will print("Lua", "is", "fun") output?
    • a) Lua is fun
    • b) Lua,is,fun
    • c) Lua is fun (with spaces in between)
    • d) Error: Multiple arguments not allowed
  4. Can print() display numbers?
    • a) No, it only works with strings
    • b) Yes, numbers are automatically converted to strings
    • c) Yes, but requires manual conversion
    • d) Only if formatted
  5. What happens when you use print() without arguments?
    • a) It throws an error
    • b) It prints a blank line
    • c) It prints nil
    • d) It prints undefined
  6. Which function is used to print formatted output in Lua?
    • a) printf()
    • b) format()
    • c) io.write()
    • d) print()
  7. What is the difference between print() and io.write()?
    • a) print() adds a newline; io.write() does not
    • b) io.write() adds a newline; print() does not
    • c) Both add a newline by default
    • d) Neither adds a newline
  8. Is it possible to print tables directly using print()?
    • a) Yes, it prints the table contents
    • b) No, it prints the memory address
    • c) Yes, but only for numerical keys
    • d) No, it throws an error
  9. What does print("Lua", nil, "is great") output?
    • a) Lua is great
    • b) Lua nil is great
    • c) Lua is great (skipping nil)
    • d) Error: Cannot print nil
  10. Can print() handle concatenated strings?
    • a) Yes, using + operator
    • b) Yes, using .. operator
    • c) Yes, using both + and ..
    • d) No, concatenation is not supported

2. Reading Input with io.read()

  1. What is the purpose of io.read()?
    • a) To print data to the console
    • b) To read input from the user
    • c) To write data to a file
    • d) To format strings
  2. Which function reads a single line of input in Lua?
    • a) read()
    • b) io.input()
    • c) io.read("*l")
    • d) get_line()
  3. What happens if io.read() is used without any arguments?
    • a) Reads the entire file
    • b) Reads a single line
    • c) Reads a single word
    • d) Waits for user input
  4. What does io.read("*n") do?
    • a) Reads a number from the user
    • b) Reads a newline character
    • c) Reads a string
    • d) Reads multiple lines
  5. Which of the following is true about io.read("*a")?
    • a) It reads a single word
    • b) It reads a single line
    • c) It reads all remaining input
    • d) It reads a number
  6. Can io.read() handle input redirection from a file?
    • a) No, it only works with the console
    • b) Yes, with io.input()
    • c) No, it requires io.open()
    • d) Yes, without additional configuration
  7. What is the default input stream for io.read()?
    • a) The console (stdin)
    • b) A file specified by the user
    • c) The stdout stream
    • d) None of the above
  8. How do you close the input stream after using io.read()?
    • a) io.close()
    • b) io.flush()
    • c) input.close()
    • d) It automatically closes
  9. What happens if io.read("*n") is used but the user enters a string?
    • a) It reads the string
    • b) It throws an error
    • c) It returns nil
    • d) It reads the first character
  10. Can io.read() read multiple inputs at once?
    • a) Yes, using io.read("*l", "*n")
    • b) Yes, with a loop
    • c) No, it only reads one input at a time
    • d) No, requires separate function calls

3. String Formatting Basics

  1. Which function is used to format strings in Lua?
    • a) string.format()
    • b) io.format()
    • c) printf()
    • d) str.format()
  2. How do you format numbers with two decimal places in Lua?
    • a) string.format("%.2n", num)
    • b) string.format("%.2f", num)
    • c) format(num, 2)
    • d) format("0.2", num)
  3. What does string.format("%s", "Lua") return?
    • a) Lua
    • b) %s Lua
    • c) string
    • d) Error
  4. What does the format specifier %d represent in Lua?
    • a) A decimal number
    • b) A floating-point number
    • c) A string
    • d) A date
  5. Which format specifier is used for hexadecimal numbers?
    • a) %h
    • b) %x
    • c) %o
    • d) %b
  6. How do you format a string with a width of 10 spaces?
    • a) %10s
    • b) %10c
    • c) %s10
    • d) %-10s
  7. What happens if more arguments are passed to string.format() than needed?
    • a) It throws an error
    • b) Extra arguments are ignored
    • c) It concatenates them
    • d) It adds placeholders automatically
  8. Which of the following is a valid example of formatted output?
    • a) string.format("%d + %d = %d", 2, 3, 5)
    • b) printf("%d + %d = %d", 2, 3, 5)
    • c) io.write("%d + %d = %d", 2, 3, 5)
    • d) log("%d + %d = %d", 2, 3, 5)
  9. Can string.format() be used for padding with zeros?
    • a) No
    • b) Yes, using %0nd
    • c) Yes, using %zd
    • d) Only for strings
  10. How do you left-align text using string.format()?
    • a) %-s
    • b) %-10s
    • c) %10s
    • d) %<s

Answers

QnoAnswer
1b) Displays output to the screen
2a) print("Hello, World!")
3c) Lua is fun (with spaces in between)
4b) Yes, numbers are automatically converted to strings
5b) It prints a blank line
6d) print()
7a) print() adds a newline; io.write() does not
8b) No, it prints the memory address
9b) Lua nil is great
10b) Yes, using .. operator
11b) To read input from the user
12c) io.read("*l")
13d) Waits for user input
14a) Reads a number from the user
15c) It reads all remaining input
16b) Yes, with io.input()
17a) The console (stdin)
18a) io.close()
19c) It returns nil
20b) Yes, with a loop
21a) string.format()
22b) string.format("%.2f", num)
23a) Lua
24a) A decimal number
25b) %x
26a) %10s
27b) Extra arguments are ignored
28a) string.format("%d + %d = %d", 2, 3, 5)
29b) Yes, using %0nd
30b) %-10s

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