30 Lua Quiz Questions and Answers

Lua is a lightweight, fast, and embeddable scripting language designed to be flexible, portable, and easy to use. It was developed in the early 1990s by a team of engineers at the Pontifical Catholic University of Rio de Janeiro in Brazil. The name “Lua” means “moon” in Portuguese and represents the project’s ambition to be a small, reflective language.

Here is an overview of Lua and its key characteristics:

Scripting Language: Lua is primarily used as a scripting language, meaning it is embedded within other applications to extend their functionality or automate tasks. It is not designed to be a standalone language for large-scale applications.

Lightweight and Portable: Lua is written in ANSI C, making it lightweight and portable across various platforms, including embedded systems and mobile devices.

Pro Tip

Want to assess your learners online? Create an online quiz for free!

Embeddable: Lua is designed to be easily embedded into other programs, and it provides a simple API for integration with C, C++, and other languages. Many applications and games use Lua as a scripting engine to enable users to customize behavior without modifying the core codebase.

Dynamic Typing: Lua is dynamically typed, meaning variable types are determined at runtime. This allows for flexibility in programming but also requires careful handling of data types.

Table of content

Part 1: 30 Lua programming quiz questions & answers

1. What type of language is Lua primarily used as?
a) Compiled language
b) Object-oriented language
c) Scripting language
d) Low-level language
Answer: c) Scripting language

2. What does the name “Lua” mean in Portuguese?
a) Sun
b) Moon
c) Star
d) Earth
Answer: b) Moon

3. Lua is designed to be lightweight and portable across various platforms because it is written in:
a) C++
b) Java
c) LuaScript
d) ANSI C
Answer: d) ANSI C

4. What is the primary data structure used in Lua to represent arrays, dictionaries, and objects?
a) Arrays
b) Lists
c) Tuples
d) Tables
Answer: d) Tables

5. Which of the following is NOT a key characteristic of Lua?
a) Dynamic typing
b) Lexical scoping
c) Garbage collection
d) Static typing
Answer: d) Static typing

6. Lua’s syntax is simple and easy to read, influenced by which programming languages?
a) Python and Java
b) JavaScript and C++
c) Ruby and Perl
d) JavaScript and Python
Answer: d) JavaScript and Python

7. What is the primary purpose of embedding Lua into other applications?
a) To create standalone programs
b) To write large-scale applications
c) To extend functionality or automate tasks
d) To develop mobile apps
Answer: c) To extend functionality or automate tasks

8. Lua uses weak typing, which means:
a) Variables must be explicitly declared with a type
b) Type conversions are not allowed
c) Type conversions are performed implicitly when necessary
d) Variables cannot be changed after initialization
Answer: c) Type conversions are performed implicitly when necessary

9. What is the main mechanism used in Lua for memory management?
a) Manual memory management
b) Automatic garbage collection
c) Reference counting
d) Stack allocation
Answer: b) Automatic garbage collection

10. Which keyword is used to define a function in Lua?
a) func
b) def
c) function
d) fn
Answer: c) function

11. In Lua, what does the “%” operator represent?
a) Addition
b) Subtraction
c) Multiplication
d) Modulus (remainder)
Answer: d) Modulus (remainder)

12. What is the output of the following code snippet?
“`lua
local x = 10
local y = 5
local result = x / y
print(result)
“`
a) 2
b) 2.0
c) 2.5
d) “2.0”
Answer: c) 2.5

13. In Lua, what is the correct syntax for a single-line comment?
a) // This is a comment
b) /* This is a comment */
c) # This is a comment
d) — This is a comment
Answer: d) — This is a comment

14. How do you declare a variable as a constant in Lua?
a) constant x = 5
b) const x = 5
c) readonly x = 5
d) local const x = 5
Answer: d) local const x = 5

15. What is the correct way to concatenate two strings in Lua?
a) str1 . str2
b) str1 + str2
c) str1 .. str2
d) str1 & str2
Answer: c) str1 .. str2

Part 2: Download Lua questions & answers for free

Download questions & answers for free

16. In Lua, what is the keyword used to break out of a loop prematurely?
a) stop
b) exit
c) break
d) end
Answer: c) break

17. Lua uses lexical scoping, which means that a function can access variables from its:
a) Parent scope
b) Child scope
c) Global scope
d) Sibling scope
Answer: a) Parent scope

18. What is the correct syntax to create a multiline string in Lua?
a) “This is a multiline string”
b) ‘This is a multiline string’
c) [[This is a multiline string]]
d) `This is a multiline string`
Answer: c) [[This is a multiline string]]

19. Which standard library function is used to iterate over key-value pairs in a Lua table?
a) pairs()
b) ipairs()
c) each()
d) map()
Answer: a) pairs()

20. What is the keyword used to define a local variable in Lua?
a) var
b) let
c) const
d) local
Answer: d) local

21. In Lua, what is the purpose of the “require” function?
a) To load external libraries and modules
b) To define a new function
c) To include another Lua file
d) To execute a shell command
Answer: a) To load external libraries and modules

22. How do you define a function with multiple return values in Lua?
a) function add(a, b) return a + b end
b) function add(a, b) return a, b end
c) function add(a, b) return (a, b) end
d) function add(a, b) a + b end
Answer: b) function add(a, b) return a, b end

23. What is the output of the following code snippet?
“`lua
local x = 10
if x > 5 then
print(“x is greater than 5”)
else
print(“x is less than or equal to 5”)
end
“`
a) x is greater than 5
b) x is less than or equal to 5
c) The code will not compile due to an error.
d) No output will be printed.
Answer: a) x is greater than 5

24. What is the purpose of the “and” operator in Lua?
a) Logical AND operator
b) Bitwise AND operator
c) String concatenation operator
d) Arithmetic AND operator
Answer: a) Logical AND operator

25. In Lua, how do you access the length of a string or an array?
a) length(str)
b) count(str)
c) size(str)
d) #str
Answer: d) #str

Just so you know

With OnlineExamMaker quiz software, anyone can create & share professional online assessments easily.

26. What is the output of the following code snippet?
“`lua
local fruits = {“apple”, “banana”, “orange”}
print(fruits[2])
“`
a) apple
b) banana
c) orange
d) The code will not compile due to an error.
Answer: b) banana

27. Which of the following is NOT a valid loop construct in Lua?
a) for loop
b) while loop
c) do-while loop
d) repeat-until loop
Answer: c) do-while loop

28. In Lua, how do you define a coroutine?
a) define coroutine my_coroutine
b) coroutine my_coroutine = new coroutine()
c) local my_coroutine = coroutine.create(function() … end)
d) my_coroutine = start coroutine function() … end
Answer: c) local my_coroutine = coroutine.create(function() … end)

29. What is the output of the following code snippet?
“`lua
local x = 5
while x > 0 do
print(x)
x = x – 1
end
“`
a) 5, 4, 3, 2, 1
b) 1, 2, 3, 4, 5
c) 5
d) The code will not compile due to an error.
Answer: a) 5, 4, 3, 2, 1

30. What is the purpose of the “nil” value in Lua?
a) It represents an empty string.
b) It represents a null value.
c) It is used to terminate a loop.
d) It is used as a placeholder for future assignment.
Answer: b) It represents a null value.

Part 3: Best online quiz making platform – OnlineExamMaker

With OnlineExamMaker quiz maker, teachers can easily create, customize, and distribute quizzes with just a few clicks. The intuitive interface provides a range of question types, including multiple-choice, true or false, fill in the blank, and open-ended questions, enabling users to make professional assessments that suit their specific needs.

Create Your Next Quiz/Exam with OnlineExamMaker

SAAS, free forever
100% data ownership