Sample questions
Question 1Easy
Which scope function returns the receiver object itself and uses this inside the block?
- let
- apply
- run
- also
Answer: B. apply
Explanation
apply configures the object (this) and returns that same object.
Step-by-step solution
- Need receiver returned.
- Need
this style. - That is apply.
Why the other options are wrong
- A. let uses
it and returns the lambda result. - C. run uses this but returns the lambda result.
- D. also uses it and returns the receiver.
Tip: apply = configure and return; let = transform.
Question 2Easy
What is the output?
val r = "kotlin".let { it.length + 1 }
println(r)
- 6
- kotlin
- Compile error
- 7
Answer: D. 7
Explanation
let passes the receiver as it and returns the lambda result: 6 + 1 = 7.
Step-by-step solution
- "kotlin".length = 6.
- Add 1.
- let returns 7.
Why the other options are wrong
- A. let returns the lambda result, which is length + 1.
- B. let does not return the receiver.
- C.
it refers to the receiver inside let.
Question 3Easy
What does an extension function allow?
- Adding a function to a class without modifying or inheriting it
- Overriding the private members of a class from outside it
- Changing the primary constructor of a class after it is compiled
- Making a final class open for inheritance at runtime
Answer: A. Adding a function to a class without modifying or inheriting it
Explanation
Extensions are resolved statically and add callable functions to a type from outside.
Step-by-step solution
- Extension functions add functionality to a class without changing it.
- They allow calling new functions as if they are part of the class.
- This is done without modifying or inheriting the class.
- So, the function can be used on instances of the class directly.
- The correct answer is A.
Why the other options are wrong
- B. Extensions cannot override or access private members; they only add new functions.
- C. Extensions cannot change constructors; a class's constructors are fixed at compile time.
- D. Extensions cannot change inheritance; a final class stays final.
Tip: Use extension functions to add methods to classes without inheritance or modification.
Question 4Medium
What is the output?
val x = "kotlin".let { it.length + 1 }
println(x)
- 6
- kotlin
- 7
- 8
Answer: C. 7
Explanation
"kotlin" has 6 characters; let returns the lambda result, 6 + 1 = 7.
Step-by-step solution
- Val x = "kotlin".let { it.length + 1 } calculates length of "kotlin" as 6.
- "kotlin".length + 1 equals 6 + 1 = 7.
- Let returns the result of the lambda, so x is 7.
- println(x) prints 7 to the console.
- Final answer: 7.
Why the other options are wrong
- A. The lambda adds 1 to the length, so the result is 6 + 1 = 7, not 6.
- B. let returns the lambda result, which is an Int, not the original string.
- D. The length of kotlin is 6, and adding 1 gives 7, not 8.
Tip: Remember let returns the lambda's value, not the original object.
Question 5Medium
What is the output?
val sb = StringBuilder().apply {
append("a")
append("b")
}
println(sb)
- Unit
- a
- ab
- Compile error
Answer: C. ab
Explanation
apply runs the block on the StringBuilder and returns it, containing "ab".
Step-by-step solution
- Block runs with this = sb.
- Appends a then b.
- Returns sb: ab.
Why the other options are wrong
- A. apply returns the receiver, not Unit.
- B. Both appends run.
- D. append resolves via this.
Question 6Medium
A class has a member fun f() and you declare an extension fun C.f() with the same signature. Which is called for c.f()?
- The member function
- The extension, because it is newer
- The compiler reports ambiguity
- Whichever is imported last
Answer: A. The member function
Explanation
Members always win over extensions with the same signature, so an extension cannot override a member.
Step-by-step solution
- Members are resolved first.
- Extension is only used if no member fits.
- The member runs.
Why the other options are wrong
- B. Members always win.
- C. No ambiguity exists, the member takes priority.
- D. Import order does not matter.
Question 7Medium
What is the output?
val x = 5.also { println("v=$it") }.plus(1)
println(x)
- 6 only
- v=5, then 6 on the next line
- v=6, then 6
- v=5, then 5
Answer: B. v=5, then 6 on the next line
Explanation
also runs a side effect on 5 and returns 5, then plus(1) gives 6.
Step-by-step solution
- also prints v=5.
- It returns 5.
- 5 + 1 = 6.
Why the other options are wrong
- A. also prints before the result.
- C. also sees the original 5.
- D. plus(1) adds 1.
Question 8Medium
Which scope function returns the lambda result and refers to the object as it?
- apply
- also
- run
- let
Answer: D. let
Explanation
let passes the object as it and returns the lambda's result; apply and also return the object itself.
Step-by-step solution
- The
let function binds the object to it inside the lambda. let returns the result of the lambda expression.- Other functions like
apply and also return the object itself, not the lambda result. - Only
let refers to the object as it and returns the lambda's outcome. - The correct answer is let.
Why the other options are wrong
- A. apply returns the object itself, not the lambda result, and refers to it as this.
- B. also refers to the object as it, but returns the object itself, not the lambda result.
- C. run returns the lambda result but refers to the object as this, not it.
Tip: Use let when you need to perform actions on an object and return its transformed value.
Question 9Hard
What is the output?
open class A { open fun f() = "A" }
class B : A() { override fun f() = "B" }
fun A.g() = "ext-A"
fun B.g() = "ext-B"
val a: A = B()
println(a.f() + a.g())
- Bext-B
- Bext-A
- Aext-A
- Aext-B
Answer: B. Bext-A
Explanation
Member calls are dynamic so f gives "B", but extension functions are resolved statically by declared type A.
Step-by-step solution
- a.f() is a virtual member call; B overrides f, so it returns B.
- a.g() is an extension, chosen by the static type A, so it returns ext-A.
- Result is Bext-A. Marked answer: option B.
Why the other options are wrong
- A. Extensions are chosen by the static type A, so B's extension is not used.
- C. f() is a virtual call and runs B's override, so it gives B, not A.
- D. f() gives B and g() gives ext-A, so the answer is Bext-A.
Tip: Overrides follow the runtime object; extension functions follow the declared type.
Question 10Hard
What is the output?
open class A
class B : A()
fun A.who() = "A"
fun B.who() = "B"
val a: A = B()
println(a.who())
- B
- A
- AB
- Compile error
Answer: B. A
Explanation
Extension functions are dispatched statically on the declared type (A), not the runtime type.
Step-by-step solution
- a is declared as A.
- Extension chosen at compile time.
- A.who() runs: "A".
Why the other options are wrong
- A. Extensions are resolved by the declared type, not runtime type.
- C. Only one function is called.
- D. Both extensions are valid.
Tip: Extensions are not virtual; use member functions for polymorphism.
Practise all 12 questions free
The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.
Open this topic in the app →