Bird
0
0

What will be the output of this Python code snippet on Raspberry Pi?

medium📝 Predict Output Q13 of 15
Raspberry Pi - Serial UART Communication
What will be the output of this Python code snippet on Raspberry Pi?
import serial
ser = serial.Serial('/dev/ttyUSB0', baudrate=4800, timeout=2)
data = ser.read(5)
print(len(data))
A5
B0
C2
DRaises an error
Step-by-Step Solution
Solution:
  1. Step 1: Understand ser.read(5) with timeout=2

    The read tries to get 5 bytes but waits up to 2 seconds if no data arrives.
  2. Step 2: Consider no data sent to device

    If no data is available, read returns empty bytes, so length is 0.
  3. Final Answer:

    0 -> Option B
  4. Quick Check:

    Timeout causes empty read if no data [OK]
Quick Trick: If no data, read returns empty bytes length 0 [OK]
Common Mistakes:
MISTAKES
  • Assuming read always returns requested bytes
  • Ignoring timeout effect
  • Expecting error without device

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Raspberry Pi Quizzes