Challenge - 5 Problems
Kotlin Class Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this Kotlin class instantiation?
Consider this Kotlin code snippet. What will be printed when the main function runs?
Kotlin
class Person(val name: String, val age: Int) { fun greet() = "Hello, my name is $name and I am $age years old." } fun main() { val p = Person("Alice", 30) println(p.greet()) }
Attempts:
2 left
💡 Hint
Look at how the primary constructor parameters are declared and used.
✗ Incorrect
The class Person has a primary constructor with parameters name and age declared as val, so they become properties. The greet() function uses these properties to return the greeting string. Instantiating Person with "Alice" and 30 sets these properties correctly.
❓ Predict Output
intermediate2:00remaining
What does this Kotlin class print?
Analyze this Kotlin code and determine the output when main() is executed.
Kotlin
class Rectangle(val width: Int, val height: Int) { val area = width * height } fun main() { val rect = Rectangle(5, 4) println(rect.area) }
Attempts:
2 left
💡 Hint
Check how the property 'area' is initialized using constructor parameters.
✗ Incorrect
The property 'area' is initialized by multiplying width and height, which are constructor parameters. For width=5 and height=4, area is 20.
🔧 Debug
advanced2:00remaining
Identify the error in this Kotlin class declaration
This Kotlin code does not compile. What is the cause of the error?
Kotlin
class Car(model: String, year: Int) { fun info() = "Model: $model, Year: $year" } fun main() { val c = Car("Toyota", 2020) println(c.info()) }
Attempts:
2 left
💡 Hint
Check if constructor parameters are accessible inside class methods.
✗ Incorrect
Constructor parameters without val or var are not properties and cannot be accessed in other class members. The info() function tries to use model and year which are not properties, causing a compilation error.
📝 Syntax
advanced2:00remaining
Which Kotlin class declaration is syntactically correct?
Choose the option that correctly declares a Kotlin class with a primary constructor and a property.
Attempts:
2 left
💡 Hint
Remember the syntax for declaring properties in the primary constructor.
✗ Incorrect
Option D correctly declares a class with a primary constructor that has a property 'title' using val. Option D tries to assign constructor parameter to property without val/var, which is allowed but redundant and less idiomatic. Option D misses colon after var. Option D is invalid syntax.
🚀 Application
expert3:00remaining
How many instances are created and what is printed?
Given this Kotlin code, how many instances of class User are created and what is the output?
Kotlin
class User(val name: String) { init { println("User created: $name") } } fun main() { val users = listOf(User("Anna"), User("Bob"), User("Cara")) println("Total users: ${users.size}") }
Attempts:
2 left
💡 Hint
Each User() call creates an instance and runs init block.
✗ Incorrect
Each User("...") creates an instance and runs the init block printing the creation message. Then the list size is printed as 3.