Bird
0
0

Identify the error in this code snippet using spidev:

medium📝 Debug Q14 of 15
Raspberry Pi - SPI Communication
Identify the error in this code snippet using spidev:
import spidev
spi = spidev.SpiDev()
spi.xfer2([0x01, 0x02])
spi.open(0, 0)
spi.close()
ANot closing SPI device after use
BCalling xfer2() before opening SPI device
CUsing wrong method name xfer2()
DMissing import statement
Step-by-Step Solution
Solution:
  1. Step 1: Check method call order

    The xfer2() method requires the SPI device to be opened first with open().
  2. Step 2: Identify the error

    Here, xfer2() is called before open(), causing a runtime error.
  3. Final Answer:

    Calling xfer2() before opening SPI device -> Option B
  4. Quick Check:

    Open SPI before xfer2() call [OK]
Quick Trick: Always open SPI before using xfer2() [OK]
Common Mistakes:
MISTAKES
  • Calling xfer2() before open()
  • Forgetting to close SPI device
  • Confusing method names

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Raspberry Pi Quizzes