JetBrains Certified Kotlin Programmer Exam Practice Test

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)
}
A) 1020
B) 30
C) Compilation Error
D) None of the Above

Answer:

B) 30

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?

A) var name: String = null
B) var name: String? = null
C) nullable var name: String = null
D) var name: String = "null"

Answer:

B) var name: String? = null

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

A) true
B) false
C) Compilation Error
D) None of the Above

Answer:

A) true

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?

A) val list = mutableListOf(1, 2, 3)
B) val list = listOf(1, 2, 3)
C) var list = listOf(1, 2, 3)
D) var list = arrayListOf(1, 2, 3)

Answer:

B) val list = listOf(1, 2, 3)

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?

A) obj?.method()
B) obj!!.method()
C) obj.method()
D) nullable obj.method()

Answer:

A) obj?.method()

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?

A) fun sum(a: Int, b: Int): Int = a + b
B) function sum(a: Int, b: Int): Int { return a + b }
C) fun sum(a: Int, b: Int) { return a + b }
D) sum function(a: Int, b: Int): Int = a + b

Answer:

A) fun sum(a: Int, b: Int): Int = a + b

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?

A) Defined inside the class body and marked with the constructor keyword
B) Defined outside the class body and marked with the constructor keyword
C) Defined in the class header
D) There is no primary constructor in Kotlin

Answer:

C) Defined in the class header

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?

A) extends
B) inherits
C) :
D) super

Answer:

C) :

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?

A) final class ClassName
B) sealed class ClassName
C) class ClassName
D) noninheritable class ClassName

Answer:

C) class ClassName

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?

A) super.functionName()
B) base.functionName()
C) parent.functionName()
D) overridden.functionName()

Answer:

A) super.functionName()

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?

A) list.forEach { println(it) }
B) for (item in list) println(item)
C) list.printAll()
D) Both A and B

Answer:

D) Both A and B

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?

A) list.filter { it > 0 }
B) list.where { it > 0 }
C) list.select { it > 0 }
D) list.query { it > 0 }

Answer:

A) list.filter { it > 0 }

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)
A) [1, 2, 3, 4, 5]
B) [2, 4, 6, 8, 10]
C) 10
D) None of the Above

Answer:

B) [2, 4, 6, 8, 10]

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?

A) To map elements of a collection
B) To execute a block of code only if the object is non-null
C) To filter elements of a collection
D) To create a new instance of a class

Answer:

B) To execute a block of code only if the object is non-null

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?

A) By using this
B) By using it
C) By using super
D) By using closure

Answer:

D) By using closure

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?

A) It converts a nullable type to a non-nullable type
B) It checks if an object is null
C) It throws a NullPointerException if the object is null
D) It safely calls a method on a nullable object

Answer:

C) It throws a NullPointerException if the object is null

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?

A) Using try-catch-finally blocks
B) Using error function
C) Using Exception class
D) Both A and B

Answer:

D) Both A and B

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?

A) It is a replacement for switch statement
B) It can be used as an expression or a statement
C) It can have an else branch
D) All of the above

Answer:

D) All of the above

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)
A) 0
B) null
C) Compilation Error
D) None of the Above

Answer:

A) 0

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?

A) Using in keyword
B) Using range function
C) Using between function
D) Using contains function

Answer:

A) Using in keyword

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?

A) To create static members
B) To create companion classes
C) To create object instances
D) To define extension functions

Answer:

A) To create static members

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?

A) Parallel Execution
B) Asynchronous Execution
C) Synchronous Execution
D) Both A and B

Answer:

D) Both A and B

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?

A) Extension Functions
B) Companion Objects
C) Data Classes
D) Sealed Classes

Answer:

A) Extension Functions

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?

A) To hold data
B) To create singleton objects
C) To define extension functions
D) To define sealed classes

Answer:

A) To hold data

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?

A) val is mutable, var is immutable
B) val is immutable, var is mutable
C) There is no difference
D) val is a variable, var is a value

Answer:

B) val is immutable, var is mutable

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!

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top