Kotlin Beginner Quiz
30 questions on Kotlin Basics.
Question 1: Which keyword declares an immutable variable in Kotlin?
- A. var
- B. val β (correct answer)
- C. const
- D. static
Explanation: The val keyword creates a read-only (immutable) variable in Kotlin, which can be assigned a value only once. var is used to declare mutable variables.
Question 2: What is the entry point function for every executable Kotlin program?
- A. start()
- B. main() β (correct answer)
- C. run()
- D. begin()
Explanation: Just like Java, Kotlin execution starts in the main() function. In Kotlin, it is declared at the package level: fun main() { }.
Question 3: How do you denote a variable that can hold null in Kotlin?
- A.
var x: Int
- B.
var x: Int? β (correct answer)
- C.
var x: Nullable<Int>
- D.
var x: Int!
Explanation: Kotlin has built-in null safety. To allow a variable to hold null, its type must be explicitly declared with a question mark suffix: Int?.
Question 4: What will be the output of the following Kotlin code?
``kotlin
fun main() {
var x = 5
x = 10
println(x)
}
``
- A. 5
- B. 10 β (correct answer)
- C. Compiler Error
- D. Garbage Value
Explanation: x is declared using the var keyword, which means it is mutable. It is initialized to 5, reassigned to 10, and printed.
Question 5: Which operator is called the "Elvis Operator" in Kotlin, used to provide a default value for null expressions?
- A. ?.
- B. !!
- C. ?: β (correct answer)
- D. ::
Explanation: The Elvis Operator ?: evaluates the expression on its left; if that expression is not null, it returns it. Otherwise, it returns the expression on its right.
Question 6: Which keyword is used to declare a function in Kotlin?
- A. function
- B. define
- C. fun β (correct answer)
- D. void
Explanation: In Kotlin, functions are declared using the fun keyword. Example: fun add(a: Int, b: Int): Int { return a + b }.
Question 7: What is the default return type of a Kotlin function if no return type is specified?
- A. void
- B. Void
- C. Unit β (correct answer)
- D. Nothing
Explanation: In Kotlin, functions that do not return a meaningful value return the type Unit. It corresponds to void in Java, but is a real object type.
Question 8: How do you declare a read-only list in Kotlin?
- A.
val list = list(1, 2, 3)
- B.
val list = listOf(1, 2, 3) β (correct answer)
- C.
val list = arrayListOf(1, 2, 3)
- D.
val list = mutableListOf(1, 2, 3)
Explanation: The helper function listOf() creates a read-only (immutable) list. mutableListOf() creates a list that can be updated.
Question 9: Which keyword is used to create a subclass or inherit a class in Kotlin?
- A. extends
- B. : β (correct answer)
- C. implements
- D. inherits
Explanation: Kotlin uses the colon : operator for both inheritance and implementing interfaces. Example: class Dog : Animal().
Question 10: How are classes declared open for inheritance in Kotlin by default?
- A. They are open by default
- B. Using the
open keyword β (correct answer)
- C. Using the
public keyword
- D. Using the
inheritable keyword
Explanation: All classes in Kotlin are final by default (cannot be inherited). To allow a class to be inherited, it must be explicitly marked with the open keyword.
Question 11: What is standard string interpolation syntax in Kotlin?
- A.
"Hello " + name
- B.
"Hello $name" β (correct answer)
- C.
"Hello {name}"
- D.
"Hello @name"
Explanation: Kotlin supports string templates. A dollar sign prefix $ interpolates a variable directly within double quotes. For expressions, use ${expr}.
Question 12: Which operator forces a nullable variable to be treated as non-null, throwing a NullPointerException if it is null?
- A. ?.
- B. ?:
- C. !! β (correct answer)
- D. as?
Explanation: The double-bang operator !! converts any value to a non-null type, throwing a NullPointerException if the value is null. Use with caution.
Question 13: What will the following code print?
``kotlin
fun main() {
val a = 10
val b = 20
val max = if (a > b) a else b
println(max)
}
``
- A. 10
- B. 20 β (correct answer)
- C. true
- D. Compiler Error
Explanation: In Kotlin, if is an **expression**, meaning it returns a value. There is no ternary operator ? : in Kotlin because standard if-else handles it.
Question 14: Which class represents a key-value collection in Kotlin?
- A. List
- B. Set
- C. Map β (correct answer)
- D. Array
Explanation: A Map holds pairs of keys and values, supporting retrieval of values based on keys.
Question 15: What keyword is used in Kotlin to declare a singleton or a companion object?
- A. static
- B. singleton
- C. object β (correct answer)
- D. instance
Explanation: The object keyword declares a class and creates a single instance of it (the Singleton pattern) concurrently.
Question 16: What is a primary constructor in Kotlin?
- A. A constructor declared in the class header β (correct answer)
- B. The first constructor declared inside class body
- C. A static constructor
- D. A constructor without parameters
Explanation: The primary constructor is part of the class header, declared directly after the class name: class User(val name: String).
Question 17: What block is used to initialize variables or run setup code for a primary constructor in Kotlin?
- A. constructor { }
- B. init { } β (correct answer)
- C. setup { }
- D. main { }
Explanation: Because the primary constructor cannot contain block statements, setup code is placed inside initialization blocks prefixed with init.
Question 18: Which keyword is used to check the dynamic type of an object in Kotlin (similar to instanceof)?
- A. is β (correct answer)
- B. as
- C. instanceof
- D. typeof
Explanation: The is operator checks if an expression is an instance of a specified type, automatically casting it (Smart Cast) if true.
Question 19: What will be the output?
``kotlin
fun main() {
val name: String? = null
println(name?.length)
}
``
- A. null β (correct answer)
- B. 0
- C. NullPointerException
- D. Compiler Error
Explanation: The safe call operator ?. returns the property if the object is not null, otherwise it returns null safely without crashing.
Question 20: Which Kotlin collection function is used to filter out elements that don't match a condition?
- A. map
- B. filter β (correct answer)
- C. reduce
- D. select
Explanation: filter returns a list containing only elements matching the given predicate block.
Question 21: Which statement is used as a replacement for the switch statement in Kotlin?
- A. select
- B. match
- C. when β (correct answer)
- D. choose
Explanation: Kotlin uses the when expression, which is a powerful multi-way branching statement replacing the standard switch statement of Java/C.
Question 22: What does the data keyword do when prefixing a Kotlin class?
- A. Speeds up class loading
- B. Instructs the compiler to automatically generate
toString(), equals(), hashCode(), and copy() methods β (correct answer)
- C. Makes class fields public
- D. Enables database ORM mapping
Explanation: Data classes (data class User(val name: String)) automatically generate standard boilerplate methods, making them perfect for holding immutable state/data.
Question 23: What is a companion object in Kotlin?
- A. An object belonging to a subclass
- B. An object declared inside a class that allows calling members like static methods in Java β (correct answer)
- C. An inheritance handler
- D. A duplicate instance
Explanation: Kotlin does not support standard static variables/methods. Instead, classes can declare a companion object, exposing methods to be called using the class name directly.
Question 24: What is the output?
``kotlin
fun main() {
val numbers = intArrayOf(1, 2, 3)
println(numbers[0])
}
``
- A. 1 β (correct answer)
- B. 2
- C. 3
- D. Compiler Error
Explanation: intArrayOf creates a primitive array of integers. Index 0 references the first element, which is 1.
Question 25: What is smart casting in Kotlin?
- A. Explicit conversion using the
as keyword
- B. Automatic compiler-managed type casting after a type check (
is) β (correct answer)
- C. Virtual machine dynamic optimization
- D. Dynamic memory reallocation
Explanation: If you check a variable's type using is, the compiler automatically casts it to the target type within that scope block, eliminating manual casting boilerplate.
Question 26: What is the difference between == and === in Kotlin?
- A.
== checks structural equality (calls equals()); === checks referential equality (points to same object) β (correct answer)
- B. They are identical
- C.
=== is for numbers only
- D.
== is faster
Explanation: In Kotlin, == represents structural equality (comparable to Java's equals()). === represents referential equality, checking if two references point to the exact same object.
Question 27: What does a high-order function do in Kotlin?
- A. Compiles with absolute speed
- B. Takes another function as a parameter or returns a function β (correct answer)
- C. Belongs to the root system package
- D. Executes concurrent threads
Explanation: A higher-order function is a function that accepts functions as parameters or returns a function. Kotlin treats functions as first-class citizens.
Question 28: What is an extension function in Kotlin?
- A. A subclass method
- B. A function that extends the class functionality without inheriting it β (correct answer)
- C. A system library macro
- D. A class destructor
Explanation: Extension functions let you extend a class with new functionality without having to inherit from it. Example: fun String.customMethod() { }.
Question 29: What is a range expression in Kotlin?
- A.
1 to 10
- B.
1..10 β (correct answer)
- C.
[1-10]
- D.
1...10
Explanation: Kotlin ranges are created using the .. range operator. Example: 1..10 represents integers from 1 to 10 inclusive.
Question 30: What is the purpose of the lateinit modifier in Kotlin?
- A. To delay function execution
- B. To allow a non-null property to be initialized after class creation (e.g., in setup methods) β (correct answer)
- C. To allocate memory dynamically
- D. To declare lazy static variables
Explanation: lateinit is a modifier used on mutable properties (var) of classes, promising the compiler that a non-null property will be initialized before use.