Which loop in Kotlin guarantees that the loop body will execute at least once?
for-each loop
for loop
while loop
do-while loop
How do you create an immutable map with key-value pairs in Kotlin?
mutableMapOf("key1" to "value1", "key2" to "value2")
mapOf("key1" to "value1", "key2" to "value2")
hashMapOf("key1" to "value1", "key2" to "value2")
dictionaryOf("key1" to "value1", "key2" to "value2")
How do you define a function in Kotlin?
def myFunction() {}
void myFunction() {}
function myFunction() {}
fun myFunction() {}
What will be printed after executing the following Kotlin code?
for (i in 1..3) { println(i * 2) }
123
1 2 3
2 4 6
246
What is the purpose of single-line comments in Kotlin?
To add explanations or disable code temporarily
To import external libraries
To declare variables
To define code blocks
What is a lambda expression in Kotlin?
A way to define anonymous functions.
A type of loop in Kotlin.
A keyword used for error handling.
A special kind of variable.
What is the difference between emptyList() and mutableListOf() in Kotlin?
emptyList()
mutableListOf()
There is no difference; both are interchangeable.
emptyList() creates an empty immutable list, while mutableListOf() creates an empty mutable list.
emptyList() is used for numbers, while mutableListOf() is used for strings.
emptyList() creates a list with a fixed size, while mutableListOf() creates a list with dynamic size.
How do you specify the return type of a function in Kotlin?
Kotlin infers the return type automatically; no explicit specification is needed.
Using the 'return' keyword followed by the type.
By placing a colon (:) after the function parameters and specifying the type.
By using the '->' symbol followed by the type before the function body.
How do you define a secondary constructor in Kotlin?
By using the 'override' keyword before the constructor declaration.
You can't define multiple constructors in Kotlin.
Using the 'secondary' keyword before the constructor declaration.
By defining another constructor inside the class body using the 'constructor' keyword.
What is the default visibility modifier for classes and members in Kotlin?
internal
private
public
protected