Bird
Raised Fist0
Raspberry Piprogramming~20 mins

LED class and methods in Raspberry Pi - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
LED Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this LED toggle code?

Consider this Python code controlling an LED on a Raspberry Pi. What will it print?

Raspberry Pi
class LED:
    def __init__(self):
        self.state = False
    def on(self):
        self.state = True
        print("LED is ON")
    def off(self):
        self.state = False
        print("LED is OFF")
    def toggle(self):
        if self.state:
            self.off()
        else:
            self.on()

led = LED()
led.toggle()
led.toggle()
A
LED is OFF
LED is ON
B
LED is ON
LED is ON
C
LED is ON
LED is OFF
D
LED is OFF
LED is OFF
Attempts:
2 left
💡 Hint

Think about what the toggle method does depending on the LED state.

❓ Predict Output
intermediate
2:00remaining
What is the final LED state after this sequence?

Given this code controlling an LED, what is the final state of the LED?

Raspberry Pi
class LED:
    def __init__(self):
        self.state = False
    def on(self):
        self.state = True
    def off(self):
        self.state = False
    def toggle(self):
        self.state = not self.state

led = LED()
led.on()
led.toggle()
led.toggle()
led.off()
print(led.state)
AFalse
BNone
CError
DTrue
Attempts:
2 left
💡 Hint

Track the state changes step by step.

❓ Predict Output
advanced
2:00remaining
What error does this LED method cause?

What error will this code raise when calling led.blink()?

Raspberry Pi
class LED:
    def __init__(self):
        self.state = False
    def on(self):
        self.state = True
    def off(self):
        self.state = False
    def blink(self):
        for _ in range(3):
            self.on
            self.off

led = LED()
led.blink()
ATypeError: 'method' object is not callable
BAttributeError: 'LED' object has no attribute 'blink'
CSyntaxError
DNo error, runs fine
Attempts:
2 left
💡 Hint

Look carefully at how methods are called inside blink.

🧠 Conceptual
advanced
2:00remaining
Which option correctly implements LED blinking with delay?

Which code snippet correctly makes the LED blink 3 times with a 1-second pause between on and off?

A
import time
class LED:
    def __init__(self):
        self.state = False
    def on(self):
        self.state = True
        print('ON')
    def off(self):
        self.state = False
        print('OFF')
    def blink(self):
        for _ in range(3):
            self.on
            time.sleep(1)
            self.off
            time.sleep(1)
B
import time
class LED:
    def __init__(self):
        self.state = False
    def on(self):
        self.state = True
        print('ON')
    def off(self):
        self.state = False
        print('OFF')
    def blink(self):
        for _ in range(3):
            self.on()
            time.sleep(1)
            self.off()
            time.sleep(1)
C
import time
class LED:
    def __init__(self):
        self.state = False
    def on(self):
        self.state = True
        print('ON')
    def off(self):
        self.state = False
        print('OFF')
    def blink(self):
        for _ in range(3):
            self.on()
            self.off()
            time.sleep(1)
D
import time
class LED:
    def __init__(self):
        self.state = False
    def on(self):
        self.state = True
        print('ON')
    def off(self):
        self.state = False
        print('OFF')
    def blink(self):
        for _ in range(3):
            time.sleep(1)
            self.on()
            time.sleep(1)
            self.off()
Attempts:
2 left
💡 Hint

Remember to call methods with parentheses and place delays correctly.

🚀 Application
expert
3:00remaining
How many times does the LED turn ON in this code?

Given this code, how many times will the LED print "LED ON"?

Raspberry Pi
class LED:
    def __init__(self):
        self.state = False
    def on(self):
        if not self.state:
            self.state = True
            print("LED ON")
    def off(self):
        if self.state:
            self.state = False
            print("LED OFF")
    def blink(self, times):
        for _ in range(times):
            self.on()
            self.off()

led = LED()
led.blink(5)
led.on()
led.on()
A6
B5
C7
D8
Attempts:
2 left
💡 Hint

Count each time print("LED ON") runs, including after blinking.

Practice

(1/5)
1.

What does the on() method of an LED class typically do in Raspberry Pi programming?

easy
A. Turns the LED light on
B. Turns the LED light off
C. Makes the LED blink continuously
D. Cleans up the GPIO pins

