Consider this Kotlin code that uses expressions instead of statements. What will it print?
val result = if (5 > 3) "Yes" else "No" println(result)
Remember, if in Kotlin can return a value.
The if expression returns "Yes" because 5 is greater than 3, so result is assigned "Yes" and printed.
Which of the following is the best reason to prefer expressions over statements in Kotlin?
Think about how expressions can produce values.
Expressions produce values that can be assigned or used immediately, which helps write concise and readable code.
Look at this Kotlin code snippet. What error will it cause?
val x: String = if (true) println("Hello") else println("Bye") println(x)
Consider what println returns and what if expression expects.
println returns Unit, so the if expression returns Unit. Assigning it to val x expecting a String causes a type mismatch.
Choose the Kotlin code snippet that correctly uses expressions to assign a value based on a condition.
Remember the syntax for if expressions in Kotlin.
Option C correctly uses if as an expression with parentheses and both branches returning values.
What is the size of the list result after running this Kotlin code?
val numbers = listOf(1, 2, 3, 4) val result = numbers.map { if (it % 2 == 0) it * 2 else it } println(result.size)
Think about how map transforms each item but keeps the list size.
The map function returns a list with the same number of elements as the original list, here 4.