What is the key characteristic that distinguishes a sealed class from a regular class in Kotlin?
Sealed classes cannot have constructors.
Sealed classes cannot have abstract members.
Sealed classes can only be extended within the same file.
Sealed classes are only used for data storage.
Which of the following is NOT a benefit of immutability in functional programming?
Easier to reason about program state.
Improved code readability.
Reduced risk of side effects.
Increased code complexity.
What is the purpose of the finally block in a Kotlin try-catch-finally statement?
finally
try-catch-finally
To specify the type of exception that the catch block can handle.
catch
To execute code regardless of whether an exception is thrown.
To define a new custom exception class.
To handle exceptions thrown in the try block.
try
Which of the following is NOT a valid way to handle a potential NullPointerException in Kotlin?
Using the Elvis operator ?: to provide a default value if the variable is null.
?:
Using the let function to execute code only if the variable is not null.
let
Using the safe call operator ?.
?.
Performing a null check using an if statement.
if
When might it be more appropriate to choose an iterative approach over recursion in Kotlin?
When the recursive solution would be significantly more readable and concise.
When the function needs to be inlined for performance optimization.
When the problem can be solved more efficiently with loops, especially for large inputs.
When the problem involves manipulating immutable data structures.
How do you throw a custom exception in Kotlin?
By using the throw keyword with an instance of your custom exception class.
throw
Custom exceptions cannot be thrown in Kotlin.
By using the try block with an instance of your custom exception class.
By using the catch block with an instance of your custom exception class.
Which of the following is a valid way to define an extension function for the String class in Kotlin?
extension fun String.newMethod() {}
String.fun newMethod() {}
fun String.newMethod() {}
fun newMethod(str: String) {}
What is the primary risk associated with using the !! operator in Kotlin?
!!
It slows down the execution of the program.
It makes the code less readable.
It can lead to NullPointerExceptions if the variable is null.
It can lead to memory leaks.
What is the purpose of the withContext function in Kotlin coroutines?
withContext
Creates a new CoroutineScope.
CoroutineScope
Launches a new coroutine on the main thread.
Cancels all coroutines within a specific scope.
Executes a block of code with a specified CoroutineContext.
CoroutineContext
What is a potential downside of relying heavily on recursion in Kotlin?
It can make the code more difficult to debug and understand compared to iterative solutions.
It prevents the use of higher-order functions like 'map' and 'filter'.
It is not supported for complex data structures like trees and graphs.
It always leads to slower performance than iterative approaches.