Bird
0
0

Given this MQTT client code snippet, what will be printed?

medium📝 Predict Output Q5 of 15
Raspberry Pi - MQTT for IoT
Given this MQTT client code snippet, what will be printed?
import paho.mqtt.client as mqtt
client = mqtt.Client()
client.on_connect = lambda client, userdata, flags, rc: print('Connected with code', rc)
client.connect('broker.hivemq.com', 1883)
client.loop_start()
AConnected with code 1
BConnected with code 0
CNo output
DSyntax error
Step-by-Step Solution
Solution:
  1. Step 1: Understand on_connect callback

    The callback prints 'Connected with code' followed by the connection result code.
  2. Step 2: Recognize successful connection code

    On success, rc is 0, so the printed message is 'Connected with code 0'.
  3. Final Answer:

    Connected with code 0 -> Option B
  4. Quick Check:

    MQTT connect success code = 0 [OK]
Quick Trick: MQTT connect success code is 0 [OK]
Common Mistakes:
MISTAKES
  • Expecting code 1 on success
  • Thinking no output occurs without loop_forever
  • Confusing syntax with runtime behavior

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Raspberry Pi Quizzes