Recall & Review
beginner
What is a primary constructor in Kotlin?
A primary constructor is a concise way to declare and initialize class properties directly in the class header.Click to reveal answer
beginner
What is the purpose of an init block in Kotlin?
An init block contains initialization code that runs when an instance of the class is created, allowing extra setup beyond property declarations.Click to reveal answer
intermediate
How do primary constructor parameters relate to properties?
Parameters in the primary constructor can be used to initialize properties by prefixing them with val or var, making them class properties.Click to reveal answer
intermediate
Can a Kotlin class have multiple init blocks? What happens when it does?Yes, a class can have multiple init blocks. They run in the order they appear in the class, each executing during object creation.Click to reveal answer
advanced
Explain the order of execution when creating an object with a primary constructor and init blocks.
First, the primary constructor parameters are passed. Then, property initializers run. After that, all init blocks execute in order. Finally, the object is fully created.
Click to reveal answer
What keyword do you use to declare a property in the primary constructor?
✗ Incorrect
Properties in the primary constructor must be declared with val (read-only) or var (mutable).
When is the code inside an init block executed?
✗ Incorrect
Init blocks run during object creation, right after the primary constructor parameters are passed.
Can you have multiple init blocks in a Kotlin class?
✗ Incorrect
Multiple init blocks are allowed and execute sequentially in the order they are written.
What happens if you declare a primary constructor parameter without val or var?
✗ Incorrect
Parameters without val or var are just constructor parameters and do not become properties.
Which of these is the correct order during object creation?
✗ Incorrect
First, primary constructor parameters are passed, then property initializers run, followed by init blocks.
Describe how you use a primary constructor and init blocks together to initialize a Kotlin class.
Think about how constructor parameters and init blocks complement each other.
You got /4 concepts.
Explain why you might use multiple init blocks in a Kotlin class.
Consider how splitting initialization into parts can help keep code clear.
You got /4 concepts.