AAD AAD Kotlin & Programming Fundamentals 2 — Questions and Answers
Question 1: What is the key difference between `val` and `var` in Kotlin?
- `val` is for integers and `var` is for strings
- `val` is a read-only reference that cannot be reassigned; `var` is mutable and can be reassigned (Correct answer)
- `val` is local scope only; `var` is global scope
- `val` requires explicit types; `var` uses type inference
Correct answer: `val` is a read-only reference that cannot be reassigned; `var` is mutable and can be reassigned
val declares a read-only reference (like final in Java) that cannot be reassigned after initialization, while var declares a mutable variable that can be reassigned.
Question 2: What is an extension function in Kotlin?
- A function that extends the app's runtime
- A function that adds new functionality to an existing class without modifying its source code (Correct answer)
- A function that handles exceptions automatically
- A coroutine that extends the app lifecycle
Correct answer: A function that adds new functionality to an existing class without modifying its source code
An extension function lets you add new functions to an existing class without inheriting from it or modifying its source, using the syntax fun ClassName.newFunction().
Question 3: What does the `suspend` keyword indicate in Kotlin?
- The function pauses execution of the entire app
- The function can be paused and resumed within a coroutine without blocking the calling thread (Correct answer)
- The function is deprecated
- The function runs on the main thread only
Correct answer: The function can be paused and resumed within a coroutine without blocking the calling thread
A suspend function can suspend coroutine execution at a specific point without blocking the thread, and can only be called from a coroutine or another suspend function.
Question 4: What is a sealed class in Kotlin?
- A class that cannot be instantiated
- A class hierarchy where all subclasses are defined in the same file, enabling exhaustive when expressions (Correct answer)
- A class with all private members
- A class that automatically implements Parcelable
Correct answer: A class hierarchy where all subclasses are defined in the same file, enabling exhaustive when expressions
A sealed class restricts class hierarchies so all direct subclasses must be defined in the same file, allowing exhaustive when expressions without an else branch.
Question 5: What is the `when` expression in Kotlin?
- A loop that runs while a condition is true
- A coroutine delay function
- A replacement for Java's switch statement that matches a value against multiple branches (Correct answer)
- A null-safety operator
Correct answer: A replacement for Java's switch statement that matches a value against multiple branches
Kotlin's when expression matches a value against a series of branches and returns the value of the matching branch, serving as a more powerful switch statement.
Question 6: What does `lateinit` allow you to do in Kotlin?
- Delay the execution of a function
- Declare a non-null var property without initializing it at declaration time (Correct answer)
- Create a property that is initialized only once on first access
- Mark a function as deferred
Correct answer: Declare a non-null var property without initializing it at declaration time
lateinit allows you to declare a non-null var property and initialize it later (typically in onCreate()), avoiding null checks while deferring initialization.
What is the key difference between `val` and `var` in Kotlin?