Bird
0
0

Given this code snippet, what will happen if a KeyboardInterrupt occurs?

medium📝 Predict Output Q5 of 15
Raspberry Pi - GPIO Basics with Python
Given this code snippet, what will happen if a KeyboardInterrupt occurs?
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
try:
    while True:
        pass
finally:
    GPIO.cleanup()
AGPIO.cleanup() runs, resetting pins safely
BProgram exits without cleanup
CAn error is raised and pins stay set
DThe while loop continues endlessly
Step-by-Step Solution
Solution:
  1. Step 1: Analyze try-finally behavior on KeyboardInterrupt

    finally block runs even if KeyboardInterrupt stops the loop.
  2. Step 2: Effect of GPIO.cleanup() in finally

    Cleanup resets GPIO pins safely before program exits.
  3. Final Answer:

    GPIO.cleanup() runs, resetting pins safely -> Option A
  4. Quick Check:

    finally always runs cleanup [OK]
Quick Trick: finally block always executes, even on interrupts [OK]
Common Mistakes:
  • Thinking cleanup is skipped on interrupt
  • Assuming loop never ends
  • Believing error is raised without cleanup

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Raspberry Pi Quizzes