A secondary constructor is an additional constructor in a Kotlin class that allows creating objects with different sets of parameters besides the primary constructor.
Click to reveal answer
beginner
How do you declare a secondary constructor in Kotlin?
Use the keyword <code>constructor</code> inside the class body, followed by parameters and an optional call to the primary constructor using <code>this()</code>.
Click to reveal answer
intermediate
Why would you use a secondary constructor instead of just a primary constructor with default parameters?
Secondary constructors let you provide different ways to create an object when default parameters are not enough or when you want to run different initialization code.
Click to reveal answer
intermediate
What must a secondary constructor do if the class has a primary constructor?
It must delegate to the primary constructor either directly or indirectly by calling this() with appropriate arguments.
Click to reveal answer
beginner
Example: What does this Kotlin code do?<br><pre>class Person(val name: String) {
constructor(name: String, age: Int) : this(name) {
println("Age is $age")
}
}</pre>
It defines a class Person with a primary constructor taking name. The secondary constructor takes name and age, calls the primary constructor with name, and prints the age.
Click to reveal answer
In Kotlin, how do you declare a secondary constructor?
AUsing the keyword <code>init</code>
BUsing the keyword <code>constructor</code> inside the class body
CUsing the keyword <code>fun</code>
DUsing the keyword <code>override</code>
✗ Incorrect
Secondary constructors are declared with the constructor keyword inside the class body.
What must a secondary constructor do if the class has a primary constructor?
AOverride the primary constructor
BCall the superclass constructor using <code>super()</code>
CDo nothing special
DCall the primary constructor using <code>this()</code>
✗ Incorrect
A secondary constructor must delegate to the primary constructor using this().
Which of these is a reason to use a secondary constructor?
ATo provide an alternative way to create an object with different parameters
BTo replace the primary constructor completely
CTo define a function inside the class
DTo override a method
✗ Incorrect
Secondary constructors provide alternative ways to create objects with different parameters.
Can a Kotlin class have multiple secondary constructors?
AYes, multiple secondary constructors are allowed
BNo, only one secondary constructor is allowed
COnly if there is no primary constructor
DOnly if the class is abstract
✗ Incorrect
Kotlin allows multiple secondary constructors in a class.
What happens if a secondary constructor does not delegate to the primary constructor in a class that has one?
AThe primary constructor is called automatically
BThe program runs but with warnings
CCompilation error occurs
DThe secondary constructor is ignored
✗ Incorrect
Kotlin requires secondary constructors to delegate to the primary constructor; otherwise, it causes a compilation error.
Explain what a secondary constructor is in Kotlin and when you might use it.
Think about creating objects with different parameters or initialization.
You got /3 concepts.
Describe the rules for delegation between secondary and primary constructors in Kotlin.
Focus on how constructors connect inside the class.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of a secondary constructor in Kotlin?
easy
A. To create static methods
B. To replace the primary constructor completely
C. To provide additional ways to create an object with different parameters
D. To define functions inside a class
Solution
Step 1: Understand constructors in Kotlin
Kotlin classes can have a primary constructor and one or more secondary constructors to create objects.
Step 2: Role of secondary constructors
Secondary constructors allow creating objects with different sets of parameters, providing flexibility.
Final Answer:
To provide additional ways to create an object with different parameters -> Option C
Quick Check:
Secondary constructors = multiple ways to create objects [OK]
Hint: Secondary constructors add extra ways to create objects [OK]
Common Mistakes:
Thinking secondary constructors replace the primary constructor
Hint: Look for 'constructor' keyword without fun or init [OK]
Common Mistakes:
Adding 'fun' before constructor
Using 'secondary' keyword
Confusing with init blocks
3. What will be the output of the following Kotlin code?
class Person(val name: String) {
var age: Int = 0
constructor(name: String, age: Int) : this(name) {
this.age = age
}
}
fun main() {
val p = Person("Alice", 30)
println("Name: ${p.name}, Age: ${p.age}")
}
medium
A. Name: Alice, Age: 0
B. Name: Alice, Age: 30
C. Name: , Age: 30
D. Compilation error
Solution
Step 1: Understand constructor calls
The secondary constructor calls the primary constructor with this(name), initializing name.
Step 2: Assign age in secondary constructor
The secondary constructor sets this.age = age, so age becomes 30.
Final Answer:
Name: Alice, Age: 30 -> Option B
Quick Check:
Secondary constructor sets age correctly [OK]
Hint: Secondary constructor calls primary, then sets extra properties [OK]
Common Mistakes:
Assuming age stays 0
Thinking name is empty
Expecting compilation error
4. Identify the error in this Kotlin class with a secondary constructor:
A. Secondary constructor must call the primary constructor
B. Cannot assign to val property 'model'
C. Missing return type in secondary constructor
D. println cannot be used inside constructor
Solution
Step 1: Check secondary constructor rules
In Kotlin, a secondary constructor must delegate to the primary constructor using : this(...).
Step 2: Analyze given code
The secondary constructor does not call this(model), so it violates the rule.
Final Answer:
Secondary constructor must call the primary constructor -> Option A
Quick Check:
Secondary constructors require delegation [OK]
Hint: Always delegate secondary constructors to primary with ': this(...)' [OK]
Common Mistakes:
Forgetting to delegate to primary constructor
Trying to assign val property directly
Thinking println is disallowed
5. You want to create a Kotlin class Book with a primary constructor that takes title and a secondary constructor that also takes author. The secondary constructor should reuse the primary constructor and set author. Which code correctly implements this?
hard
A. class Book(val title: String) {
var author: String = "Unknown"
constructor(title: String, author: String) : this(title) {
this.author = author
}
}
B. class Book(val title: String, var author: String) {
constructor(title: String) {
this.title = title
this.author = "Unknown"
}
}
C. class Book(val title: String) {
constructor(title: String, author: String) {
this.title = title
this.author = author
}
}
D. class Book {
constructor(title: String) {
this.title = title
}
constructor(title: String, author: String) {
this.title = title
this.author = author
}
}
Solution
Step 1: Check primary constructor and property initialization
class Book(val title: String) {
var author: String = "Unknown"
constructor(title: String, author: String) : this(title) {
this.author = author
}
} defines primary constructor with title and a mutable author property with default value.