MCQs on Basic Debugging and Error Handling | Perl

Chapter 7: Basic Debugging and Error Handling focuses on essential techniques in Perl to handle errors, warnings, and debugging. Mastering warn, die, and using strict and warnings will improve code reliability. Below are 30 multiple-choice questions (MCQs) to help you test your knowledge on these crucial debugging techniques.


Using warn and die

  1. What is the purpose of the warn function in Perl?
    • A) It terminates the program
    • B) It prints a message to STDERR without terminating the program
    • C) It prints a message to STDOUT
    • D) It displays an error message and halts the program
  2. Which of the following will trigger a fatal error in Perl?
    • A) warn "Error occurred";
    • B) die "Fatal error occurred";
    • C) print "Hello World";
    • D) exit;
  3. What is the output of the following code?
    warn “This is a warning!”;
    A) “This is a warning!”
    B) A warning message to STDERR
    C) An error message
    D) It terminates the script
  4. What happens when die is used in a Perl script?
    A) It prints the message to STDOUT and continues execution
    B) It terminates the program and prints the error message
    C) It stops the script but ignores the error message
    D) It ignores all errors
  5. Which function in Perl is used to immediately terminate the program with an error message?
    A) exit()
    B) warn()
    C) die()
    D) end()
  6. What will this code do in Perl?
    die “Error: $!”;
    A) Prints an error message and stops execution
    B) Prints nothing and continues execution
    C) Causes a syntax error
    D) Outputs the string “$!”
  7. When is the die function typically used in Perl?
    A) To display warnings
    B) To throw exceptions and stop execution on critical errors
    C) To log non-critical messages
    D) To manage program flow without errors
  8. Which output will occur for the following code in Perl?
    warn “Check this!”;
    print “This will print after warning.”;
    A) “Check this!” and “This will print after warning.”
    B) “Check this!”
    C) “This will print after warning.”
    D) Only “Check this!” will print
  9. Which of the following is true about warn and die?
    A) warn stops execution, while die continues
    B) warn continues execution, while die stops execution
    C) Both warn and die stop execution
    D) Both warn and die continue execution
  10. What does $! represent in Perl?
    A) The last error message in the program
    B) A file handle for standard output
    C) A variable used for warnings
    D) The program’s exit status

Basic Debugging Techniques

  1. Which Perl command can be used to start the debugger?
    • A) perl -d
    • B) perl -debug
    • C) perl -D
    • D) perl debug
  2. How do you enable debugging in Perl?
    • A) By using the use Debug pragma
    • B) By using the -d flag when running the script
    • C) By using debug() function
    • D) By setting $debug variable to true
  3. What does the Perl debugger allow you to do?
    • A) Print variables
    • B) Set breakpoints
    • C) Step through the program line by line
    • D) All of the above
  4. What is the effect of the following Perl command?
    perl -d script.pl
    A) It runs the script normally without debugging
    B) It enters the Perl debugger before executing the script
    C) It terminates the script without running
    D) It compiles the script but doesn’t execute it
  1. Which Perl debugger command is used to continue execution until the next breakpoint?
    • A) c
    • B) n
    • C) s
    • D) q
  2. Which command in Perl’s debugger allows you to set a breakpoint?
    • A) b
    • B) break
    • C) stop
    • D) pause
  3. What is the purpose of the x command in the Perl debugger?
    • A) Examine the contents of a variable
    • B) Exit the debugger
    • C) Execute a script
    • D) Set a breakpoint
  4. What will happen if you use the p command in the Perl debugger?
    • A) It prints the value of a variable
    • B) It pauses the program
    • C) It executes the next statement
    • D) It prints the program’s output
  5. How do you step into a function call while debugging in Perl?
    • A) Use s to step into
    • B) Use n to step into
    • C) Use p to step into
    • D) Use r to step into
  6. In Perl, how can you display the backtrace of a running script while debugging?
    • A) Use the bt command
    • B) Use the backtrace command
    • C) Use the trace command
    • D) Use the st command

Introduction to Perl Warnings and Strict Pragma

  1. What does the use warnings; pragma do in Perl?
    • A) Enables debugging messages
    • B) Enables warnings for potential issues in the code
    • C) Displays all print statements
    • D) Turns off error reporting
  2. What is the purpose of the use strict; pragma in Perl?
    • A) It enables warnings
    • B) It prevents the use of symbolic references
    • C) It allows more relaxed syntax rules
    • D) It enables debugging messages
  3. Which of the following is a result of using the strict pragma in Perl?
    • A) It prevents the creation of global variables
    • B) It restricts variable names to certain formats
    • C) It allows all types of variable declarations
    • D) It requires all subroutines to be explicitly declared
  4. What happens when you use use strict; in a Perl script?
    • A) It ensures that the code is error-free
    • B) It enforces a stricter syntax and variable declarations
    • C) It disables warnings
    • D) It compiles the script in debug mode
  5. Which statement will generate a warning if use warnings; is enabled?
    • A) $x = 10;
    • B) my $x = 10;
    • C) $x = "abc";
    • D) my $x;
  6. What does the use warnings; pragma help with in Perl?
    • A) It helps catch runtime errors
    • B) It helps catch potential issues like undeclared variables
    • C) It improves script performance
    • D) It adds debugging features
  7. Which of the following will cause an error when using strict in Perl?
    • A) $x = 10;
    • B) my $x;
    • C) $x = $y + 10;
    • D) our $x = 10;
  8. What will happen if you use use strict; and attempt to use an undeclared variable in Perl?
    • A) It will compile without an issue
    • B) It will throw a runtime error
    • C) It will generate a warning
    • D) It will throw a compile-time error
  9. Which Perl pragma helps identify potential issues without halting script execution?
    • A) use strict;
    • B) use warnings;
    • C) use diagnostics;
    • D) use debug;
  10. In Perl, what does the -w flag do when running a script?
    • A) It activates warnings globally
    • B) It turns off error checking
    • C) It enables strict checking
    • D) It enables debugging mode

Answer Key

QnoAnswer
1B) It prints a message to STDERR without terminating the program
2B) die "Fatal error occurred";
3B) A warning message to STDERR
4B) It terminates the program and prints the error message
5C) die()
6A) Prints an error message and stops execution
7B) To throw exceptions and stop execution on critical errors
8A) “Check this!” and “This will print after warning.”
9B) warn continues execution, while die stops execution
10A) The last error message in the program
11A) perl -d
12B) By using the -d flag when running the script
13D) All of the above
14B) It enters the Perl debugger before executing the script
15A) c
16A) b
17A) Examine the contents of a variable
18A) It prints the value of a variable
19A) Use s to step into
20A) Use the bt command
21B) Enables warnings for potential issues in the code
22B) It prevents the use of symbolic references
23A) It prevents the creation of global variables
24B) It enforces a stricter syntax and variable declarations
25D) my $x;
26B) It helps catch potential issues like undeclared variables
27D) our $x = 10;
28D) It will throw a compile-time error
29B) use warnings;
30A) It activates warnings globally

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