December Code

Kotlin for placements: interview questions by topic

Kotlin is the language of Android development and a growing part of server-side work on the JVM, so it appears in interviews for mobile roles and for backend teams that have moved on from Java. Most candidates arrive knowing Java, and interviewers know it, so the questions concentrate on what Kotlin does differently rather than on what the two share.

That means null safety and the operators around it, data classes and properties instead of hand-written boilerplate, final-by-default classes and sealed hierarchies, lambdas with receivers and the scope functions built on them, collection pipelines and sequences, and coroutines, which have replaced callbacks and threads for asynchronous code on Android. Being able to predict what a short piece of Kotlin prints, and to say which Java habit it breaks, is what these rounds test, whether the role is Android, backend or both.

The pages below take one topic each, with answers whose code was compiled with kotlinc 2.3 and run, and one question exactly as the diagnostic asks it. The diagnostic draws from every Kotlin topic in the bank and names the ones to read first.

New to this? How to prepare for placements — the method — and what the readiness test measures.

Kotlin topics, one page each

8 of the 8 Kotlin topics in the bank have a page so far; the diagnostic draws from all 8.

  1. Kotlin data types interview questions

    Kotlin's types for interviews: numbers and inference, val versus var, conversions, string templates, raw strings, Any, Unit, Nothing, ranges.

  2. Kotlin null safety interview questions

    Kotlin null safety for interviews: nullable types, ?. and ?:, the !! operator, smart casts, lateinit and lazy, null-free collections, Java calls.

  3. Kotlin functions and lambdas interview questions

    Kotlin functions for interviews: default and named arguments, extension functions, lambdas and trailing lambdas, it, references, operators, vararg.

  4. Kotlin classes and data classes interview questions

    Kotlin classes for interviews: data classes, copy, primary constructors and init blocks, properties, enum classes, value classes and visibility.

  5. Kotlin inheritance and interfaces interview questions

    Kotlin inheritance for interviews: open classes and override, abstract classes versus interfaces, sealed types, super calls, object expressions, casts.

  6. Kotlin collections interview questions

    Kotlin collections for interviews: List versus MutableList, map and filter chains, sequences, groupBy and partition, arrays, sorting and maps.

  7. Kotlin scope functions interview questions

    Kotlin scope functions explained: let, run, with, apply and also, this versus it, what each returns, takeIf and takeUnless, and choosing one.

  8. Kotlin coroutines interview questions

    Kotlin coroutines for interviews: suspend functions, launch versus async, structured concurrency, cancellation, Flow and coroutines versus threads.

One question, exactly as the diagnostic asks it

From the Kotlin bank — 30 questions across 8 topics. The answer is marked because this one is public; in a sitting you choose first, then see why each option is right or wrong.

Null Safety · mediumKT-005

What does this Kotlin code print?

val words: List<String?> = listOf("hi", null, "kotlin")
val lens = words.map { it?.length ?: -1 }
println(lens)
  1. 1[2, -1, 6]correct
  2. 2[2, null, 6]
  3. 3[2, 0, 6]
  4. 4It throws a NullPointerException on the null element

For each element, it?.length is the string's length when it is not null and null when it is, without throwing. The Elvis operator ?: returns its left side if that is not null, and its right side otherwise, so the null element maps to -1 while the others map to 2 and 6. The list prints as [2, -1, 6]. [2, null, 6] is what it?.length alone would give. [2, 0, 6] assumes a null string has length 0. ?. exists precisely so that a null receiver does not throw a NullPointerException.

More on this topic: Kotlin null safety interview questions

Measure it

Reading the answers tells you what’s true. A diagnostic tells you what you get wrong.

10 Kotlin questions across its topics, easy to hard, about fifteen minutes. You get a readiness figure with the arithmetic shown, the topics you missed named, and a practice set sized for today. Free: 1 diagnostic a month and 15 problems a day. No card.

By Harshit · updated