Preparing for the JetBrains Certified Kotlin Programmer exam? You’re in the right place! This practice test includes 25 multiple-choice questions covering various aspects of Kotlin programming. Along with each question, we provide the correct answer and a brief explanation to deepen your understanding.
1. What will be the output of the following Kotlin code?
fun main() {
val x = 10
val y = 20
println(x + y)
}
Answer:
Explanation:
The variables x and y are initialized with 10 and 20, respectively, and then added together. The println function prints the sum, which is 30.
2. Which of the following is the correct way to define a nullable variable in Kotlin?
Answer:
Explanation:
In Kotlin, a variable is made nullable by appending a ? to the type declaration.
3. What will be the result of the following expression? 3 in 1..10
Answer:
Explanation:
The expression checks whether 3 is in the range from 1 to 10, which is true.
4. Which of the following correctly declares a read-only list in Kotlin?
Answer:
Explanation:
listOf is used to declare a read-only list in Kotlin, and val makes it immutable.
5. How can you safely call a method on a nullable object in Kotlin?
Answer:
Explanation:
The ?. (safe call) operator is used to safely call a method or access a property on a nullable object.
6. Which of the following is the correct syntax for a function that takes two integers as parameters and returns their sum?
Answer:
Explanation:
In Kotlin, functions are defined using the fun keyword, followed by the function name, parameters, return type, and body.
7. What is the primary constructor of a Kotlin class?
Answer:
Explanation:
The primary constructor is defined in the class header, alongside the class name, and it can have optional parameters.
8. Q: Which Kotlin keyword is used to extend a class?
Answer:
Explanation:
In Kotlin, the : symbol is used for class extension (inheritance) and interface implementation.
9. How do you mark a class so that it cannot be inherited in Kotlin?
Answer:
Explanation:
In Kotlin, classes are final by default, meaning they cannot be inherited unless marked with the open keyword.
10. How can you call the superclass implementation of an overridden function in Kotlin?
Answer:
Explanation:
The super keyword is used to call the superclass implementation of a function.
11. Which of the following will print all elements of a list in Kotlin?
Answer:
Explanation:
Both forEach lambda and the for-in loop can be used to iterate over and print all elements of a list in Kotlin.
12. How do you filter elements of a list in Kotlin that satisfy a condition?
Answer:
Explanation:
The filter function is used to filter elements of a collection based on a predicate.
13. What will be the output of the following code?
val list = listOf(1, 2, 3, 4, 5)
val result = list.map { it * 2 }
println(result)
Answer:
Explanation:
The map function applies the given lambda function (doubling each element) to each element of the original list, resulting in a new list with the transformed elements.
14. What is the purpose of the let function in Kotlin?
Answer:
Explanation:
The let function is used to execute a block of code only if the receiver object (on which let is invoked) is non-null, and it returns the result of the lambda block.
15. How can a lambda function access the parameters of an outer function in Kotlin?
Answer:
Explanation:
In Kotlin, lambda functions form a closure, meaning they can access and modify variables defined in the outer function.
16. What does the !! operator do in Kotlin?
Answer:
Explanation:
The !! operator asserts that an expression is non-null and throws a NullPointerException if it is null.
17. Q: How do you handle exceptions in Kotlin?
Answer:
Explanation:
In Kotlin, exceptions can be handled using try-catch-finally blocks, and the error function can be used to throw an exception.
18. Which of the following is true about the when expression in Kotlin?
Answer:
Explanation:
The when expression in Kotlin is a powerful control structure that can replace switch statement, be used as both an expression and a statement, and can have an else branch.
19. What is the result of the following code?
val number: Int? = null
val result = number ?: 0
println(result)
Answer:
Explanation:
The ?: (Elvis) operator returns the left-hand expression if it is non-null, and the right-hand expression (0 in this case) otherwise.
20. How can you check if a number is in a specific range in Kotlin?
Answer:
Explanation:
In Kotlin, the in keyword is used to check if a value belongs to a range.
21. What is the purpose of the companion object in Kotlin?
Answer:
Explanation:
A companion object is an object declared inside a class and marked with the companion keyword. It is used to create members that are tied to the class itself rather than instances, similar to static members in Java.
22. What is the advantage of using coroutines in Kotlin?
Answer:
Explanation:
Coroutines in Kotlin enable both parallel and asynchronous execution of code, allowing for more efficient and responsive applications.
23. Which of the following Kotlin features allows you to extend the functionality of existing classes without inheritance?
Answer:
Explanation:
Extension functions allow you to add new functions to existing classes without inheriting from them.
24. What is the primary purpose of data classes in Kotlin?
Answer:
Explanation:
Data classes in Kotlin are used primarily to hold data and automatically generate utility functions like toString(), hashCode(), and equals().
25. What is the difference between val and var in Kotlin?
Answer:
Explanation:
In Kotlin, val is used to declare read-only (immutable) variables, whereas var is used for mutable variables.
This practice test should help you assess your knowledge and understanding of Kotlin programming as you prepare for the JetBrains Certified Kotlin Programmer exam. Reviewing the explanations for each answer will deepen your grasp of Kotlin concepts and improve your confidence in tackling the certification. Good luck, and happy studying!