Interview prep · Stage 1: Kotlin and DSA basics
Kotlin init block interview questions
One opener and four harder follow-ups. Each step builds on the last, the way a real interviewer digs.
Try each question out loud before you open the answer.
Opening question
When does an init block run?
Show answer
class Email(val value: String) {
init {
require("@" in value) { "Not an email: $value" }
}
}Junior answer
When an object is created, after the constructor.
Senior answer
Every time an object is created, as part of the primary constructor. Properties in the class header are set first. Then property initializers and init blocks run from top to bottom, in the order they are written, mixed together. A class can have any number of init blocks.
What they're really checking: Saying init runs after all the properties are set. It runs between them.
Follow-up 1
How can a property of type String be null inside init?
Show answer
class Profile(name: String) {
init { println(describe()) } // greeting = null
val greeting: String = "Hi, $name"
fun describe() = "greeting = $greeting"
}Answer
If it is declared below the init block, its line hasn't run yet, so the field still holds the JVM default, which is null. Reading it directly is a compile error (variable must be initialized), but reading it through a function call compiles and returns null. The fix is to declare properties above the init block that uses them.
What they're really checking: Thinking Kotlin's null safety protects you during construction.
Follow-up 2
Can an init block read a lateinit property?
Show answer
class Settings {
lateinit var theme: String
init { println(theme) } // throws at runtime
}Answer
It compiles, with no error and no warning. If nothing set the property yet, creating the object throws UninitializedPropertyAccessException. If the init block sets it first and reads it after, it works, and the compiler then says lateinit isn't needed.
What they're really checking: Saying it's a compile error.
Follow-up 3
With a secondary constructor, where does the init code end up in bytecode?
Show answer
// Decompiled
public Download(String url) { super(); this.url = url; System.out.println("init: " + url); }
public Download() { this("https://example.com/file.zip"); System.out.println("secondary body"); }Answer
Only in the primary constructor, once. There is no init() method. The secondary constructor calls this(...) first, so all init code runs, and then its own body runs. The compiler copies init code into a secondary constructor only when the class has no primary constructor and that constructor calls super(...).
What they're really checking: Saying init is copied into every constructor.
Follow-up 4
What happens if a parent's init block calls an open function?
Show answer
open class Screen {
init { println("title = ${title()}") }
open fun title(): String = "Untitled"
}
class HomeScreen : Screen() {
private val name = "Home"
override fun title() = name
}
HomeScreen() // title = nullAnswer
The parent class is set up before any of the child's properties. If the parent's init calls an open function that the child overrides, the child's version runs while its properties are still empty, so a non-null String can come back null. The compiler doesn't warn. The Kotlin docs say to avoid open members in constructors, property initializers and init blocks.
What they're really checking: Calling open functions from a base class init block.
Say it in 30 seconds
Show the answer
An init block runs every time an object is created, as part of the primary constructor. Properties in the class header are set first. Then property initializers and init blocks run from top to bottom, in the order they are written, mixed together. So a property declared below an init block is still empty, null or zero, while that block runs. A secondary constructor calls the primary one first, so its own body runs last. In bytecode there is no init method: the code sits inside the primary constructor, once. The parent class is set up before the child, so an open function called from the parent's init sees the child's properties empty.