Kotlin Online Quiz

Dive into our Kotlin online quiz, designed to sharpen your skills in Kotlin, a modern programming language that enhances productivity and developer happiness. Kotlin is renowned for its concise syntax, interoperability with Java, and robust tooling support. This quiz covers a range of topics from basic syntax and constructs to advanced features like coroutines and extension functions. Whether you're a beginner or an experienced developer, these questions aim to test your understanding and perhaps teach you something new about Kotlin.

1. What does the following Kotlin code output?

fun main() {
    val numbers = listOf(1, 2, 3, 4, 5)
    println(numbers.filter { it > 3 })
}
a) [4, 5]
b) [1, 2, 3, 4, 5]
c) [3, 4, 5]
d) [1, 2, 3]

2. In Kotlin, what is the use of the vararg keyword?

a) To declare a variable as nullable
b) To mark a variable as volatile
c) To allow a function to accept a variable number of arguments
d) To declare a variable as transient

3. What is the output of the following Kotlin code?

fun main() {
    val x = 5
    val message = if (x > 5) "Greater than 5" else "Less or equal to 5"
    println(message)
}
a) Greater than 5
b) Less or equal to 5
c) Null
d) Error

4. How do you declare a nullable string in Kotlin?

a) String str = null
b) var str: String = null
c) var str: String? = null
d) String? str = null

5. What is the output of the following Kotlin code?

fun main() {
    var counter = 0
repeat(5) { counter++ }
println(counter)
}
a) 1
b) 4
c) 5
d) 0

6. Which of these is the correct way to create a singleton in Kotlin?

a) class Singleton {}
b) object Singleton {}
c) singleton class Singleton {}
d) Singleton object {}

7. What will the following Kotlin code output?

fun main() {
    val name: String? = null
    println(name?.length ?: "null value")
}
a) 0
b) null
c) "null value"
d) Error

8. How do you create a read-only list in Kotlin?

a) val list = listOf(1, 2, 3)
b) var list = listOf(1, 2, 3)
c) val list = mutableListOf(1, 2, 3)
d) var list = mutableListOf(1, 2, 3)

9. What is the output of the following Kotlin code?

fun main() {
    val languages = mutableSetOf("Java", "Kotlin")
    languages.add("Java")
    println(languages.size)
}
a) 1
b) 2
c) 3
d) 4

10. What is the purpose of extension functions in Kotlin?

a) To allow inheritance from multiple classes
b) To extend a class with new functionality without having to inherit from the class
c) To modify the existing API of a class
d) To override methods of a class

11. How do you define a function in Kotlin that returns no value?

a) fun myFunction(): None {}
b) fun myFunction(): Unit {}
c) fun myFunction() {}
d) Both b) and c) are correct

12. Which keyword is used for safe type casting in Kotlin?

a) as
b) is
c) safe
d) as?

13. What is the output of the following Kotlin code?

fun main() {
    val map = mapOf(1 to "a", 2 to "b")
    println(map[2])
}
a) "a"
b) "b"
c) null
d) 2

14. What is the significance of the lateinit keyword in Kotlin?

a) It indicates that a variable will be initialized later in the code
b) It allows a non-null type to be initialized as null
c) It forces a variable to be initialized immediately
d) It guarantees that a variable will never be null

15. What is the output of the following Kotlin code?

fun main() {
    for (i in 1..5 step 2) {
        print(i)
    }
}
a) 135
b) 12345
c) 246
d) 234

16. In Kotlin, which of the following correctly declares a nullable integer array?

a) var numbers: Array<Int> = arrayOfNulls<Int>(3)
b) var numbers: Array<Int?> = arrayOfNulls<Int>(3)
c) var numbers: Array<Int?> = arrayOfNulls<Int?>(3)
d) Both b) and c) are correct

17. What is the purpose of the data keyword in Kotlin?

a) To indicate that a class is a data holder
b) To create a class that provides copy and equals methods among others
c) To define a new data type
d) To ensure data security and encryption

18. What is the output of the following Kotlin code?

fun main() {
    val x = 10
    val y = 9
    println(x > y && x < 12)
}
a) true
b) false
c) error
d) null

19. What does the following Kotlin code snippet output?

fun main() {
    val list = mutableListOf("a", "b", "c")
    list.add("d")
    println(list.size)
}
a) 3
b) 4
c) Error
d) None of the above

20. How do you ensure that a Kotlin function throws an exception when a condition fails?

a) Using the fail keyword
b) Using the assert keyword
c) Using the require function
d) Using the throw keyword

21. What is the output of the following Kotlin code?

fun main() {
    val str = "Kotlin Quiz"
    println(str.substring(7))
}
a) "Quiz"
b) "Kotlin"
c) "Kotlin Quiz"
d) ""

22. How can you convert a list to an array in Kotlin?

a) .toArray()
b) .toList()
c) .toTypedArray()
d) .toArr()

23. What will the following Kotlin code output?

fun main() {
    val nullableName: String? = null
    println(nullableName?.length ?: "No name")
}
a) 0
b) null
c) "No name"
d) Error

24. How do you implement an interface in Kotlin?

a) Using the interface keyword
b) Using the implements keyword
c) By using a colon (:) after the class name
d) By using the extends keyword

25. What is the output of the following Kotlin code?

fun main() {
    val numbers = setOf(1, 2, 3, 4, 5)
    println(numbers.first { it > 3 })
}
a) 3
b) 4
c) 5
d) Error

Comments