Skip to content
tezvyn:

Purpose of safe call (`?.`) and Elvis (`?:`) operators

Source: kotlinlang.orgEasyHow cards are made

Purpose of safe call (`?.`) and Elvis (`?:`) operators

This tests your grasp of Kotlin's idiomatic null safety. Explain the safe call (?.) for chaining on nullables and the Elvis operator (?:) for providing defaults. Combine them in a one-line example. A multi-line if/else is a red flag.

What's really being asked

This question isn't just about syntax; it's a test of your fluency in idiomatic Kotlin. Interviewers want to see that you've moved past Java-style null checks (if (obj != null)) and embraced Kotlin's more expressive and safer constructs. It's a filter for developers who write concise, modern Kotlin versus those who write Java-in-a-Kotlin-file. The core concept is compile-time safety and code readability.

The full answer

A strong answer explains three things in order. First, define the safe call operator (?.). It allows you to access properties or call methods on a nullable reference. If the reference is null, the entire expression evaluates to null without causing a NullPointerException. Second, define the Elvis operator (?:). It takes the expression to its left if it's not null; otherwise, it evaluates and returns the expression to its right. It's a concise way to provide a default value. Third, provide a single-line code example that combines both, demonstrating how to safely get a property's value or a default if the object itself is null.

The mistakes people make

The most common mistake is providing a correct but non-idiomatic answer, like using a multi-line if (user != null) block to explain the concept. This completely misses the point of the question, which is about the operators that replace that boilerplate. Another red flag is confusing the safe call (?.) with the non-null assertion operator (!!), which is generally discouraged and demonstrates a misunderstanding of Kotlin's safety philosophy. Finally, some candidates describe the operators but fail to provide a code example, which is a key part of the prompt.

What usually comes next

Expect questions about other null-safety tools. For example, "When would you use let with a safe call, like user?.let { ... }?" or "In what rare situations is the non-null assertion operator (!!) actually acceptable?" They might also ask you to compare this to Java's Optional.

A concrete example

Consider a nullable User object which may or may not have a name: data class User(val name: String?). If we have val user: User? = null, we can get the name or a default value in one line: val userName = user?.name ?: "Guest". Here, user?.name safely returns null because user is null, and the Elvis operator then provides the default string "Guest". If user was User("Alice"), userName would be "Alice".

Interview question

Given `data class User(val name: String?)` and a nullable `val activeUser: User?`, which Kotlin expression correctly retrieves the user's name, providing "Guest" if `activeUser` is null or its `name` property is null?

  • a.activeUser!!.name ?: "Guest"
  • b.activeUser.name ?: "Guest"
  • c.if (activeUser != null && activeUser.name != null) activeUser.name else "Guest"
  • d.activeUser?.name ?: "Guest"Correct
Why?

Option D correctly uses the safe call operator (?. ) to access 'name' only if 'activeUser' is not null, and then the Elvis operator (?: ) to provide "Guest" if the result is null. Option C, while functionally correct, is considered non-idiomatic Kotlin as it uses a verbose if/else block instead of the more concise and expressive ?. and ?: operators.

Just read this? Test yourself on what you have been reading.

Read the original → kotlinlang.org

You just looked this up. Could you explain it out loud?

That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on kotlin — each one lists the topics its interview covers.

See open roles