Challenge - 5 Problems
Kotlin Instance Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Kotlin object instantiation without 'new'
What is the output of this Kotlin code that creates an instance without using the 'new' keyword?
Kotlin
class Person(val name: String) { fun greet() = "Hello, $name!" } fun main() { val p = Person("Anna") println(p.greet()) }
Attempts:
2 left
💡 Hint
In Kotlin, you create instances by calling the class name like a function.
✗ Incorrect
Kotlin does not use the 'new' keyword. You create an instance by calling the class constructor directly. Here, Person("Anna") creates an instance with name 'Anna'.
❓ Predict Output
intermediate2:00remaining
Creating data class instance without 'new' keyword
What will be the output of this Kotlin code snippet?
Kotlin
data class Point(val x: Int, val y: Int) fun main() { val p = Point(5, 10) println("x=${p.x}, y=${p.y}") }
Attempts:
2 left
💡 Hint
Data classes are instantiated like normal classes in Kotlin.
✗ Incorrect
Kotlin creates instances by calling the constructor directly. Here, Point(5, 10) creates an instance with x=5 and y=10.
🔧 Debug
advanced2:00remaining
Why does this Kotlin code fail to compile?
Identify the reason this Kotlin code does not compile.
Kotlin
class Animal(val type: String) fun main() { val a = new Animal("Dog") println(a.type) }
Attempts:
2 left
💡 Hint
Check how Kotlin creates instances compared to Java.
✗ Incorrect
Kotlin creates instances by calling the constructor directly without 'new'. Using 'new' causes a compilation error.
🧠 Conceptual
advanced2:00remaining
How does Kotlin create instances without 'new'?
Which statement best explains how Kotlin creates instances without using the 'new' keyword?
Attempts:
2 left
💡 Hint
Think about how you write code to create objects in Kotlin.
✗ Incorrect
Kotlin simplifies instance creation by calling the constructor directly, so no 'new' keyword is needed.
❓ Predict Output
expert2:30remaining
Output of Kotlin companion object instance creation
What is the output of this Kotlin code that creates an instance using a companion object without 'new'?
Kotlin
class Logger private constructor(val level: String) { companion object { fun create(level: String) = Logger(level) } } fun main() { val log = Logger.create("DEBUG") println(log.level) }
Attempts:
2 left
💡 Hint
Companion objects can create instances even with private constructors.
✗ Incorrect
The companion object method 'create' calls the private constructor internally, allowing instance creation without 'new'.