Examveda

Why does not this code snippet compile?
class Cat (name: String) {
  fun greet() { println("Hello ${this.name}") }
}

fun main() {
  val thunderCat = Cat("ThunderCat")
  thunderCat.greet()
}

A. Because name is a class parameter, not a property-it is unresolved main().

B. In order to create an instance of a class, you need the keyword new

C. The reference to name needs to be scoped to the class, so it should be this.name

D. Classes cannot be immutable. You need to change var to val

Answer: Option A


This Question Belongs to Computer Science >> Kotlin Program

Join The Discussion

Related Questions on Kotlin Program

What is Kotlin?

A. A new version of Java.

B. A JavaScript framework.

C. A statically-typed programming language for the JVM, Android, and browser.

D. A database management system.