skip to content

Fundamentals

The syntactic surface you touch in every Kotlin file: how declarations are laid out, how values are bound, which built-in types exist, and how branching and operators behave. Interviewers start here because Kotlin's small syntax choices — expressions everywhere, val-first, no implicit conversions — explain most of the surprises that follow.

part ofKotlinoverview, primer and where to startread it →
on this pageshow

explore

questions

139 · 6 sections

How does Kotlin's if differ from Java's if, and what does it mean that if is an expression?

level: juniorimportance: must knowfreq 80%
basics
~10 s

In Kotlin, if can produce a value, so you can assign its result to a variable. Because of that, Kotlin has no separate ternary (?:) operator — you just use if/else.

open as a page

What is the difference between val and var in Kotlin, and why is val-first considered idiomatic?

level: juniorimportance: must knowfreq 90%
basics
~10 s

val makes a read-only reference you can't reassign; var makes a reassignable one. Kotlin prefers val first because it makes code safer and easier to reason about.

open as a page

Explain Kotlin's when as an expression: its forms, exhaustiveness rules, and how it replaces switch.

level: middleimportance: must knowfreq 78%
basics
~10 s

when is Kotlin's flexible replacement for switch. It can return a value, match ranges or types, and needs to cover every case (be exhaustive) when used as an expression.

open as a page

How is try a value-producing expression in Kotlin, and how do optional semicolons and statement termination work?

level: middleimportance: should knowfreq 55%
basics
~10 s

Kotlin's try/catch can return a value you assign to a variable. Semicolons are optional because newlines end statements; you only need them to put two statements on one line.

open as a page

Why does Kotlin make if/when/try expressions instead of statements, and what design trade-offs come with leaning on this?

level: seniorimportance: should knowfreq 40%
basics
~10 s

Making control structures return values lets you assign results directly and skip temporary mutable variables, leading to shorter, safer code. Overusing it, though, can make logic hard to read.

open as a page

In Kotlin, what kinds of declarations can live at the top level of a .kt file, and how does this differ from Java?

level: juniorimportance: must knowfreq 72%
basics
~10 s

A Kotlin file can hold functions, properties (variables), classes, interfaces, objects, and type aliases directly, without putting them inside a class. Java requires almost everything to live inside a class.

open as a page

What is the entry point of a Kotlin program, and what are the valid signatures for `main`?

level: juniorimportance: must knowfreq 75%
basics
~10 s

Every Kotlin program starts in a function called main. You can write fun main() with no parameters, or fun main(args: Array<String>) to receive command-line arguments. The runtime calls it automatically to begin execution.

open as a page

In Kotlin, must a file's package declaration mirror its directory structure on disk? Explain what the compiler actually requires.

level: juniorimportance: must knowfreq 55%
basics
~20 s

No. In Kotlin the package name does not have to match the folder path. The compiler accepts any package name regardless of where the file lives, though matching folders to packages is still recommended for clarity.

open as a page

What is the default visibility of a top-level declaration in Kotlin, and how does that compare to Java?

level: juniorimportance: must knowfreq 60%
basics
~10 s

By default everything in Kotlin is public — visible everywhere. In Java, members with no modifier are package-private (visible only in the same package). Kotlin has no package-private at all.

open as a page

Are semicolons required in Kotlin? When, if ever, do you actually need one?

level: juniorimportance: must knowfreq 55%
basics
~20 s

No. Kotlin ends statements at line breaks, so you almost never write a semicolon. You only need one to put two statements on the same line, like val a = 1; val b = 2.

open as a page

What is `const val` in Kotlin, and how does it differ from a regular `val`?

level: juniorimportance: must knowfreq 70%
basics
~20 s

const val is a value the compiler knows at compile time and copies directly into the code that uses it. A plain val is set when the program runs and is read through a hidden getter method.

open as a page

What is a destructuring declaration in Kotlin, and what does `val (name, age) = person` actually do under the hood?

level: juniorimportance: must knowfreq 70%
basics
~10 s

It unpacks one object into several variables in a single line. Each variable is filled by calling the object's component functions in order: the first gets component1(), the second component2().

open as a page

What is `lateinit var` in Kotlin and what problem does it solve?

level: juniorimportance: must knowfreq 80%
basics
~20 s

lateinit var lets you declare a non-null variable without giving it a value right away. You promise to set it before you read it. It's used when a value isn't known at object creation but will be ready before first use.

open as a page

What is type inference in Kotlin, and how does the compiler decide the type of `val n = 1`?

level: juniorimportance: must knowfreq 80%
basics
~20 s

Type inference lets you skip writing the type. The compiler reads the value on the right and figures out the type itself. For val n = 1 it sees a whole number, so n is an Int.

open as a page

What is the difference between val and var in Kotlin, and when should you reach for each?

level: juniorimportance: must knowfreq 92%
basics
~10 s

val means you set the value once and cannot reassign it. var means you can reassign it later. Prefer val by default and use var only when the value really needs to change.

open as a page

