MCQs on Error Handling and Debugging | Lua

1. Error Handling with pcall and xpcall

  1. What does the pcall function do in Lua?
    • a) Executes a function in protected mode
    • b) Prints error messages
    • c) Pauses the execution of the program
    • d) None of the above
  2. How does xpcall differ from pcall in Lua?
    • a) xpcall uses a custom error handler
    • b) xpcall does not handle errors
    • c) pcall uses a custom error handler
    • d) There is no difference between xpcall and pcall
  3. What is the primary advantage of using pcall over a simple function call in Lua?
    • a) It ensures that any errors do not stop the program
    • b) It improves the performance of the program
    • c) It automatically logs the errors
    • d) It reduces memory consumption
  4. What type of value does pcall return when the function executes successfully?
    • a) true and the function’s result
    • b) false and the error message
    • c) true and an error message
    • d) false and the function’s result
  5. Which of the following is true about xpcall in Lua?
    • a) It provides a way to handle errors with a custom error handler
    • b) It throws an error and exits the program
    • c) It automatically handles all errors without needing a handler
    • d) It returns only the error message without the error status
  6. In Lua, what happens if a function inside pcall throws an error?
    • a) The error is caught and handled gracefully
    • b) The program halts without any error message
    • c) The error is thrown to the global scope
    • d) The error is printed to the console
  7. Which of the following is a valid use case for xpcall?
    • a) To catch errors and provide a custom error handler
    • b) To execute multiple functions simultaneously
    • c) To call functions asynchronously
    • d) To prevent a function from returning any value
  8. What is required as the second argument in xpcall?
    • a) A function to handle the error
    • b) A string containing the error message
    • c) A custom debug function
    • d) A table of error types
  9. What would happen if you call pcall with a function that always throws an error?
    • a) pcall will return false and the error message
    • b) The error will be displayed and program execution stops
    • c) It will cause an infinite loop
    • d) It will cause the function to retry automatically
  10. How do you use pcall to handle errors in Lua?
    • a) By wrapping the function call inside pcall
    • b) By manually checking for errors after every function call
    • c) By using the assert function
    • d) By using pcall only in loops

2. Using error() and assert()

  1. What is the purpose of the error() function in Lua?
    • a) To raise an error with a custom message
    • b) To suppress an error
    • c) To log errors to a file
    • d) To ignore errors
  2. What does the assert() function do in Lua?
    • a) It raises an error if the condition is false
    • b) It catches all errors and prints a message
    • c) It forces the program to exit
    • d) It throws an error if the condition is true
  3. In Lua, what happens when assert() is given a false condition?
    • a) It raises an error with the provided message
    • b) It returns false
    • c) It prints a warning message
    • d) It does nothing
  4. What is the output of assert(false, "Error occurred") in Lua?
    • a) The function throws an error with the message “Error occurred”
    • b) The function returns false
    • c) The program continues without interruption
    • d) It prints “Error occurred” to the console
  5. How can error() be used to specify the level of the error in Lua?
    • a) By passing an integer level argument after the message
    • b) By passing a string containing the error level
    • c) By calling error() inside a loop
    • d) Lua does not allow specifying the level of the error
  6. What does assert() return if the condition is true?
    • a) The condition itself
    • b) nil
    • c) The argument provided to assert()
    • d) It returns the function that called assert()
  7. What is the function signature of error() in Lua?
    • a) error(message, level)
    • b) error(level, message)
    • c) error(message)
    • d) error()
  8. How does assert() handle the message parameter when the condition is true?
    • a) It ignores the message
    • b) It prints the message
    • c) It returns the message as a string
    • d) It throws the message as an exception
  9. Can you use error() to raise a custom error message in Lua?
    • a) Yes, by passing a string as an argument
    • b) No, error() can only print default messages
    • c) Yes, but it only works for system errors
    • d) No, error() does not support custom messages
  10. How would you use assert() to ensure a variable x is not nil?
    • a) assert(x, "x cannot be nil")
    • b) assert(x == nil)
    • c) assert(x ~= nil, "x cannot be nil")
    • d) assert(x)

3. Introduction to the Debug Library

  1. What is the main purpose of the Lua debug library?
    • a) To enable the inspection and manipulation of program execution
    • b) To handle errors and exceptions
    • c) To improve program performance
    • d) To generate reports on program usage
  2. Which function from the debug library is used to get the stack trace in Lua?
    • a) debug.traceback
    • b) debug.getstack
    • c) debug.getinfo
    • d) debug.showstack
  3. What does the debug.getinfo() function return in Lua?
    • a) Information about a function or its execution context
    • b) The current stack trace
    • c) The global variables used in the function
    • d) The memory usage of the function
  4. Which function would you use to set a hook for debugging in Lua?
    • a) debug.sethook
    • b) debug.settrace
    • c) debug.trace
    • d) debug.gethook
  5. What does the debug.getlocal() function do in Lua?
    • a) Returns local variables of a function
    • b) Returns the current function name
    • c) Returns the global variables of the program
    • d) Prints the current local variables to the console
  6. How would you use debug.traceback() to get a detailed stack trace in Lua?
    • a) By passing the error message as an argument
    • b) By calling it within an error-handling function
    • c) By passing the level argument
    • d) debug.traceback() cannot generate stack traces
  7. What is the purpose of debug.sethook() in Lua?
    • a) To monitor function calls and return values
    • b) To log all errors
    • c) To set breakpoints in the code
    • d) To print the stack trace
  8. What does debug.getupvalue() return in Lua?
    • a) The upvalue of a function
    • b) The local variable inside the function
    • c) The global variable referenced by the function
    • d) The environment of a function
  9. How would you prevent the Lua debugger from showing certain variable values?
    • a) By using debug.sethook() to filter out variables
    • b) By disabling the debug library
    • c) By using debug.sethook() with specific conditions
    • d) The debugger cannot be filtered
  10. Which function from the Lua debug library allows you to get the number of function arguments?
    • a) debug.getn()
    • b) debug.getstack()
    • c) debug.getinfo()
    • d) debug.getlocal()

Answer Key

QnoAnswer (Option with the text)
1a) Executes a function in protected mode
2a) xpcall uses a custom error handler
3a) It ensures that any errors do not stop the program
4a) true and the function’s result
5a) It provides a way to handle errors with a custom error handler
6a) The error is caught and handled gracefully
7a) To catch errors and provide a custom error handler
8a) A function to handle the error
9a) false and the error message
10a) By wrapping the function call inside pcall
11a) To raise an error with a custom message
12a) It raises an error if the condition is false
13a) It raises an error with the provided message
14a) The function throws an error with the message “Error occurred”
15a) By passing an integer level argument after the message
16a) It ignores the message
17a) error(message, level)
18a) It ignores the message
19a) Yes, by passing a string as an argument
20a) assert(x, "x cannot be nil")
21a) To enable the inspection and manipulation of program execution
22a) debug.traceback
23a) Information about a function or its execution context
24a) debug.sethook
25a) Returns local variables of a function
26b) By calling it within an error-handling function
27a) To monitor function calls and return values
28a) The upvalue of a function
29c) By using debug.sethook() with specific conditions
30a) debug.getn()

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