Bird
0
0

What will be the output of this code snippet if the serial device sends the bytes b'OK'?

medium📝 Predict Output Q13 of 15
Raspberry Pi - Serial UART Communication
What will be the output of this code snippet if the serial device sends the bytes b'OK'?
import serial
ser = serial.Serial('/dev/ttyUSB0', 9600, timeout=1)
response = ser.read(2)
print(response)
ser.close()
Ab'OK'
B'OK'
COK
Db"\x00\x00"
Step-by-Step Solution
Solution:
  1. Step 1: Understand ser.read(2) behavior

    The read(2) method reads 2 bytes from the serial port and returns them as a bytes object.
  2. Step 2: Interpret the print output

    Printing a bytes object shows it with a leading b and quotes, so b'OK' will be printed.
  3. Final Answer:

    b'OK' -> Option A
  4. Quick Check:

    ser.read() returns bytes, printed as b'...' [OK]
Quick Trick: ser.read() returns bytes, print shows b'...' [OK]
Common Mistakes:
MISTAKES
  • Expecting a string instead of bytes
  • Confusing print output with raw string
  • Assuming null bytes if device sends data

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Raspberry Pi Quizzes