Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare a singleton object named Logger.
Kotlin
object [1] {
fun log(message: String) {
println(message)
}
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase names for the object.
Forgetting to use the
object keyword.✗ Incorrect
The keyword object declares a singleton object named Logger.
2fill in blank
mediumComplete the code to call the log function of the Logger object.
Kotlin
fun main() {
[1].log("Hello, Kotlin!")
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase or incorrect object names.
Trying to create an instance instead of using the object directly.
✗ Incorrect
To call the log function, use the object name Logger.
3fill in blank
hardFix the error in the object declaration syntax.
Kotlin
object [1] { fun greet() = println("Hi!") }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Adding parentheses after the object name.
Using lowercase names for objects.
✗ Incorrect
Object declarations do not use parentheses. Remove them and use a valid name like Greeter.
4fill in blank
hardFill both blanks to declare an object with a property and a function.
Kotlin
object [1] { val [2] = "Kotlin" fun printName() = println(name) }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Mismatch between property name and usage in function.
Using lowercase object names.
✗ Incorrect
The object is named AppConfig and the property used in the function is name.
5fill in blank
hardFill all three blanks to create an object with a mutable property and a function to update it.
Kotlin
object [1] { var [2] = 0 fun [3](value: Int) { count = value } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using immutable property
val instead of var.Mismatch between property and function parameter names.
✗ Incorrect
The object is Counter, the mutable property is count, and the function to update it is updateCount.