MCQs on Variables and Data Types | Lua

1. Variables and Dynamic Typing

  1. What is a variable in Lua?
    • a) A constant value
    • b) A storage location identified by a name
    • c) An immutable value
    • d) A pre-defined keyword
  2. How are variables declared in Lua?
    • a) Using var keyword
    • b) Using local or without any keyword
    • c) Using let keyword
    • d) Using def keyword
  3. Lua is considered a dynamically-typed language because:
    • a) Variables must have a declared type
    • b) Variable types can change at runtime
    • c) Variables cannot be reassigned
    • d) It has strict type-checking
  4. What happens if you access an undefined variable in Lua?
    • a) It throws an error
    • b) It returns nil
    • c) It returns 0
    • d) It initializes the variable
  5. How do you assign multiple values to variables in Lua?
    • a) x = 1, 2, 3
    • b) x, y, z = 1, 2, 3
    • c) x: 1, y: 2, z: 3
    • d) x = {1, 2, 3}
  6. What does the local keyword do?
    • a) Declares a global variable
    • b) Declares a block-scoped variable
    • c) Declares a constant variable
    • d) Declares a variable with default value
  7. Which is a valid variable name in Lua?
    • a) 1var
    • b) var-name
    • c) _var1
    • d) true

2. Primitive Data Types

  1. Which of the following is NOT a primitive data type in Lua?
    • a) Nil
    • b) Boolean
    • c) Array
    • d) Number
  2. What does the nil data type represent in Lua?
    • a) An undefined value
    • b) A Boolean false
    • c) A zero value
    • d) A reserved memory
  3. Which of the following evaluates to false in Lua?
    • a) nil and false
    • b) 0
    • c) Empty string ""
    • d) Both nil and 0
  4. Lua’s number type can represent:
    • a) Only integers
    • b) Only floating-point numbers
    • c) Both integers and floating-point numbers
    • d) Complex numbers
  5. Which function checks the type of a variable in Lua?
    • a) typeof()
    • b) getType()
    • c) type()
    • d) checkType()
  6. How do you concatenate strings in Lua?
    • a) Using +
    • b) Using &
    • c) Using ..
    • d) Using concat()
  7. What is the default value of an uninitialized variable in Lua?
    • a) false
    • b) nil
    • c) 0
    • d) undefined
  8. What does the expression type(nil) return?
    • a) "undefined"
    • b) "null"
    • c) "nil"
    • d) "void"
  9. What is the length of an empty string ("") in Lua?
    • a) 0
    • b) 1
    • c) Nil
    • d) Undefined
  10. The Boolean type in Lua can have values:
    • a) true or false
    • b) 1 or 0
    • c) on or off
    • d) Any value
  11. What happens if you try to compare nil with another value using ==?
    • a) It always returns true
    • b) It always returns false
    • c) It throws an error
    • d) It depends on the value

3. Working with Constants

  1. Does Lua natively support constant variables?
    • a) Yes, using the const keyword
    • b) No, but it can be implemented through metatables
    • c) No, constants are not supported
    • d) Yes, using the constant keyword
  2. Which approach is commonly used to simulate constants in Lua?
    • a) Declaring a global variable
    • b) Using metatables to restrict modifications
    • c) Prefixing variable names with const_
    • d) None of the above
  3. Can you reassign a local variable to make it constant in Lua?
    • a) No
    • b) Yes
    • c) Only inside a function
    • d) Only in global scope
  4. What happens if you try to modify a constant-like table with metatables?
    • a) Lua ignores the modification
    • b) It allows modification
    • c) It throws an error
    • d) None of the above
  5. Constants are mostly used to:
    • a) Hold reusable values
    • b) Store changing data
    • c) Declare global variables
    • d) Improve script performance
  6. How do you protect a variable from being modified in Lua?
    • a) Use local
    • b) Use metatables with __newindex
    • c) Prefix it with _
    • d) Use nil
  7. What is a common use case for constants in Lua?
    • a) Storing configuration settings
    • b) Defining local functions
    • c) Optimizing string operations
    • d) Storing temporary values
  8. When simulating constants, which metamethod is crucial?
    • a) __add
    • b) __index
    • c) __newindex
    • d) __call
  9. What would const.PI = 3.14 do if PI is protected by a metatable?
    • a) Allow the reassignment
    • b) Ignore the reassignment
    • c) Throw an error
    • d) Create a new variable
  10. Can you prevent reassignment of primitive data types like number?
    • a) Yes, with const
    • b) No, unless using a metatable wrapper
    • c) Yes, using local
    • d) No
  11. Which of these techniques ensures constant-like behavior?
    • a) Encapsulating values in a function
    • b) Using global variables
    • c) Assigning values directly
    • d) Using global const
  12. Why are constants useful in scripting?
    • a) Prevent errors from accidental overwrites
    • b) Increase performance
    • c) Simplify debugging
    • d) All of the above

Answers

QnoAnswer
1b) A storage location identified by a name
2b) Using local or without any keyword
3b) Variable types can change at runtime
4b) It returns nil
5b) x, y, z = 1, 2, 3
6b) Declares a block-scoped variable
7c) _var1
8c) Array
9a) An undefined value
10a) nil and false
11c) Both integers and floating-point numbers
12c) type()
13c) Using ..
14b) nil
15c) "nil"
16a) 0
17a) true or false
18b) It always returns false
19b) No, but it can be implemented through metatables
20b) Using metatables to restrict modifications
21a) No
22c) It throws an error
23a) Hold reusable values
24b) Use metatables with __newindex
25a) Storing configuration settings
26c) __newindex
27c) Throw an error
28b) No, unless using a metatable wrapper
29a) Encapsulating values in a function
30d) All of the above

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