What is the correct syntax to define a function named 'greet' that takes no arguments and returns nothing in Kotlin?
def greet() {}
function greet() {}
fun greet() {}
void greet() {}
What is the output of the following Kotlin code?
fun main() { val numbers = listOf(1, 2, 3, 4, 5) val doubled = numbers.map { it * 2 } println(doubled) }
10
[2, 4, 6, 8, 10]
Error
[1, 2, 3, 4, 5]
How do you define a secondary constructor in Kotlin?
Using the 'secondary' keyword before the constructor declaration.
By using the 'override' keyword before the constructor declaration.
You can't define multiple constructors in Kotlin.
By defining another constructor inside the class body using the 'constructor' keyword.
How do you pass a lambda expression as an argument to a function in Kotlin?
By simply passing the lambda expression without any special syntax.
By enclosing the lambda expression in parentheses.
Lambda expressions cannot be passed as arguments in Kotlin.
By using the 'function' keyword before the lambda expression.
Which of the following is NOT a valid way to declare an immutable list in Kotlin?
mutableListOf("apple", "banana", "cherry")
listOf("apple", "banana", "cherry")
emptyList<String>().plus("apple").plus("banana").plus("cherry")
listOf<String>("apple", "banana", "cherry")
What is the purpose of the 'when' statement in Kotlin?
To declare a variable
To define a function
To perform different actions based on different conditions
To execute a block of code repeatedly
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 property in Kotlin?
Properties are automatically generated from constructor parameters
Using 'property' keyword
Using 'field' keyword
Using 'var' or 'val' keyword before the variable declaration
fun main() { val result = add(3, 5) println(result) } fun add(a: Int, b: Int): Int = a + b
5
3
8
How do you read a line of input from the user in Kotlin?
input()
scanner.nextLine()
read()
readLine()