Quiz: Classes

Test your understanding of the concepts discussed in this chapter.

1

What is printed by the following code?

class Holder {
    val v1: Int get() = calculate("v1")
    val v2: Int = calculate("v2")

    private fun calculate(propertyName: String): Int {
        println("Calculating $propertyName")
        return 42
    }
}

fun main() {
    val h1 = Holder()
    val h2 = Holder()
    println(h2.v1)
    println(h2.v1)
    println(h2.v2)
    println(h2.v2)
}
A)
Calculating v2
Calculating v2
Calculating v1
42
Calculating v1
42
42
42
B)
Calculating v2
42
Calculating v2
42
Calculating v1
42
Calculating v1
42
C)
Calculating v1
Calculating v2
Calculating v1
Calculating v2
42
42
42
42
D)
Calculating v2
Calculating v2
Calculating v1
42
42
42
42
Question 1 of 50 attempted

Get hands-on with 1400+ tech skills courses.