Bird
0
0

Identify the error in this code snippet:

medium📝 Debug Q6 of 15
Raspberry Pi - GPIO Basics with Python
Identify the error in this code snippet:
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
try:
    GPIO.setup(18, GPIO.OUT)
    GPIO.output(18, true)
except:
    GPIO.cleanup()
AGPIO.output() cannot be used with GPIO.OUT
BGPIO.setup() is missing a pin number
CNo import statement for GPIO
DGPIO.cleanup() should be in finally, not except
Step-by-Step Solution
Solution:
  1. Step 1: Understand exception handling for cleanup

    Cleanup should run always, not only on exceptions.
  2. Step 2: Why finally is better than except here

    finally block runs regardless of error, ensuring cleanup.
  3. Final Answer:

    GPIO.cleanup() should be in finally, not except -> Option D
  4. Quick Check:

    Use finally for guaranteed cleanup [OK]
Quick Trick: Put cleanup in finally, not just except [OK]
Common Mistakes:
  • Placing cleanup only in except block
  • Confusing setup and output usage
  • Missing import statement

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Raspberry Pi Quizzes