How do you use a named argument when calling a function in Kotlin?
Explanation:
Named arguments improve code readability by explicitly associating arguments with their corresponding parameters.
What is the output of the following Kotlin code snippet?
val x = 5
if (x > 10) {
println("Greater than 10")
} else if (x < 10) {
println("Less than 10")
} else {
println("Equal to 10")
}
Explanation:
The code checks if the value of 'x' is greater than, less than, or equal to 10. Since 'x' is 5, which is less than 10, the code will print 'Less than 10'.
What is the default visibility modifier for classes and members in Kotlin?
Explanation:
Unless specified otherwise, classes and members in Kotlin are accessible from anywhere, making 'public' the default visibility.
How do you access the element at index 2 in a Kotlin array named 'numbers'?
Explanation:
Kotlin uses square brackets []
for array indexing, similar to many other programming languages.
What is the output of the following code?
for (i in 1..5) {
if (i == 3) continue
println(i)
}
Explanation:
The 'continue' keyword skips the current iteration when 'i' is 3, so 3 is not printed in the output.
Which data type represents a whole number in Kotlin?
Explanation:
The 'Int' data type is used to represent whole numbers in Kotlin, similar to 'int' in Java.
What is the correct syntax to declare a class named 'MyClass' in Kotlin?
Explanation:
In Kotlin, 'class' keyword is used to declare a class. Curly braces '{}' are used to define the class body.
What is the purpose of single-line comments in Kotlin?
Explanation:
Comments, marked by '//' in Kotlin, help explain code to yourself and others, or to temporarily disable parts of code without deleting them.
What is the difference between 'val' and 'var' when declaring a property in Kotlin?
Explanation:
Properties declared with 'val' cannot be reassigned after initialization (similar to 'final' in Java), while 'var' properties can be changed.
What is the size of the array declared in this Kotlin code?
val fruits = arrayOf("apple", "banana")
Explanation:
The code initializes an array named fruits
with two elements: "apple" and "banana". Therefore, the size of the array is 2.
What is the primary difference between listOf()
and mutableListOf()
in Kotlin?
Explanation:
The core distinction lies in mutability. listOf()
generates a read-only list, while mutableListOf()
allows modifications like adding or removing elements after creation.
What is the output of listOf(1, 2, 2, 3).toSet()
in Kotlin?
Explanation:
toSet()
converts the list to a Set, which removes duplicate elements. Sets are represented with curly braces {}.
Which keyword is used to declare a class in Kotlin?
Explanation:
The 'class' keyword is used to define a blueprint for creating objects, which can hold data and functions.
What is the primary constructor in Kotlin?
Explanation:
In Kotlin, the primary constructor is declared directly in the class header, alongside the class name and any parameters it takes.
How do you access the value associated with the key "name" in a Kotlin map named user
?
Explanation:
In Kotlin, you can access the value associated with a key in a map using square brackets []
with the key inside.