MCQs on Functions and Scope | Lua

1. Defining and Calling Functions

  1. How do you define a function in Lua?
    • a) function myFunction()
    • b) def myFunction()
    • c) func myFunction()
    • d) declare myFunction()
  2. How do you call a function in Lua?
    • a) myFunction()
    • b) call myFunction()
    • c) execute myFunction()
    • d) invoke myFunction()
  3. What keyword is used to define a function in Lua?
    • a) function
    • b) define
    • c) func
    • d) declare
  4. What happens if a function is called without defining it in Lua?
    • a) The program executes successfully
    • b) An error is thrown
    • c) The function is ignored
    • d) Lua creates a default function
  5. Can you call a function before it is defined in Lua?
    • a) Yes, Lua allows forward declaration
    • b) No, functions must be defined first
    • c) Only in certain versions of Lua
    • d) Yes, if the function is defined globally
  6. What is the correct way to call a function with arguments in Lua?
    • a) myFunction(arg1, arg2)
    • b) myFunction[arg1, arg2]
    • c) myFunction{arg1, arg2}
    • d) call myFunction(arg1, arg2)
  7. How do you terminate a function in Lua?
    • a) exit
    • b) end
    • c) return
    • d) stop
  8. Can functions in Lua return multiple values?
    • a) Yes, they can return multiple values
    • b) No, only one value is returned
    • c) Only tables can return multiple values
    • d) Functions cannot return values

2. Function Arguments and Return Values

  1. How are function arguments passed in Lua?
    • a) By reference
    • b) By value
    • c) Both by value and reference
    • d) Arguments cannot be passed
  2. Can you call a function without passing any arguments in Lua?
    • a) Yes, if the function is defined without arguments
    • b) No, arguments are always required
    • c) Yes, but only with default values
    • d) Only in global functions
  3. How do you return a value from a function in Lua?
    • a) Using return
    • b) Using exit
    • c) Using out
    • d) Using output
  4. What is the result of a function returning no value in Lua?
    • a) Returns nil
    • b) The function terminates early
    • c) The function throws an error
    • d) It continues execution silently
  5. What happens when a function returns multiple values in Lua?
    • a) Only the last value is used
    • b) All values are returned as a table
    • c) All values are returned as separate results
    • d) The function throws an error
  6. Which of the following is the correct way to return multiple values?
    • a) return 1, 2, 3
    • b) return (1, 2, 3)
    • c) return {1, 2, 3}
    • d) return [1, 2, 3]
  7. How can you assign function return values to variables in Lua?
    • a) Using var1, var2 = myFunction()
    • b) Using var1 = myFunction()
    • c) Using var1 <- myFunction()
    • d) Using var1 + myFunction()
  8. What is the default return value for a function with no explicit return statement in Lua?
    • a) false
    • b) nil
    • c) 0
    • d) ""
  9. Can functions accept optional arguments in Lua?
    • a) Yes, by using nil as default values
    • b) No, all arguments are mandatory
    • c) Only with specific functions
    • d) Only in global scope
  10. Which of the following will cause an error when calling a function in Lua?
    • a) Passing too many arguments
    • b) Calling a function without parentheses
    • c) Not using end to close the function
    • d) Passing no arguments when they are required

3. Local vs. Global Variables

  1. What is a local variable in Lua?
    • a) A variable that is accessible anywhere in the program
    • b) A variable that is accessible only within the function or block where it is defined
    • c) A variable that exists only when the program is running
    • d) A variable that stores global settings
  2. How do you define a local variable in Lua?
    • a) local varName = value
    • b) def varName = value
    • c) variable varName = value
    • d) var varName = value
  3. Can a local variable in Lua be accessed outside the function in which it is defined?
    • a) Yes, if it’s declared at the beginning of the script
    • b) No, local variables are confined to their scope
    • c) Yes, but only after the function ends
    • d) Only in nested functions
  4. How does a global variable differ from a local variable in Lua?
    • a) Global variables are faster to access
    • b) Local variables are always private and cannot be accessed globally
    • c) Global variables are created with the global keyword
    • d) Local variables must be initialized before use
  5. What is the default scope of a variable in Lua if it is not explicitly declared as local?
    • a) Global
    • b) Local
    • c) Temporary
    • d) Static
  6. Which of the following will create a global variable in Lua?
    • a) global varName = value
    • b) varName = value
    • c) local varName = value
    • d) define varName = value
  7. How do you explicitly make a global variable inside a function in Lua?
    • a) Use the global keyword
    • b) Assign a value to a variable without using local
    • c) Use setglobal keyword
    • d) It is not possible in Lua
  8. What happens if a local variable and a global variable have the same name in Lua?
    • a) The local variable will overwrite the global variable
    • b) The global variable will overwrite the local variable
    • c) An error is thrown
    • d) They will exist independently without conflict
  9. Can you access a global variable inside a local scope in Lua?
    • a) Yes, global variables are always accessible
    • b) No, only local variables are accessible inside a local scope
    • c) Only if declared with global
    • d) Only if the function is recursive
  10. What is the main disadvantage of using global variables in Lua?
    • a) They are slower to access
    • b) They can create conflicts and make code harder to debug
    • c) They require more memory
    • d) They are only available in some versions of Lua
  11. How do you change the value of a global variable inside a function?
    • a) By using global variable = value
    • b) By assigning a new value to the variable without the local keyword
    • c) By using setglobal
    • d) Global variables cannot be modified
  12. What is the typical use case for local variables in Lua?
    • a) To store global configuration settings
    • b) To limit variable scope and reduce potential conflicts
    • c) To store data that needs to be shared across functions
    • d) For storing function names

Answers

QnoAnswer
1a) function myFunction()
2a) myFunction()
3a) function
4b) An error is thrown
5b) No, functions must be defined first
6a) myFunction(arg1, arg2)
7b) end
8a) Yes, they can return multiple values
9c) Both by value and reference
10a) Yes, if the function is defined without arguments
11a) Using return
12a) Returns nil
13c) All values are returned as separate results
14a) return 1, 2, 3
15a) Using var1, var2 = myFunction()
16b) nil
17a) Yes, by using nil as default values
18a) Passing too many arguments
19b) A variable that is accessible only within the function or block where it is defined
20a) local varName = value
21b) No, local variables are confined to their scope
22b) Local variables are always private and cannot be accessed globally
23a) Global
24b) varName = value
25b) Assign a value to a variable without using local
26a) The local variable will overwrite the global variable
27a) Yes, global variables are always accessible
28b) They can create conflicts and make code harder to debug
29b) By assigning a new value to the variable without the local keyword
30b) To limit variable scope and reduce potential conflicts

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