In Kotlin, if-else is an expression that returns a value. You can assign the result of an if-else directly to a variable.
Click to reveal answer
beginner
How do you assign a value using if-else expression in Kotlin?
You write: val result = if (condition) value1 else value2. The variable 'result' gets the value based on the condition.
Click to reveal answer
intermediate
Can if-else expression in Kotlin omit the else part when used for assignment?
No. When using if-else as an expression for assignment, you must include the else branch to cover all cases.
Click to reveal answer
beginner
Example: What is the value of 'max' after this code?
val a = 5
val b = 10
val max = if (a > b) a else b
The value of 'max' is 10 because 'a > b' is false, so the else branch returns b which is 10.
Click to reveal answer
intermediate
Why is if-else in Kotlin called an expression and not just a statement?
Because it produces a value that can be assigned or used directly, unlike statements which do not return values.
Click to reveal answer
In Kotlin, how do you assign the larger of two numbers a and b to a variable max using if-else expression?
Aval max = if (a > b) a else b
Bval max = if (a > b) { a }
Cval max = if (a > b) a
Dval max = a > b ? a : b
✗ Incorrect
Option A correctly uses if-else expression with both branches to assign the larger value. Kotlin does not use the ternary operator (option D).
What happens if you omit the else branch in an if-else expression assignment in Kotlin?
AThe variable gets assigned the if branch value or null
BThe else branch is assumed as null
CThe program runs but with a warning
DCompilation error because else is required
✗ Incorrect
Kotlin requires the else branch in if-else expressions used for assignment to ensure all cases are covered.
Which of these is true about if-else in Kotlin?
AIt must always be followed by a semicolon
BIt can only be used as a statement without returning a value
CIt can be used as an expression that returns a value
DIt cannot be assigned to a variable
✗ Incorrect
In Kotlin, if-else is an expression that returns a value and can be assigned to variables.
What is the output of this Kotlin code?
val x = 7
val y = 3
val result = if (x < y) "Less" else "More"
print(result)
A"More"
B"Less"
C7
D3
✗ Incorrect
Since 7 < 3 is false, the else branch "More" is assigned to result and printed.
Why might you prefer if-else expression assignment over traditional if-else statements?
AIt is required in all Kotlin code
BIt makes code shorter and clearer by directly assigning values
CIt avoids the need for variables
DIt runs faster than statements
✗ Incorrect
Using if-else as an expression lets you assign values directly, making code concise and readable.
Explain how if-else expressions work in Kotlin and how they differ from traditional if-else statements.
Think about how you can use if-else to get a value directly.
You got /4 concepts.
Write a Kotlin code snippet that assigns the smaller of two numbers to a variable using if-else expression.
Use val result = if (condition) value1 else value2
You got /4 concepts.
Practice
(1/5)
1. What does the following Kotlin code do? val result = if (score > 50) "Pass" else "Fail"
easy
A. Assigns "Pass" to result if score is greater than 50, otherwise "Fail"
B. Assigns true if score is greater than 50, otherwise false
C. Throws an error because else is missing
D. Assigns the value of score to result
Solution
Step 1: Understand the if-else expression
The code uses if-else as an expression to assign a value to result based on the condition score > 50.
Step 2: Evaluate the condition and assignment
If score is greater than 50, result gets "Pass"; otherwise, it gets "Fail".
Final Answer:
Assigns "Pass" to result if score is greater than 50, otherwise "Fail" -> Option A
Quick Check:
If-else expression assigns value based on condition [OK]
Hint: If-else expression returns a value, not just controls flow [OK]
Common Mistakes:
Forgetting the else part causes a syntax error
Thinking if-else is only for flow, not value assignment
Confusing condition with assignment
2. Which of the following is the correct syntax for assigning a value using if-else expression in Kotlin?
easy
A. val x = if (a > b) 10 else 20
B. val x if (a > b) = 10 else = 20
C. val x = if a > b then 10 else 20
D. val x = if (a > b) { 10 } else { 20 } else
Solution
Step 1: Review Kotlin if-else expression syntax
The correct syntax uses if(condition) value1 else value2 without extra keywords or misplaced braces.
Step 2: Compare options with correct syntax
val x = if (a > b) 10 else 20 matches the correct syntax: val x = if (a > b) 10 else 20.
Final Answer:
val x = if (a > b) 10 else 20 -> Option A
Quick Check:
Correct if-else assignment syntax is val x = if (cond) val1 else val2 [OK]
Hint: Remember: if-else expression is inside assignment with = [OK]
Common Mistakes:
Using 'then' keyword which Kotlin does not have
Misplacing equals signs around else
Adding extra else after braces
3. What is the output of this Kotlin code?
val age = 18
val status = if (age >= 18) "Adult" else "Minor"
println(status)
medium
A. Minor
B. Adult
C. 18
D. Compilation error
Solution
Step 1: Check the condition age >= 18
Since age is 18, the condition age >= 18 is true.
Step 2: Determine the assigned value to status
Because the condition is true, status is assigned "Adult".
Final Answer:
Adult -> Option B
Quick Check:
Condition true assigns "Adult" [OK]
Hint: If condition true, if-else expression returns first value [OK]
Common Mistakes:
Assuming >= 18 is false for 18
Printing age instead of status
Confusing syntax causing compile error
4. Identify the error in this Kotlin code:
val x = if (num > 0) "Positive"
medium
A. Incorrect variable declaration
B. Wrong comparison operator
C. Missing else branch in if-else expression
D. No error, code is correct
Solution
Step 1: Check if-else expression completeness
An if-else expression used for assignment must have both if and else branches.
Step 2: Identify missing else branch
The code only has if (num > 0) "Positive" but no else part, causing a syntax error.
Final Answer:
Missing else branch in if-else expression -> Option C
Quick Check:
If-else expression must include else [OK]
Hint: Always include else when assigning with if-else expression [OK]
Common Mistakes:
Omitting else branch
Thinking if alone can assign value
Ignoring compiler error messages
5. You want to assign a grade based on score using if-else expression assignment in Kotlin. Which code correctly assigns "A" if score >= 90, "B" if score >= 80, else "C"?
hard
A. val grade = if (score >= 90) "A" else if score >= 80 "B" else "C"
B. val grade = if (score >= 90) "A" else (score >= 80) "B" else "C"
C. val grade = if (score >= 90) { "A" } else if (score >= 80) { "B" } else { "C" } else
D. val grade = if (score >= 90) "A" else if (score >= 80) "B" else "C"