MCQs on Control Structures | Lua

1. if, else, and elseif

  1. What does the if statement do in Lua?
    • a) Loops through a block of code
    • b) Executes a block if a condition is true
    • c) Stops program execution
    • d) Handles errors
  2. Which keyword is used to provide an alternative path in an if block?
    • a) elseif
    • b) else
    • c) elseifelse
    • d) then
  3. What must an if condition evaluate to in Lua?
    • a) Boolean (true or false)
    • b) Any number
    • c) Any string
    • d) A table
  4. What happens if none of the conditions in an ifelseif block are true?
    • a) An error is thrown
    • b) The else block is executed
    • c) The if block is retried
    • d) The script terminates
  5. What keyword must immediately follow the if condition?
    • a) then
    • b) do
    • c) end
    • d) elseif
  6. Can elseif be used without an else block?
    • a) Yes
    • b) No
    • c) Only with a condition
    • d) It depends on Lua version
  7. How are nested if statements structured in Lua?
    • a) Using brackets
    • b) Each if must have its own end
    • c) Nesting is not allowed
    • d) Using semicolons
  8. What is the default value of a condition in Lua if omitted?
    • a) true
    • b) false
    • c) nil
    • d) Error

2. Loops: while, repeat...until, and for

  1. What does a while loop do in Lua?
    • a) Executes until the condition becomes false
    • b) Executes once
    • c) Iterates through a list
    • d) Evaluates conditions after the block
  2. Which loop evaluates its condition after executing its body?
    • a) while
    • b) for
    • c) repeat...until
    • d) if
  3. How is the repeat...until loop terminated?
    • a) Using break
    • b) When the condition evaluates to true
    • c) When the condition evaluates to false
    • d) When a variable is updated
  4. What keyword is used to iterate over a sequence of numbers in Lua?
    • a) for
    • b) while
    • c) do
    • d) in
  5. What is the default step in a numeric for loop?
    • a) 1
    • b) 0
    • c) 2
    • d) Depends on the range
  6. Which of the following is NOT true for a for loop?
    • a) It must have a start, stop, and step
    • b) It can decrement values
    • c) It executes at least once
    • d) It uses end to terminate
  7. Can while loops be nested in Lua?
    • a) Yes, with do blocks
    • b) Yes, with separate conditions
    • c) No, nesting is not allowed
    • d) Only in certain versions
  8. What will a repeat...until loop with a true condition do?
    • a) Execute once and exit
    • b) Cause an infinite loop
    • c) Skip execution
    • d) Throw an error
  9. Which statement best describes the difference between while and repeat...until?
    • a) while executes at least once
    • b) repeat...until executes at least once
    • c) Both are identical
    • d) while doesn’t require conditions

3. Using break and return

  1. What does the break statement do in Lua?
    • a) Exits a loop immediately
    • b) Exits the program
    • c) Pauses a loop temporarily
    • d) Returns a value
  2. Can break be used outside of a loop in Lua?
    • a) Yes
    • b) No
    • c) Only in functions
    • d) Only in conditionals
  3. What happens if break is executed in a while loop?
    • a) The loop terminates
    • b) The loop restarts
    • c) The loop condition is ignored
    • d) An error is thrown
  4. Where can the return statement be used in Lua?
    • a) Only in loops
    • b) Only in functions
    • c) Anywhere
    • d) Only with if
  5. What does return do in a function?
    • a) Ends the function execution and returns a value
    • b) Exits a loop
    • c) Starts a new iteration
    • d) Throws an error
  6. How many values can a return statement provide?
    • a) Only one
    • b) Two
    • c) Unlimited
    • d) None
  7. Can break and return be used together?
    • a) Yes, in any block
    • b) Yes, but only in loops
    • c) No, it causes a conflict
    • d) Only in functions
  8. Which of these statements is valid for using break?
    • a) Inside repeat...until
    • b) Inside if
    • c) Outside any control structure
    • d) Inside a table
  9. Does return terminate the function immediately?
    • a) Yes, always
    • b) No, it depends on the condition
    • c) Only if followed by end
    • d) Only if in a loop
  10. What happens if return is used without a value?
    • a) Returns nil
    • b) Causes an error
    • c) Returns 0
    • d) Continues execution
  11. Can break be used in nested loops?
    • a) Yes, but only exits the inner loop
    • b) No
    • c) Only if explicitly declared
    • d) Exits all loops
  12. Which keyword immediately stops a Lua program?
    • a) break
    • b) return
    • c) os.exit()
    • d) stop
  13. Can break and return have conditions in Lua?
    • a) Yes, with if statements
    • b) No
    • c) Only break can have conditions
    • d) Only return can have conditions

Answer Key

QnoAnswer
1b) Executes a block if a condition is true
2b) else
3a) Boolean (true or false)
4b) The else block is executed
5a) then
6a) Yes
7b) Each if must have its own end
8b) false
9a) Executes until the condition becomes false
10c) repeat...until
11b) When the condition evaluates to true
12a) for
13a) 1
14c) Executes at least once
15b) Yes, with separate conditions
16b) Cause an infinite loop
17b) repeat...until executes at least once
18a) Exits a loop immediately
19b) No
20a) The loop terminates
21b) Only in functions
22a) Ends the function execution and returns a value
23c) Unlimited
24b) Yes, but only in loops
25a) Inside repeat...until
26a) Yes, always
27a) Returns nil
28a) Yes, but only exits the inner loop
29c) os.exit()
30a) Yes, with if statements

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