Bird
0
0

Which Python construct guarantees that GPIO.cleanup() executes regardless of exceptions during GPIO operations?

easy📝 Conceptual Q2 of 15
Raspberry Pi - GPIO Basics with Python
Which Python construct guarantees that GPIO.cleanup() executes regardless of exceptions during GPIO operations?
AUsing a <code>while</code> loop to repeatedly call cleanup
BPlacing cleanup code inside an <code>if</code> statement
CCalling cleanup only after the main loop finishes
DUsing a <code>finally</code> block after a <code>try</code> block
Step-by-Step Solution
Solution:
  1. Step 1: Understand exception handling

    The finally block runs after try, even if exceptions occur.
  2. Step 2: Ensuring cleanup

    Placing GPIO.cleanup() inside finally ensures it always executes.
  3. Final Answer:

    Using a finally block after a try block -> Option D
  4. Quick Check:

    finally always runs [OK]
Quick Trick: Use finally block to guarantee cleanup [OK]
Common Mistakes:
  • Putting cleanup inside if statements
  • Calling cleanup only after loops
  • Using loops to call cleanup repeatedly

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Raspberry Pi Quizzes