The Elvis operator (?:) in Kotlin is a shorthand to provide a default value when an expression is null. It returns the left side if it's not null, otherwise the right side.
Click to reveal answer
beginner
How does the Elvis operator help with null safety?
It lets you handle nullable values concisely by providing a fallback value, avoiding explicit null checks and preventing null pointer exceptions.
Click to reveal answer
intermediate
Explain chaining Elvis operators with an example.
You can chain Elvis operators to check multiple nullable values in order. For example: val result = a ?: b ?: c ?: "default". It returns the first non-null value among a, b, c, or "default" if all are null.
Click to reveal answer
intermediate
How can the Elvis operator be used with function calls?
You can use it to call a function on a nullable object or provide a default if null. Example: val length = name?.length ?: 0 returns the length if name is not null, else 0.
Click to reveal answer
advanced
What happens if you use the Elvis operator with a throw expression?
You can throw an exception as the right side of the Elvis operator to enforce non-null values. Example: val value = nullableValue ?: throw IllegalArgumentException("Value required") stops execution if nullableValue is null.
Click to reveal answer
What does the Elvis operator ?: return if the left side is not null?
ANull
BThe right side value
CThe left side value
DThrows an exception
✗ Incorrect
The Elvis operator returns the left side if it is not null; otherwise, it returns the right side.
Which of the following is a valid use of the Elvis operator?
Aval result = a && b ?: c
Bval result = a ?: b ?: "default"
Cval result = a ? b : c
Dval result = a +: b
✗ Incorrect
Chaining Elvis operators like a ?: b ?: "default" is valid Kotlin syntax to provide fallback values.
What is the output of this code? val name: String? = null; val length = name?.length ?: 0; println(length)
ALength of name
Bnull
CThrows NullPointerException
D0
✗ Incorrect
Since name is null, name?.length is null, so the Elvis operator returns 0.
How can you use the Elvis operator to throw an exception if a value is null?
Aval value = nullable ?: throw Exception()
Bval value = nullable ?: return Exception()
Cval value = nullable ?: println("Error")
Dval value = nullable ?: null
✗ Incorrect
You can throw an exception on the right side of ?: to stop execution if the left side is null.
What is the main benefit of using the Elvis operator in Kotlin?
ASimplifies null checks and provides default values
BReplaces all if statements
CAutomatically converts types
DImproves performance by skipping null checks
✗ Incorrect
The Elvis operator simplifies code by handling nulls with default values, making code cleaner and safer.
Explain how the Elvis operator can be chained to handle multiple nullable variables.
Think about checking several variables one after another.
You got /4 concepts.
Describe how you can use the Elvis operator to throw an exception when a nullable value is null.
Consider what happens if the left side is null and you want to stop the program.
You got /4 concepts.
Practice
(1/5)
1.
What does the Elvis operator ?: do in Kotlin?
easy
A. Checks if two values are equal.
B. Returns the left value if it is not null; otherwise returns the right value.
C. Converts a value to a nullable type.
D. Throws an exception if the left value is null.
Solution
Step 1: Understand the Elvis operator purpose
The Elvis operator ?: is used to provide a default value when the left side is null.
Step 2: Analyze the behavior
If the left value is not null, it returns that value; otherwise, it returns the right value.
Final Answer:
Returns the left value if it is not null; otherwise returns the right value. -> Option B
Quick Check:
Elvis operator returns left if not null else right [OK]
Hint: Elvis operator picks left if not null, else right [OK]
Common Mistakes:
Thinking it compares two values
Assuming it throws exceptions
Confusing it with null checks
2.
Which of the following is the correct syntax to use the Elvis operator in Kotlin?
val result = value1 ??: value2
easy
A. val result = value1 ?: value2
B. val result = value1 ? value2
C. val result = value1 :: value2
D. val result = value1 ?:: value2
Solution
Step 1: Identify the correct Elvis operator syntax
The Elvis operator in Kotlin is written as ?: without spaces.
Step 2: Match the syntax with options
val result = value1 ?: value2 uses ?: correctly, while others use invalid or incorrect symbols.
Final Answer:
val result = value1 ?: value2 -> Option A
Quick Check:
Elvis operator syntax is ?: [OK]
Hint: Elvis operator is always ?: with no spaces [OK]
Common Mistakes:
Using single question mark ? instead of ?:
Using double colon :: by mistake
Adding extra colons or spaces
3.
What is the output of the following Kotlin code?
val a: String? = null
val b: String? = "Hello"
val c: String? = null
val result = a ?: b ?: c ?: "Default"
println(result)
medium
A. Hello
B. null
C. Default
D. Compilation error
Solution
Step 1: Evaluate the chained Elvis operators
Variable a is null, so Elvis operator moves to b. b is "Hello", which is not null.
Step 2: Determine the final value
Since b is not null, result is assigned "Hello" and the rest is ignored.
Final Answer:
Hello -> Option A
Quick Check:
Chained Elvis picks first non-null: Hello [OK]
Hint: Chained Elvis returns first non-null value [OK]
Common Mistakes:
Assuming it returns null if first is null
Ignoring chaining and picking last value
Thinking it causes a compile error
4.
Identify the error in this Kotlin code snippet using the Elvis operator:
val x: String? = null
val y: String = x ?: null
medium
A. Elvis operator cannot be used with nullable types
B. No error, code is correct
C. Variable x should not be nullable
D. Cannot assign null to non-nullable type String
Solution
Step 1: Analyze variable types and assignment
Variable y is declared as non-nullable String, but x ?: null can be null.
Step 2: Understand Kotlin's null safety rules
Assigning null to a non-nullable type causes a compilation error.
Final Answer:
Cannot assign null to non-nullable type String -> Option D
Quick Check:
Non-nullable cannot hold null [OK]
Hint: Non-nullable vars cannot be assigned null [OK]
Common Mistakes:
Thinking Elvis operator changes nullability
Assuming nullable can be assigned to non-nullable
Ignoring Kotlin's strict null safety
5.
Given the following Kotlin code, what will be the output?
fun getName(user: Map): String {
return user["firstName"] ?: user["nickname"] ?: user["lastName"] ?: "Anonymous"
}
val user1 = mapOf("firstName" to null, "nickname" to null, "lastName" to "Smith")
println(getName(user1))
hard
A. Anonymous
B. Compilation error
C. Smith
D. null
Solution
Step 1: Evaluate the map values in order
firstName and nickname are null, so Elvis operator moves to lastName, which is "Smith".
Step 2: Determine the returned value
Since lastName is not null, the function returns "Smith".
Final Answer:
Smith -> Option C
Quick Check:
Elvis chain returns first non-null: Smith [OK]
Hint: Elvis chains pick first non-null map value [OK]
Common Mistakes:
Assuming it returns Anonymous despite lastName present
Confusing null with string "null"
Thinking map access causes error
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.