Bird
0
0

Given the Python code snippet reading GPS data, what will be printed if the GPS sends the sentence '$GPGGA,123519,4807.038,N,01131.000,E,1,08,0.9,545.4,M,46.9,M,,*47'?

medium📝 Predict Output Q13 of 15
Raspberry Pi - Serial UART Communication
Given the Python code snippet reading GPS data, what will be printed if the GPS sends the sentence '$GPGGA,123519,4807.038,N,01131.000,E,1,08,0.9,545.4,M,46.9,M,,*47'?
import serial
ser = serial.Serial('/dev/ttyS0', 9600)
line = ser.readline().decode('ascii', errors='replace')
if line.startswith('$GPGGA'):
    print('GPS fix data:', line.strip())
else:
    print('Other data')
AGPS fix data: $GPGGA,123519,4807.038,N,01131.000,E,1,08,0.9,545.4,M,46.9,M,,*47
BOther data
CSyntaxError
DNo output
Step-by-Step Solution
Solution:
  1. Step 1: Check if line starts with '$GPGGA'

    The line from GPS starts exactly with '$GPGGA', so the condition is True.
  2. Step 2: Print the GPS fix data line

    Since condition is True, it prints 'GPS fix data:' followed by the full line without trailing newline.
  3. Final Answer:

    GPS fix data: $GPGGA,123519,4807.038,N,01131.000,E,1,08,0.9,545.4,M,46.9,M,,*47 -> Option A
  4. Quick Check:

    Line startswith $GPGGA = print GPS fix [OK]
Quick Trick: Check string start with $GPGGA to print GPS fix [OK]
Common Mistakes:
MISTAKES
  • Ignoring the startswith condition
  • Expecting syntax errors from decode
  • Not stripping newline before print

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Raspberry Pi Quizzes