0
0
Kotlinprogramming~20 mins

Secondary constructors in Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Secondary Constructor Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of secondary constructor call
What is the output of this Kotlin code using a secondary constructor?
Kotlin
class Person(val name: String) {
    var age: Int = 0

    constructor(name: String, age: Int) : this(name) {
        this.age = age
    }

    fun info() = "$name is $age years old"
}

fun main() {
    val p = Person("Alice", 30)
    println(p.info())
}
AAlice is 30 years old
BAlice is 0 years old
CRuntime error: Uninitialized property
DCompilation error due to secondary constructor
Attempts:
2 left
💡 Hint
Remember that the secondary constructor calls the primary constructor first.
Predict Output
intermediate
2:00remaining
Value of property after secondary constructor
What is the value of 'age' after creating an instance with the secondary constructor?
Kotlin
class Animal(val species: String) {
    var age: Int = 5

    constructor(species: String, age: Int) : this(species) {
        this.age = age
    }
}

fun main() {
    val a = Animal("Dog", 7)
    println(a.age)
}
A7
B5
C0
DCompilation error
Attempts:
2 left
💡 Hint
Check how the secondary constructor sets the 'age' property.
🔧 Debug
advanced
2:00remaining
Identify the error in secondary constructor usage
What error does this Kotlin code produce?
Kotlin
class Car(val brand: String) {
    var year: Int

    constructor(brand: String, year: Int) {
        this.brand = brand
        this.year = year
    }
}

fun main() {
    val c = Car("Toyota", 2020)
    println(c.brand + " " + c.year)
}
ACompilation error: Val cannot be reassigned
BRuntime error: Uninitialized property 'year'
CCompilation error: Secondary constructor must delegate to primary
DNo error, prints 'Toyota 2020'
Attempts:
2 left
💡 Hint
Check how the secondary constructor calls the primary constructor.
📝 Syntax
advanced
2:00remaining
Correct syntax for secondary constructor delegation
Which option shows the correct syntax for a secondary constructor delegating to the primary constructor?
Kotlin
class Book(val title: String) {
    var pages: Int = 0

    // Secondary constructor here
}
Aconstructor(title: String, pages: Int) { this.pages = pages }
Bconstructor(title: String, pages: Int) : this(title) { this.pages = pages }
Cconstructor(title: String, pages: Int) -> this(title) { this.pages = pages }
Dconstructor(title: String, pages: Int) : super(title) { this.pages = pages }
Attempts:
2 left
💡 Hint
Look for the correct delegation syntax using ': this(...)'.
🚀 Application
expert
2:00remaining
Number of instances created with secondary constructor
Given this Kotlin code, how many times is the primary constructor called when creating an instance with the secondary constructor?
Kotlin
class User(val username: String) {
    init {
        println("Primary constructor called for $username")
    }

    constructor(username: String, email: String) : this(username) {
        println("Secondary constructor called for $username with email $email")
    }
}

fun main() {
    val u = User("john_doe", "john@example.com")
}
APrimary constructor is called after secondary constructor
BPrimary constructor is called twice
CPrimary constructor is not called
DPrimary constructor is called once
Attempts:
2 left
💡 Hint
Remember how secondary constructors delegate to primary constructors in Kotlin.