Bird
0
0

What will be the output behavior of this code snippet on a Raspberry Pi with a buzzer connected to GPIO 18?

medium📝 Predict Output Q13 of 15
Raspberry Pi - gpiozero Library

What will be the output behavior of this code snippet on a Raspberry Pi with a buzzer connected to GPIO 18?

from gpiozero import TonalBuzzer
from time import sleep
buzzer = TonalBuzzer(18)
buzzer.play(440)
sleep(1)
buzzer.stop()
AThe buzzer plays a random tone for 1 second.
BThe buzzer beeps once immediately and stops.
CThe code raises an error because play() is not a method.
DThe buzzer plays a 440 Hz tone for 1 second, then stops.
Step-by-Step Solution
Solution:
  1. Step 1: Understand TonalBuzzer play() method

    The play(frequency) method plays a tone at the given frequency in Hz, here 440 Hz (A note).
  2. Step 2: Analyze code timing

    The tone plays for 1 second due to sleep(1), then buzzer.stop() stops the sound.
  3. Final Answer:

    The buzzer plays a 440 Hz tone for 1 second, then stops. -> Option D
  4. Quick Check:

    play(440) + sleep(1) + stop() = D [OK]
Quick Trick: play(frequency) sounds tone; sleep controls duration [OK]
Common Mistakes:
  • Thinking play() is not a valid method
  • Assuming beep instead of tone
  • Ignoring sleep duration effect

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Raspberry Pi Quizzes