Bird
0
0

What will be the output of this JavaScript code running on a Raspberry Pi browser?

medium📝 Predict Output Q5 of 15
Raspberry Pi - Web Server and API
What will be the output of this JavaScript code running on a Raspberry Pi browser? const ws = new WebSocket('ws://localhost:8080'); ws.onopen = () => ws.send('Ping'); ws.onmessage = (event) => console.log(event.data); // Assume server replies with 'Pong' to 'Ping'
APing
BPong
CError: WebSocket not defined
DNo output
Step-by-Step Solution
Solution:
  1. Step 1: Analyze WebSocket events

    On connection open, client sends 'Ping'. Server replies 'Pong'.
  2. Step 2: Identify console output

    onmessage logs the server's reply 'Pong'.
  3. Final Answer:

    Pong -> Option B
  4. Quick Check:

    onmessage logs server reply = Pong [OK]
Quick Trick: onmessage logs server reply, not sent message [OK]
Common Mistakes:
MISTAKES
  • Expecting 'Ping' to be logged
  • Confusing client send with server response
  • Assuming no output without error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Raspberry Pi Quizzes