What does it mean that if is an expression in Kotlin?
In Kotlin, if can return a value, so you can assign the result of an if expression directly to a variable.
Click to reveal answer
beginner
How do you assign a value using if as an expression?
You write val result = if (condition) value1 else value2. The variable result gets the value from the branch that runs.
Click to reveal answer
intermediate
Can if expression in Kotlin have no else branch?
No, if you use if as an expression to return a value, you must include an else branch to cover all cases.
Click to reveal answer
intermediate
What type does an if expression return?
The if expression returns a value of the common supertype of all branches. For example, if one branch returns Int and another Double, the result type will be Double.
Click to reveal answer
beginner
Show a simple Kotlin example using if as an expression.
Example:
val max = if (a > b) a else b
This assigns the greater of a or b to max.
Click to reveal answer
In Kotlin, what must you include when using if as an expression to assign a value?
AAn else branch
BOnly the if branch
CA loop
DA return statement
✗ Incorrect
When using if as an expression, Kotlin requires an else branch to ensure a value is always returned.
What does this Kotlin code return?
val result = if (x > 0) "Positive" else "Non-positive"
AA Boolean value
BNo value
CAn Int value
DA String value
✗ Incorrect
The if expression returns a String depending on the condition.
Which of these is a valid use of if as an expression in Kotlin?
Aif (a > b) a else b
Bval x = if (a > b) a
Cval x = if (a > b) a else b
Dif (a > b) { a }
✗ Incorrect
Option C includes both if and else branches, making it a valid expression that returns a value.
What type will Kotlin infer for this expression?
val x = if (flag) 10.0 else 20.5
AInt
BDouble
CString
DBoolean
✗ Incorrect
Kotlin infers the common supertype, which is Double because both 10.0 and 20.5 are Doubles.
Why is if as an expression useful in Kotlin?
AIt allows assigning values directly from conditions.
BIt replaces loops.
CIt is faster than statements.
DIt can only be used with numbers.
✗ Incorrect
If as an expression lets you write cleaner code by assigning values directly based on conditions.
Explain how if as an expression works in Kotlin and why it requires an else branch.
Think about how Kotlin needs a value no matter what condition happens.
You got /3 concepts.
Write a Kotlin example using if as an expression to assign the smaller of two numbers to a variable.
Use <code>if (a < b) a else b</code> pattern.
You got /3 concepts.
Practice
(1/5)
1. What does it mean when if is used as an expression in Kotlin?
easy
A. It returns a value that can be assigned to a variable.
B. It can only be used without an else block.
C. It cannot be used inside functions.
D. It always causes a syntax error.
Solution
Step 1: Understand if as expression concept
In Kotlin, if can return a value, unlike some other languages.
Step 2: Recognize usage in assignments
This means you can assign the result of an if expression directly to a variable.
Final Answer:
It returns a value that can be assigned to a variable. -> Option A
Quick Check:
if returns value = D [OK]
Hint: Remember: Kotlin's if returns a value, assignable to variables [OK]
Common Mistakes:
Thinking if cannot return values
Forgetting to include else block
Assuming if is only a statement
2. Which of the following is the correct syntax to assign the result of an if expression to a variable in Kotlin?
easy
A. val result if (x > 0) = "Positive" else "Non-positive"
B. val result = if (x > 0) "Positive" else "Non-positive"
C. val result = if (x > 0) { "Positive" }
D. val result = if x > 0 then "Positive" else "Non-positive"
Solution
Step 1: Check correct Kotlin if expression syntax
The correct syntax uses if (condition) value1 else value2 inside an assignment.
Step 2: Identify errors in other options
val result if (x > 0) = "Positive" else "Non-positive" has wrong order, C misses else, D uses invalid keywords.
Final Answer:
val result = if (x > 0) "Positive" else "Non-positive" -> Option B
Quick Check:
Correct syntax with else = A [OK]
Hint: Always include else and use parentheses in if expression [OK]
Common Mistakes:
Omitting else block
Wrong keyword order
Missing parentheses around condition
3. What is the output of the following Kotlin code?
val x = 5
val result = if (x % 2 == 0) "Even" else "Odd"
println(result)
medium
A. Even
B. 5
C. Odd
D. SyntaxError
Solution
Step 1: Evaluate the condition x % 2 == 0
Since 5 % 2 equals 1, the condition is false.
Step 2: Determine which branch executes
Because the condition is false, the else branch returns "Odd".
Final Answer:
Odd -> Option C
Quick Check:
5 is odd = C [OK]
Hint: Check condition result carefully to pick correct branch [OK]
Common Mistakes:
Assuming 5 is even
Ignoring else branch
Confusing modulo operation
4. Identify the error in this Kotlin code using if as an expression:
val x = 10
val result = if (x > 5) "Big"
medium
A. Incorrect variable declaration
B. Extra parentheses in if condition
C. Wrong comparison operator
D. Missing else branch for the if expression
Solution
Step 1: Check if expression completeness
When using if as an expression, Kotlin requires an else branch to cover all cases.
Step 2: Identify missing else branch
The code lacks an else branch, causing a compilation error.
Final Answer:
Missing else branch for the if expression -> Option D
Quick Check:
if expression needs else = B [OK]
Hint: Always add else when if returns a value [OK]
Common Mistakes:
Omitting else block
Assuming if can be incomplete
Ignoring compiler errors
5. You want to write a Kotlin function that returns "Positive", "Negative", or "Zero" based on an integer input using if as an expression. Which code correctly implements this?
hard
A. fun checkNumber(n: Int): String = if (n > 0) "Positive" else if (n < 0) "Negative" else "Zero"
B. fun checkNumber(n: Int): String { if (n > 0) return "Positive" else if (n < 0) return "Negative" else return "Zero" }
C. fun checkNumber(n: Int): String = if (n > 0) "Positive" else "Negative"
D. fun checkNumber(n: Int): String = if n > 0 then "Positive" else if n < 0 then "Negative" else "Zero"
Solution
Step 1: Understand nested if as expression
Kotlin allows nested if expressions to return values in a single expression.
Step 2: Check each option for correctness
fun checkNumber(n: Int): String = if (n > 0) "Positive" else if (n < 0) "Negative" else "Zero" correctly uses nested if expressions with proper syntax and returns all cases. fun checkNumber(n: Int): String { if (n > 0) return "Positive" else if (n < 0) return "Negative" else return "Zero" } uses statements, not expression style. fun checkNumber(n: Int): String = if (n > 0) "Positive" else "Negative" misses the zero case. fun checkNumber(n: Int): String = if n > 0 then "Positive" else if n < 0 then "Negative" else "Zero" uses invalid syntax.
Final Answer:
fun checkNumber(n: Int): String = if (n > 0) "Positive" else if (n < 0) "Negative" else "Zero" -> Option A
Quick Check:
Nested if expression with else = A [OK]
Hint: Use nested if-else expressions for multiple return values [OK]
Common Mistakes:
Missing else for zero case
Using statement style instead of expression
Incorrect syntax with then keyword
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.