0
0
Kotlinprogramming~20 mins

Class declaration syntax in Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Kotlin Class Mastery
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 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())
}
AHello, my name is Alice and I am 30 years old.
BHello, my name is name and I am age years old.
CCompilation error due to missing constructor
DRuntime error: Uninitialized property
Attempts:
2 left
💡 Hint
Look at how the primary constructor parameters are declared and used.
Predict Output
intermediate
2: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)
}
A20
BCompilation error: val area must be initialized in constructor
C9
DRuntime error: Uninitialized property area
Attempts:
2 left
💡 Hint
Check how the property 'area' is initialized using constructor parameters.
🔧 Debug
advanced
2: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())
}
ANo error, code runs and prints the info
BError because info() function is missing return type
CError because model and year are not declared as properties with val or var
DError because class Car lacks a primary constructor
Attempts:
2 left
💡 Hint
Check if constructor parameters are accessible inside class methods.
📝 Syntax
advanced
2:00remaining
Which Kotlin class declaration is syntactically correct?
Choose the option that correctly declares a Kotlin class with a primary constructor and a property.
Aclass Book(title: String) val title: String
Bclass Book(title: String) { val title = title }
Cclass Book(var title String) {}
Dclass Book(val title: String)
Attempts:
2 left
💡 Hint
Remember the syntax for declaring properties in the primary constructor.
🚀 Application
expert
3: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}")
}
A1 instance created; prints User created: Anna, Total users: 1
B3 instances created; prints User created: Anna, User created: Bob, User created: Cara, Total users: 3
C3 instances created; prints Total users: 3 only
DCompilation error due to missing constructor
Attempts:
2 left
💡 Hint
Each User() call creates an instance and runs init block.