Solution

  1. Step 1: Understand the purpose of on() method

    The on() method is designed to switch the LED light to the ON state.
  2. Step 2: Compare with other methods

    Other methods like off() turn the LED off, and blink() makes it blink. Cleanup is done separately.
  3. Final Answer:

    Turns the LED light on -> Option A
  4. Quick Check:

    on() means LED ON [OK]
Hint: Remember: on() means light ON, off() means light OFF [OK]
Common Mistakes:
  • Confusing on() with off()
  • Thinking on() cleans GPIO pins
  • Assuming on() makes LED blink
2.

Which of the following is the correct way to create an LED object on GPIO pin 17 using the gpiozero library?

from gpiozero import LED

led = ?
easy
A. LED(17)
B. LED.pin(17)
C. LED[17]
D. LED{17}

Solution

  1. Step 1: Recall LED object creation syntax

    In gpiozero, you create an LED by calling LED(pin_number), e.g., LED(17).
  2. Step 2: Check other options for syntax errors

    Options A, B, and D use invalid Python syntax for object creation.
  3. Final Answer:

    LED(17) -> Option A
  4. Quick Check:

    LED(pin) = LED(17) [OK]
Hint: Use parentheses with pin number: LED(17) [OK]
Common Mistakes:
  • Using square brackets instead of parentheses
  • Trying to call a method like LED.pin()
  • Using curly braces which are invalid here
3.

What will be the output of this code snippet controlling an LED?

from gpiozero import LED
from time import sleep

led = LED(22)
led.on()
sleep(1)
led.off()
print('LED state:', led.is_lit)
medium
A. SyntaxError
B. LED state: True
C. LED state: None
D. LED state: False

Solution

  1. Step 1: Trace the LED state changes

    The LED is turned on, then after 1 second turned off before printing.
  2. Step 2: Check the printed LED state

    After led.off(), led.is_lit is False, so output is 'LED state: False'.
  3. Final Answer:

    LED state: False -> Option D
  4. Quick Check:

    LED off means is_lit = False [OK]
Hint: Check final LED state after all commands run [OK]
Common Mistakes:
  • Assuming LED stays on after off()
  • Confusing is_lit property value
  • Expecting syntax error due to sleep
4.

Identify the error in this code that tries to blink an LED on pin 5:

from gpiozero import LED
from time import sleep

led = LED(5)
led.blink(on_time=0.5, off_time=0.5)
sleep(2)
led.off()
medium
A. Missing led.on() before blink
B. Should call led.close() to clean GPIO
C. No error; code works correctly
D. blink() method requires a times argument

Solution

  1. Step 1: Review the code for GPIO cleanup

    The code blinks the LED and turns it off but does not release GPIO resources.
  2. Step 2: Identify missing cleanup step

    Proper practice is to call led.close() to free GPIO pins after use.
  3. Final Answer:

    Should call led.close() to clean GPIO -> Option B
  4. Quick Check:

    Always close GPIO pins after use [OK]
Hint: Always close() LEDs to free GPIO pins [OK]
Common Mistakes:
  • Thinking blink() needs on() first
  • Assuming blink() requires times argument
  • Ignoring GPIO cleanup after program
5.

You want to create a program that blinks two LEDs on pins 17 and 27 alternately every 0.5 seconds for 5 seconds total. Which code snippet correctly implements this using the LED class?

from gpiozero import LED
from time import sleep

led1 = LED(17)
led2 = LED(27)

start = 0
while start < 5:
    led1.on()
    led2.off()
    sleep(0.5)
    led1.off()
    led2.on()
    sleep(0.5)
    start += 1

led1.off()
led2.off()
led1.close()
led2.close()
hard
A. Should use blink() method instead of manual on/off
B. The loop runs too long; should use start < 10
C. This code blinks LEDs alternately for 5 seconds correctly
D. Missing sleep between led1.off() and led2.on()

Solution

  1. Step 1: Analyze the loop timing and LED control

    The loop runs 5 times, each iteration takes 1 second (two 0.5s sleeps), total 5 seconds blinking alternately.
  2. Step 2: Check LED on/off sequence and cleanup

    LED1 and LED2 turn on/off alternately with proper sleep delays, and GPIO pins are closed at the end.
  3. Final Answer:

    This code blinks LEDs alternately for 5 seconds correctly -> Option C
  4. Quick Check:

    Loop count x sleep = total blink time [OK]
Hint: Count loop x sleep to verify total blink time [OK]
Common Mistakes:
  • Miscounting total blink duration
  • Forgetting to close GPIO pins
  • Assuming blink() method is mandatory
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard