0
0
Kotlinprogramming~20 mins

Why classes define behavior and state in Kotlin - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Master of Classes: Behavior and State
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 Kotlin class example?
Consider this Kotlin class that models a simple counter. What will be printed when the main function runs?
Kotlin
class Counter {
    var count = 0
    fun increment() {
        count += 1
    }
}

fun main() {
    val counter = Counter()
    counter.increment()
    counter.increment()
    println(counter.count)
}
ACompilation error
B2
C0
D1
Attempts:
2 left
💡 Hint
Think about how many times the increment function is called and what it does.
🧠 Conceptual
intermediate
1:30remaining
Why do classes hold both state and behavior?
Which of the following best explains why classes in Kotlin combine state (data) and behavior (functions)?
ATo allow functions to run without any data.
BBecause Kotlin requires all functions to be inside classes.
CTo separate data from functions for better performance.
DTo keep data and related actions together for easier management and reuse.
Attempts:
2 left
💡 Hint
Think about how grouping related things helps in real life, like keeping tools in one box.
🔧 Debug
advanced
2:30remaining
Identify the error in this Kotlin class that models a bank account
This Kotlin class is supposed to model a bank account with a balance and a deposit function. What error will occur when compiling or running this code?
Kotlin
class BankAccount {
    var balance: Double
    fun deposit(amount: Double) {
        balance += amount
    }
}

fun main() {
    val account = BankAccount()
    account.deposit(100.0)
    println(account.balance)
}
ACompilation error: Variable 'balance' must be initialized
BOutput: 100.0
CRuntime error: NullPointerException when accessing balance
DCompilation error: deposit function missing return type
Attempts:
2 left
💡 Hint
Check if all variables have initial values before use.
Predict Output
advanced
2:00remaining
What is the output of this Kotlin class with a method changing state?
Look at this Kotlin class and main function. What will be printed when the program runs?
Kotlin
class LightSwitch {
    var isOn = false
    fun toggle() {
        isOn = !isOn
    }
}

fun main() {
    val switch = LightSwitch()
    switch.toggle()
    switch.toggle()
    switch.toggle()
    println(switch.isOn)
}
Atrue
Bfalse
Cnull
DCompilation error
Attempts:
2 left
💡 Hint
Each toggle flips the switch state from on to off or off to on.
🧠 Conceptual
expert
3:00remaining
How does encapsulation relate to classes defining behavior and state?
Which statement best describes how encapsulation connects to classes holding both state and behavior in Kotlin?
AEncapsulation allows classes to share their state freely with other classes.
BEncapsulation requires that behavior is written outside the class to keep state private.
CEncapsulation hides the internal state and only exposes behavior to protect data integrity.
DEncapsulation means all class variables must be public for easy access.
Attempts:
2 left
💡 Hint
Think about how you keep your personal information safe by only sharing what is necessary.