0
0
Raspberry Piprogramming~20 mins

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

Choose your learning style9 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.