Kotlin val and var, types, string templates, nullable types, safe calls, the elvis operator, when and control flow.
40 cards · basic cards · AI-written, checked twice. Edit anything.
- What is the difference between val and var in Kotlin?
Show answer
val is immutable (read-only), var is mutable (can be reassigned)
- Does Kotlin require explicit type declarations for variables?
Show answer
No, Kotlin can infer types from the assigned value, though explicit types are allowed
- What is the default integer type in Kotlin?
Show answer
Int, which is a 32-bit signed integer
- How do you declare a String variable in Kotlin?
Show answer
Use the String type, e.g., val name: String = "Alice", or let the compiler infer it
- What values can a Boolean variable hold in Kotlin?
Show answer
Only true or false
- What is the syntax for inserting a variable inside a string template?
Show answer
Use $variableName, e.g., "Hello $name"
- How do you evaluate an expression inside a string template?
Show answer
Wrap it in curly braces: ${expression}, e.g., "The answer is ${2 + 2}"
- How do you print a literal dollar sign in a string template?
Show answer
Use a backslash to escape it: \$, e.g., "Cost is \$100"
- How do you declare a variable that can hold null in Kotlin?
Show answer
Add a question mark after the type: Type?, e.g., val x: Int? = null
- What is the default for a variable type in Kotlin if no question mark is added?
Show answer
Non-nullable, meaning it cannot hold null and must be initialized with a value
- Why does Kotlin require explicit nullable types?
Show answer
To prevent null pointer exceptions by making nullability explicit and forcing you to handle null cases
- What is the syntax for the safe call operator in Kotlin?
Show answer
?. (question mark followed by a dot), e.g., obj?.method()
- What does the safe call operator do when the object is null?
Show answer
It returns null instead of throwing a null pointer exception
- What happens when you use ?. on an object that is not null?
Show answer
The method or property is called normally and the result is returned
- What is the symbol for the elvis operator in Kotlin?
Show answer
?: (question mark followed by colon)