Which of the following is NOT a valid way to handle a potential NullPointerException in Kotlin?
Performing a null check using an if statement.
if
Using the Elvis operator ?: to provide a default value if the variable is null.
?:
Using the safe call operator ?.
?.
Using the let function to execute code only if the variable is not null.
let
What is the primary purpose of extension functions in Kotlin?
To define static methods within a class.
To add new functionality to existing classes without inheriting from them.
To override existing methods of a class.
To create abstract methods that must be implemented by subclasses.
What is the output of the following Kotlin code snippet?
val numbers = listOf(1, 2, 3, 4, 5) val result = numbers.filter { it % 2 == 0 }.map { it * 2 } println(result)
[4, 8]
[2, 4, 6, 8, 10]
[2, 6, 10]
Error
What is the primary advantage of using coroutines in Kotlin?
Improved code readability with asynchronous operations.
Eliminates the need for error handling.
Reduces memory consumption by eliminating objects.
Direct replacement for traditional threads.
What is the difference between any() and all() functions for collections?
any()
all()
any() checks if at least one element matches a condition, while all() checks if all elements match.
any() checks if all elements match a condition, while all() checks if at least one element matches.
any() and all() are interchangeable and produce the same result.
any() returns the first matching element, while all() returns a list of all matching elements.
Can a sealed class be instantiated directly in Kotlin?
No, sealed classes cannot be instantiated directly.
Only if the sealed class is declared as open.
open
Yes, but only inside the same file where it's defined.
Yes, just like regular classes.
What benefit do sealed classes provide when used with when expressions in Kotlin?
when
Sealed classes do not offer any specific benefits with when expressions.
The ability to omit the else branch in certain scenarios.
else
Enhanced performance for pattern matching.
Improved code readability.
Which keyword guarantees immutability for a collection in Kotlin?
val
var
const
How can you find the first element in a list that satisfies a given condition?
filter().first()
find()
search()
first()
How do you throw a custom exception in Kotlin?
By using the try block with an instance of your custom exception class.
try
By using the catch block with an instance of your custom exception class.
catch
Custom exceptions cannot be thrown in Kotlin.
By using the throw keyword with an instance of your custom exception class.
throw