Complete the code to define a secondary constructor in the class.
class Person { var name: String constructor(name: String) { this.name = [1] } }
this.name on the right side instead of the parameter.The secondary constructor assigns the parameter name to the property name using this.name = name.
Complete the code to call the primary constructor from the secondary constructor.
class Person(val name: String) { constructor() : this([1]) { println("Secondary constructor called") } }
name without defining it.this.name which is not valid here.The secondary constructor calls the primary constructor with a default name "Unknown".
Fix the error in the secondary constructor call to the primary constructor.
class Person(val name: String) { constructor(age: Int) : this([1]) { println("Age is $age") } }
age directly which is an Int."age" as a string literal which is not meaningful here.The secondary constructor must call the primary constructor with a String. Passing "Unknown" is correct.
Fill both blanks to create a secondary constructor that calls the primary constructor with a default name and prints a message.
class Person(val name: String) { constructor() : this([1]) { println([2]) } }
The secondary constructor calls the primary constructor with "Guest" and prints "Secondary constructor called".
Fill all three blanks to define a class with a primary constructor and two secondary constructors calling it with different default names.
class Person(val name: String) { constructor() : this([1]) { println([2]) } constructor(age: Int) : this([3]) { println("Age is $age") } }
The first secondary constructor calls the primary with "Default" and prints "No name provided". The second calls it with "Unknown".