Bird
0
0

What will this Python code print on Raspberry Pi if the serial device sends the string 'World'?

medium📝 Predict Output Q4 of 15
Raspberry Pi - Serial UART Communication
What will this Python code print on Raspberry Pi if the serial device sends the string 'World'?
import serial
ser = serial.Serial('/dev/ttyS0', 9600)
data = ser.read(5).decode('utf-8')
print(data)
AWorld
BWorl
CHello
DError
Step-by-Step Solution
Solution:
  1. Step 1: Understand ser.read(5)

    This reads exactly 5 bytes from the serial port.
  2. Step 2: Decode bytes to string

    The bytes are decoded using UTF-8, converting them to the string 'World'.
  3. Final Answer:

    World -> Option A
  4. Quick Check:

    Read 5 bytes matches 'World' length [OK]
Quick Trick: Read count matches string length [OK]
Common Mistakes:
MISTAKES
  • Assuming read reads more or less bytes
  • Not decoding bytes before printing
  • Confusing output with different strings

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Raspberry Pi Quizzes