In Kotlin, is Char a numeric type? How do you get the numeric (code-point) value of a Char, and how do you turn a digit Char like '7' into the Int 7?

level: juniorimportance: must knowfreq 70%
basics
~10 s

No. A Char in Kotlin is a character, not a number. To get its underlying code you use .code, and to turn a digit character like '7' into the number 7 you use .digitToInt().

open as a page

In Kotlin, why does `val l: Long = anInt` fail to compile, and how do you fix it?

level: juniorimportance: must knowfreq 75%
basics
~10 s

Kotlin never auto-converts a smaller number type to a bigger one. An Int is not a Long. You must convert it yourself by calling a conversion function, for example anInt.toLong().

open as a page

What numeric types does Kotlin provide for whole and fractional numbers, and how do you write integer literals in hex, binary, with separators, and as a Long?

level: juniorimportance: must knowfreq 70%
basics
~10 s

Whole numbers: Byte, Short, Int, Long. Fractional: Float, Double. Write hex as 0xFF, binary as 0b1010, use underscores like 1_000 for readability, and add an L suffix (1L) to make a Long.

open as a page

What is a triple-quoted (raw) string in Kotlin, and how does it differ from a regular double-quoted string?

level: juniorimportance: must knowfreq 55%
basics
~10 s

A triple-quoted string is wrapped in three quotes. It can span many lines and treats backslashes as plain text, so you don't need escape codes like \n or \.

open as a page

What are Kotlin string templates, and what is the difference between the $name and ${expression} forms?

level: juniorimportance: must knowfreq 80%
basics
~10 s

String templates let you put values inside a string. Use $name to insert a variable, and ${...} to insert a whole expression. Kotlin replaces them with the text of the value.

open as a page

Kotlin has no C-style three-part for loop (for (int i=0; i<n; i++)). How do you write the equivalent loops in Kotlin, and what does the for loop actually require to iterate?

level: juniorimportance: must knowfreq 70%
basics
~10 s

Kotlin's for loop walks over a sequence, not a counter. To count, you loop over a range like 0 until n. For anything else, for works on anything you can iterate, like a list.

open as a page

Kotlin has no ternary operator (a ? b : c). How do you write the equivalent, and what does that tell you about if in Kotlin?

level: juniorimportance: must knowfreq 70%
basics
~20 s

Kotlin has no a ? b : c. You use if/else directly, because if returns a value. Write val m = if (a > b) a else b. The if/else expression takes the place of the ternary.

open as a page

In a nested for loop, which loop does a plain `break` (without a label) terminate, and how do you instead break out of the outer loop?

level: juniorimportance: must knowfreq 70%
basics
~10 s

A plain break only stops the loop directly around it (the innermost one). To stop an outer loop, you put a name (label) on that loop and write break@name.

open as a page

In Kotlin, what does a bare `return` statement inside a lambda passed to `forEach` do, and how is that different from returning from the lambda alone?

level: juniorimportance: must knowfreq 70%
basics
~10 s

A bare return inside a forEach lambda exits the whole surrounding function, not just the loop step. To skip only the current step you write return@forEach, which leaves the lambda and continues the loop.

open as a page

In Kotlin, when must a `when` be exhaustive (cover all cases), and when can it leave cases out? Explain the difference between a `when` expression and a `when` statement.

level: juniorimportance: must knowfreq 70%
basics
~20 s

If you use when to produce a value (like assigning it to a variable or returning it), it must handle every possible case. If you use when just to do an action and ignore its value, it does not have to cover everything.

open as a page

What does 7 / 2 evaluate to in Kotlin, and how do you get a fractional result instead?

level: juniorimportance: must knowfreq 70%
basics
~10 s

7 / 2 is 3, because dividing two whole numbers throws away the part after the decimal point. To get 3.5, make at least one number a decimal, like 7.0 / 2.

open as a page

In Kotlin, what is the difference between == and ===? Which one calls equals()?

level: juniorimportance: must knowfreq 85%
basics
~10 s

== checks if two values are equal (same contents) by calling equals(). === checks if two variables point to the exact same object in memory. Use == for value checks.

open as a page

In Kotlin, what does the `operator` keyword do, and how does an expression like `a + b` connect to a function?

level: juniorimportance: must knowfreq 65%
basics
~10 s

Operators like + or [] are shortcuts. Kotlin turns them into calls to specially named functions you mark with the operator keyword. So a + b actually calls a.plus(b).

open as a page

What does the .. operator do in Kotlin, and how do you check whether a value falls inside the resulting range?

level: juniorimportance: must knowfreq 70%
basics
~10 s

The .. operator builds a range, like 1..10 meaning 1 through 10 inclusive. You check if something is inside it with the in keyword, for example x in 1..10.

open as a page

In Kotlin, what do the comparison operators <, >, <=, >= actually translate to under the hood?

level: middleimportance: must knowfreq 65%
basics
~10 s

They all call a single method named compareTo. The compiler rewrites a < b into a.compareTo(b) < 0, and similarly for the others, comparing the returned number against zero.

open as a page