0
0
Kotlinprogramming~10 mins

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

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare a class named Person.

Kotlin
class [1] {
    var name: String = ""
}
Drag options to blanks, or click blank then click option'
AMyClass
Bperson
CData
DPerson
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase or unrelated class names.
2fill in blank
medium

Complete the code to add a function greet() that prints a greeting.

Kotlin
class Person {
    var name: String = ""
    fun [1]() {
        println("Hello, $name!")
    }
}
Drag options to blanks, or click blank then click option'
Agreet
BsayHello
Chello
DprintGreeting
Attempts:
3 left
💡 Hint
Common Mistakes
Using different function names that don't match the instruction.
3fill in blank
hard

Fix the error in the code to correctly initialize the name property via constructor.

Kotlin
class Person([1]: String) {
    var name: String = name
}
Drag options to blanks, or click blank then click option'
Avar name
Bname
Cval name
Dval n
Attempts:
3 left
💡 Hint
Common Mistakes
Not declaring the constructor parameter as a property.
4fill in blank
hard

Fill both blanks to create a class with a mutable property and a function that updates it.

Kotlin
class Counter {
    var count: Int = [1]
    fun increment() {
        count [2] 1
    }
}
Drag options to blanks, or click blank then click option'
A0
B+=
C-=
D1
Attempts:
3 left
💡 Hint
Common Mistakes
Using '-=' instead of '+='.
Starting count at 1 instead of 0.
5fill in blank
hard

Fill all three blanks to define a class with a property, a constructor, and a method that returns a greeting.

Kotlin
class Person([1]: String) {
    val name: String = [2]
    fun greet(): String {
        return "Hello, $[3]!"
    }
}
Drag options to blanks, or click blank then click option'
Aname
DpersonName
Attempts:
3 left
💡 Hint
Common Mistakes
Using different names causing errors or confusion.