Python PCEP Practice Questions: KeyboardInterrupt Exception
PCEP Certification Practice Test - Questions, Answers and Explanations
Here are 25 PCEP questions focusing on the “KeyboardInterrupt” exception from Python's built-in exception hierarchy. The questions use various formats, including single- and multiple-select questions, fill-in-the-gap, code fill, code insertion, sorting, and more.
Question 1: Which of the following best describes the KeyboardInterrupt exception in Python?
- Raised when the user interrupts the program with a keyboard signal (e.g., Ctrl+C).
- Raised when the program cannot find a file.
- Raised when an arithmetic operation exceeds the limits.
- Raised when a syntax error is found in the code.
Answer: A) Raised when the user interrupts the program with a keyboard signal (e.g., Ctrl+C).
Explanation: The KeyboardInterrupt exception is raised when the user manually interrupts a running program, typically by pressing Ctrl+C.
Question 2: Which of the following are true about the KeyboardInterrupt exception? (Select all that apply.)
- It is a subclass of Exception.
- It is raised when a user presses Ctrl+C during program execution.
- It can be caught using a try-except block.
- It cannot be caught or handled.
Answer: B) It is raised when a user presses Ctrl+C during program execution.
C) It can be caught using a try-except block.
Explanation: The KeyboardInterrupt exception is a subclass of Exception and is raised when the user interrupts the program using keyboard input (like Ctrl+C). It can be handled by catching it in a try-except block.
Question 3: _________ is the keyboard combination that usually raises the KeyboardInterrupt exception in Python.
- Alt+F4
- Ctrl+C
- Shift+Esc
Answer: B) Ctrl+C
Explanation: The combination Ctrl+C is commonly used in terminal or command-line environments to signal an interruption, which raises a KeyboardInterrupt in Python.
Question 4: In Python, pressing Ctrl+C raises the __________ exception, which can be caught to prevent program termination.
▼Answer: KeyboardInterrupt
Explanation: The KeyboardInterrupt exception is raised when a user sends an interrupt signal to the program, typically by pressing Ctrl+C.
Question 5: Fill in the blanks to complete the code that catches a KeyboardInterrupt exception and prints a message:
try: while True: pass except __________: print("Program interrupted by the user")▼
Answer: except KeyboardInterrupt:
Explanation: The except KeyboardInterrupt: block catches the KeyboardInterrupt exception, allowing the program to handle user interruptions gracefully.
Question 6: What happens if a KeyboardInterrupt is not caught by an exception handler?
- The program continues execution.
- The program terminates immediately.
- The program pauses until user input is received.
- The program skips to the next line of code.
Answer: B) The program terminates immediately
Explanation: If the KeyboardInterrupt exception is not caught, the program terminates immediately, stopping further execution.
Question 7: Insert the code to handle a KeyboardInterrupt exception in the following code snippet:
while True: print("Running...") # INSERT CODE HERE to allow interruption with Ctrl+C▼
Answer:
try: while True: print("Running...") except KeyboardInterrupt: print("Program stopped by user.")
Explanation: By using a try-except block, the KeyboardInterrupt exception can be caught, allowing the program to print a message before termination instead of abruptly stopping.
Question 8: Which of the following operations could be interrupted by a KeyboardInterrupt exception? (Select all that apply.)
- An infinite loop (while True:)
- A file read operation
- A network request
- A function that raises a ZeroDivisionError
Answer: A) An infinite loop (while True:)
B) A file read operation
C) A network request
Explanation: Most operations, including loops, file reading, and network requests, can be interrupted by a KeyboardInterrupt. The ZeroDivisionError is unrelated to user interruptions.
Question 9: To allow users to terminate a long-running script gracefully, the _________ exception should be caught and handled in Python.
- ImportError
- IOError
- KeyboardInterrupt
Answer: C) KeyboardInterrupt
Explanation: Catching KeyboardInterrupt allows the program to handle user-triggered interruptions, such as pressing Ctrl+C, which is useful for gracefully terminating long-running scripts.
Question 10: The __________ exception is a subclass of the BaseException class and is designed to catch user interruptions.
▼Answer: KeyboardInterrupt
Explanation: KeyboardInterrupt is a subclass of BaseException, which allows it to be caught when the user sends an interrupt signal, typically by pressing Ctrl+C.
Question 11: Arrange the following steps in the correct order for how a KeyboardInterrupt is handled in Python when Ctrl+C is pressed:
- The interrupt signal is sent to the program.
- The program terminates (if the exception is not caught).
- The exception is caught by the program (if a handler is present).
- Python raises the KeyboardInterrupt exception.
Answer: A) The interrupt signal is sent to the program.
D) Python raises the KeyboardInterrupt exception.
C) The exception is caught by the program (if a handler is present).
B) The program terminates (if the exception is not caught).
Explanation: When Ctrl+C is pressed, the system sends an interrupt signal to the program, Python raises the KeyboardInterrupt exception, and the program either handles it or terminates if it's not caught.
Question 12: Which of the following exceptions are similar in nature to KeyboardInterrupt, in that they inherit from BaseException? (Select all that apply.)
- SystemExit
- MemoryError
- KeyboardInterrupt
- NameError
Answer: A) SystemExit
B) MemoryError
C) KeyboardInterrupt
Explanation: Both SystemExit, MemoryError, and KeyboardInterrupt inherit from BaseException. They are not typical exceptions but are used to handle specific situations like system exit, memory exhaustion, and user interruptions.
Question 13: In the try-except block, the _________ exception is used to catch interruptions from user input like pressing Ctrl+C.
- IOError
- ImportError
- KeyboardInterrupt
Answer: C) KeyboardInterrupt
Explanation: KeyboardInterrupt is used to catch the interruption signal when the user presses Ctrl+C, allowing the program to handle it and potentially clean up before exiting.
Question 14: The Python interpreter raises KeyboardInterrupt when the user sends an interrupt signal, typically by pressing __________.
▼Answer: Ctrl+C
Explanation: The most common method for sending an interrupt signal to a Python program is pressing Ctrl+C in the terminal, which raises the KeyboardInterrupt exception.
Question 15: Fill in the blanks in this code to catch KeyboardInterrupt and stop the program gracefully:
try: while True: print("Processing...") except __________: print("Terminating...")▼
Answer: except KeyboardInterrupt:
Explanation: The except KeyboardInterrupt block catches the KeyboardInterrupt exception when the user interrupts the program, allowing it to stop gracefully.
Question 16: What happens if a user sends an interrupt signal (e.g., Ctrl+C) while the program is executing without any exception handling?
- The program ignores the signal and continues running.
- The program pauses until user input is provided.
- The program raises a KeyboardInterrupt exception and terminates.
- The program raises a SystemExit exception.
Answer: C) The program raises a KeyboardInterrupt exception and terminates.
Explanation: When an interrupt signal is sent by the user and there is no exception handling, Python raises a KeyboardInterrupt exception and terminates the program.
Question 17: Insert the correct code to handle a KeyboardInterrupt exception and terminate the loop gracefully:
while True: try: print("Running...") # INSERT CODE HERE to allow for interruption▼
Answer:
except KeyboardInterrupt: print("Loop interrupted by user.") break
Explanation: Catching the KeyboardInterrupt allows the program to terminate the loop gracefully when the user interrupts the program using Ctrl+C.
Question 18: In which of the following scenarios would a KeyboardInterrupt exception typically be raised?
- When trying to divide by zero.
- When pressing Ctrl+C while a program is running.
- When attempting to import a non-existent module.
- When referencing an undefined variable.
Answer: B) When pressing Ctrl+C while a program is running.
Explanation: A KeyboardInterrupt is typically raised when the user presses Ctrl+C to manually interrupt a running program.
Question 19: When a user interrupts a Python program by pressing Ctrl+C, the program raises the ___________ exception, which can be caught for graceful termination.
▼Answer: KeyboardInterrupt
Explanation: The KeyboardInterrupt exception is raised when the user manually interrupts a running program, usually by pressing Ctrl+C.
Question 20: Which of the following methods can be used to handle a KeyboardInterrupt exception? (Select all that apply.)
- Wrapping the code inside a try-except block.
- Ignoring the KeyboardInterrupt exception and letting the program crash.
- Catching the exception and executing custom code.
- Raising a ZeroDivisionError.
Answer: A) Wrapping the code inside a try-except block.
C) Catching the exception and executing custom code.
Explanation: The KeyboardInterrupt exception can be caught using a try-except block, and custom code can be executed to handle the program termination gracefully.
Question 21: If a KeyboardInterrupt is not caught, Python will terminate the program by raising the __________ exception.
- ZeroDivisionError
- SystemExit
- KeyboardInterrupt
Answer: C) KeyboardInterrupt
Explanation: If a KeyboardInterrupt exception is not caught in the program, Python will raise this exception and terminate the program.
Question 22: Fill in the blanks to complete the following code that terminates the program when the user sends an interrupt signal:
try: while True: print("Working...") except __________: print("Process interrupted by user.") exit()▼
Answer: except KeyboardInterrupt:
Explanation: The except KeyboardInterrupt block catches the user interrupt signal (Ctrl+C) and allows the program to terminate gracefully with a custom message.
Question 23: Which of the following exceptions is raised when the user sends an interrupt signal during a program’s execution?
- MemoryError
- NameError
- KeyboardInterrupt
- ImportError
Answer: C) KeyboardInterrupt
Explanation: KeyboardInterrupt is the exception raised when the user sends an interrupt signal to the program, typically by pressing Ctrl+C.
Question 24: Arrange the following in the correct order when handling a KeyboardInterrupt exception:
- User sends an interrupt signal by pressing Ctrl+C.
- Python raises a KeyboardInterrupt.
- The program catches the exception (if a handler is present).
- Program starts executing.
Answer: D) Program starts executing.
A) User sends an interrupt signal by pressing Ctrl+C.
B) Python raises a KeyboardInterrupt.
C) The program catches the exception (if a handler is present).
Explanation: First, the program runs, and when the user interrupts it, Python raises the KeyboardInterrupt exception. If the exception is caught by a handler, the program can manage the interruption.
Question 25: What happens when a KeyboardInterrupt exception is raised and caught? (Select all that apply.)
- The program continues executing.
- The program stops immediately.
- Custom logic in the except block is executed.
- The program terminates without executing any additional code.
Answer: A) The program continues executing.
C) Custom logic in the except block is executed.
Explanation: When a KeyboardInterrupt exception is raised and caught, the custom logic inside the except block is executed, and the program can continue running or terminate based on that logic.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics