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 pageshowhide
explore
- How Kotlin Does Basic Syntax5 questions
- Syntax Basics27 questions
- File Structure & Top-Level Declarations5 questions
- fun main & Program Entry5 questions
- Statements vs Expressions6 questions
- Semicolons, Idioms & File-Level Visibility6 questions
- Packages & Imports5 questions
- Variables26 questions
- val vs var5 questions
- Type Inference5 questions
- const & Compile-Time Constants5 questions
- lateinit6 questions
- Destructuring Declarations5 questions
- Basic Types30 questions
- Numbers & Literals5 questions
- Unsigned Types5 questions
- Boolean, Char & String5 questions
- String Templates5 questions
- Raw & Triple-Quoted Strings5 questions
- Explicit Conversions & No Implicit Widening5 questions
- Control Flow31 questions
- if as an Expression5 questions
- when as an Expression5 questions
- when Exhaustiveness5 questions
- for/while & Ranges6 questions
- Labeled break & continue5 questions
- Returns at Labels5 questions
- Operators20 questions
- Arithmetic & Comparison Operators5 questions
- == vs ===5 questions
- Range & in Operators5 questions
- Operator-to-Function Mapping5 questions
questions
139 · 6 sectionsHow does Kotlin's if differ from Java's if, and what does it mean that if is an expression?
basics
~10 sIn 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.
What is the difference between val and var in Kotlin, and why is val-first considered idiomatic?
basics
~10 sval 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.
Explain Kotlin's when as an expression: its forms, exhaustiveness rules, and how it replaces switch.
basics
~10 swhen 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.
How is try a value-producing expression in Kotlin, and how do optional semicolons and statement termination work?
basics
~10 sKotlin'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.
Why does Kotlin make if/when/try expressions instead of statements, and what design trade-offs come with leaning on this?
basics
~10 sMaking 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.
In Kotlin, what kinds of declarations can live at the top level of a .kt file, and how does this differ from Java?
basics
~10 sA 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.
What is the entry point of a Kotlin program, and what are the valid signatures for `main`?
basics
~10 sEvery 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.
In Kotlin, must a file's package declaration mirror its directory structure on disk? Explain what the compiler actually requires.
basics
~20 sNo. 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.
What is the default visibility of a top-level declaration in Kotlin, and how does that compare to Java?
basics
~10 sBy 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.
Are semicolons required in Kotlin? When, if ever, do you actually need one?
basics
~20 sNo. 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.
What is `const val` in Kotlin, and how does it differ from a regular `val`?
basics
~20 sconst 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.
What is a destructuring declaration in Kotlin, and what does `val (name, age) = person` actually do under the hood?
basics
~10 sIt 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().
What is `lateinit var` in Kotlin and what problem does it solve?
basics
~20 slateinit 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.
What is type inference in Kotlin, and how does the compiler decide the type of `val n = 1`?
basics
~20 sType 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.
What is the difference between val and var in Kotlin, and when should you reach for each?
basics
~10 sval 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.
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?
basics
~10 sNo. 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().
In Kotlin, why does `val l: Long = anInt` fail to compile, and how do you fix it?
basics
~10 sKotlin 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().
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?
basics
~10 sWhole 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.
What is a triple-quoted (raw) string in Kotlin, and how does it differ from a regular double-quoted string?
basics
~10 sA 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 \.
What are Kotlin string templates, and what is the difference between the $name and ${expression} forms?
basics
~10 sString 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.
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?
basics
~10 sKotlin'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.
Kotlin has no ternary operator (a ? b : c). How do you write the equivalent, and what does that tell you about if in Kotlin?
basics
~20 sKotlin 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.
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?
basics
~10 sA 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.
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?
basics
~10 sA 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.
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.
basics
~20 sIf 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.
What does 7 / 2 evaluate to in Kotlin, and how do you get a fractional result instead?
basics
~10 s7 / 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.
In Kotlin, what is the difference between == and ===? Which one calls equals()?
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.
In Kotlin, what does the `operator` keyword do, and how does an expression like `a + b` connect to a function?
basics
~10 sOperators 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).
What does the .. operator do in Kotlin, and how do you check whether a value falls inside the resulting range?
basics
~10 sThe .. 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.
In Kotlin, what do the comparison operators <, >, <=, >= actually translate to under the hood?
basics
~10 sThey 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.