Bird
0
0

What will be the output of the following code snippet?

medium📝 Predict Output Q13 of 15
Raspberry Pi - SPI Communication
What will be the output of the following code snippet?
import spidev
spi = spidev.SpiDev()
spi.open(0, 0)
response = spi.xfer2([0x01, 0x02, 0x03])
spi.close()
print(response)
A[1, 2, 3]
B[0, 0, 0]
CA list of bytes received from the SPI device
DSyntaxError
Step-by-Step Solution
Solution:
  1. Step 1: Understand spi.xfer2() behavior

    The xfer2() method sends bytes and returns a list of bytes received from the SPI device.
  2. Step 2: Analyze the code output

    The output depends on the connected SPI device's response, so it returns a list of received bytes, not necessarily the same as sent.
  3. Final Answer:

    A list of bytes received from the SPI device -> Option C
  4. Quick Check:

    xfer2() returns received bytes list [OK]
Quick Trick: xfer2() returns device response bytes list [OK]
Common Mistakes:
MISTAKES
  • Assuming output equals input list
  • Expecting zeros if no device connected
  • Thinking code causes syntax error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Raspberry Pi Quizzes