Bird
0
0

Given this code snippet, what will be the output?

medium📝 Predict Output Q5 of 15
Raspberry Pi - I2C Communication
Given this code snippet, what will be the output?
from smbus2 import SMBus
bus = SMBus(1)
try:
    bus.write_byte_data(0x50, 0x02, 0x10)
    print("Command sent")
except Exception:
    print("Failed to send")
ACommand sent
BFailed to send
CNo output
DSyntax error
Step-by-Step Solution
Solution:
  1. Step 1: Understand try-except block

    If write_byte_data succeeds, it prints "Command sent"; if it fails, it prints "Failed to send".
  2. Step 2: Assume normal device connection

    With device connected and no error, the try block runs successfully.
  3. Final Answer:

    Command sent -> Option A
  4. Quick Check:

    Successful write triggers "Command sent" [OK]
Quick Trick: Try block runs if no error, printing success [OK]
Common Mistakes:
MISTAKES
  • Assuming exception without cause
  • Ignoring try-except logic

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Raspberry Pi Quizzes