Intermediate Kotlin test (Intermediate)
30 questions · 35 minutes · Free
Extension functions, lambdas, sealed classes and coroutines basics.
Take the Intermediate Kotlin test
What it covers
- Extension functions
- Lambdas and higher-order functions
- Sealed classes
- Coroutines basics
- Java interop
Interview format
- Questions: 30 questions — 18 multiple choice, 9 multi-answer, 3 predict-the-output
- Time: 35 minutes, timed from the first question
- Difficulty: Intermediate
- Skill area: Backend
- Price: Free
Earn a certificate
The Intermediate Kotlin test is one of the 5 tests of the Android Developer: Intermediate learning path. Pass it and the other 4 tests at 70% or more and readyforhires issues you a verifiable certificate in your name, with its own code and verify link to share or add to LinkedIn.
Sample questions
5 sample questions from the Intermediate Kotlin test, with the answers. Samples are taken out of the live test, so you will not meet these when you sit it.
Which of the following correctly describes the primary purpose of sealed classes in Kotlin?
- Sealed classes in Kotlin are used to create abstract classes that cannot be instantiated directly.
- Sealed classes in Kotlin are used to represent restricted class hierarchies, where a value can be one of a number of predefined subtypes.
- Sealed classes in Kotlin are used to represent open-ended class hierarchies with no restrictions on subclasses.
- Sealed classes in Kotlin are used to allow unlimited inheritance and are similar to regular classes.
Answer: B. Sealed classes in Kotlin are used to represent restricted class hierarchies, where a value can be one of a number of predefined subtypes.
Sealed classes are used to represent restricted class hierarchies, where a value can be one of a number of predefined subtypes. This is different from regular classes, which can have any number of subtypes. Sealed classes are useful when you want to restrict the set of subclasses that can be created, making them ideal for scenarios like representing different types of data in a closed set.
Consider the following Kotlin code snippet: `sealed class Result; data class Ok(val v: Int) : Result; data class Err(val msg: String) : Result; fun show(r: Result) = when (r) { is Ok -> "ok ${r.v}"; is Err -> "err ${r.msg}" }`. Why does this code fail to compile?
- The `when` expression is exhaustive because the sealed class has no subtypes.
- The `when` expression is exhaustive because all subtypes are covered.
- The `when` expression is not exhaustive because it does not cover all possible subtypes of the sealed class.
- The `when` expression is not exhaustive because the sealed class is declared incorrectly.
Answer: C. The `when` expression is not exhaustive because it does not cover all possible subtypes of the sealed class.
In Kotlin, when using a `when` expression on a sealed class, the compiler ensures that all possible subtypes are covered. If any subtype is missing, the compiler will produce an error. In this case, the `when` expression is not exhaustive because it does not include all subtypes of the sealed class, which is why the code will not compile.
What is the inferred parameter type of the lambda in the following code: `listOf(1, 2, 3).map { it * 2 }`?
- The lambda's parameter type cannot be inferred
- The lambda's parameter type is inferred as Int
- The lambda's parameter type is inferred as Unit
- The lambda's parameter type is inferred as Any
Answer: B. The lambda's parameter type is inferred as Int
In Kotlin, when a lambda is used as a function parameter and the context provides enough information, the compiler can infer the parameter types. In this case, the lambda is passed to a function that expects a function with one parameter of type Int, so the compiler infers the parameter type as Int.
Which statement accurately describes sealed classes in Kotlin?
- Sealed classes in Kotlin are used to represent restricted class hierarchies, where a value can be one of several known subclasses.
- Sealed classes are abstract classes that cannot have any implementations.
- Sealed classes are only used for data classes in Kotlin.
- Sealed classes are used to create interfaces in Kotlin.
Answer: A. Sealed classes in Kotlin are used to represent restricted class hierarchies, where a value can be one of several known subclasses.
Sealed classes are a special kind of class in Kotlin used to represent restricted class hierarchies, where a value can be one of several known subclasses. They are useful for exhaustive when expressions and are typically used for modeling restricted sets of types, such as different types of expressions in a programming language or different states in a state machine.
Which coroutine dispatcher is most appropriate for performing network operations that involve blocking I/O?
- Dispatchers.Default
- Dispatchers.IO
- Dispatchers.Unconfined
- Dispatchers.Main
Answer: B. Dispatchers.IO
Dispatchers.IO is designed for I/O-bound tasks such as network or disk operations, which typically involve blocking operations. It uses a larger thread pool to handle these tasks efficiently. Dispatchers.Default is for CPU-bound tasks, Dispatchers.Main is for UI-related work on Android, and Dispatchers.Unconfined is for tasks that do not require a specific thread.
Frequently asked questions
- How many questions are in the Intermediate Kotlin test?
- 30 questions (18 multiple choice, 9 multi-answer, 3 predict-the-output), with 35 minutes to answer them.
- What does the Intermediate Kotlin test cover?
- Extension functions, Lambdas and higher-order functions, Sealed classes, Coroutines basics, Java interop.
- Is the Intermediate Kotlin test free?
- Yes. It is free to take with a readyforhires account.
- What level is the Intermediate Kotlin test?
- Intermediate, in Backend.