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?
Aclass
Bfun
Cval or var
Dinit
✗ 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?
AWhen the program ends
BWhen the class is compiled
CWhen a method is called
DWhen an object of the class is created
✗ 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?
AYes, and they run in the order they appear
BNo, only one init block is allowed
CYes, but only the first one runs
DNo, init blocks are not allowed
✗ 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?
AIt is just a parameter and not a property
BIt becomes a global variable
CIt causes a compile error
DIt becomes a property automatically
✗ 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?
The init block tries to print productId, but the class has only id declared.
Step 2: Confirm variable declaration
Since productId is not declared anywhere, this causes a compilation error.
Final Answer:
Variable productId is not declared -> Option C
Quick Check:
Use declared property names in init block [OK]
Hint: Use exact property names inside init blocks [OK]
Common Mistakes:
Assuming init block can't use println
Thinking primary constructor must be empty
Believing secondary constructor is mandatory
5. You want to create a Kotlin class Employee with a primary constructor that takes name and salary. You also want to print a welcome message and calculate a 10% bonus right after object creation. Which is the best way to do this?
hard
A. Use primary constructor for parameters and an init block to print message and calculate bonus
B. Use only secondary constructors for all initialization
C. Put all code inside the primary constructor parentheses
D. Use companion object to run code after object creation
Solution
Step 1: Understand primary constructor role
Primary constructor is for receiving parameters and initializing properties simply.
Step 2: Use init block for extra setup
Init block runs after constructor to print messages and calculate bonus, keeping code organized.
Step 3: Evaluate other options
Secondary constructors are unnecessary here; code can't go inside constructor parentheses; companion object is unrelated.
Final Answer:
Use primary constructor for parameters and an init block to print message and calculate bonus -> Option A