Question 3Easy
What is the output?
fun greet(name: String = "World") = "Hello, $name"
println(greet())
- Hello, name
- Hello,
- Hello, World
- Compile error
Answer: C. Hello, World
Explanation
A default parameter value is used when the argument is omitted, and $name interpolates it.
Step-by-step solution
- The function greet has a default parameter 'World' for 'name'.
greet() calls the function without an argument, so 'name' uses default value 'World'.$name interpolates the value of 'name', which is 'World'.- So the output becomes 'Hello, World'.
- The correct answer is Hello, World.
Why the other options are wrong
- A. $name is an interpolation, so the parameter value World is inserted, not the word name.
- B. The string has the full text Hello, followed by the default value World.
- D. A default parameter is valid Kotlin, so this code compiles and prints a greeting.
Tip: Remember default parameters and string interpolation in Kotlin.
Question 5Medium
In list.map { it * 2 }, what is it?
- A reference to the list itself that map is called on
- A reserved keyword for the receiver of the call
- The implicit name of a lambda's single parameter
- The index of the current element in the list
Answer: C. The implicit name of a lambda's single parameter
Explanation
When a lambda has exactly one parameter, Kotlin lets you refer to it as it.
Step-by-step solution
- In Kotlin,
it is used to refer to the single parameter of a lambda. list.map { it * 2 } applies a function to each element in the list.- The
it here represents each element as the lambda is called on them. - This makes
it the implicit name for the lambda's single parameter. - So,
it refers to the current element being processed in the list. - Final answer: The implicit name of a lambda's single parameter.
Why the other options are wrong
- A. it is the lambda parameter, which holds each element in turn, not the list itself.
- B. it is not a keyword; it is only an implicit name for a single lambda parameter.
- D. map does not pass an index; use withIndex or mapIndexed to get indexes.
Tip: Remember: In Kotlin, it is the default parameter name for single-argument lambdas.
Question 8Medium
Why is list.filter { it > 2 } written without parentheses?
- filter is a keyword
- When the last parameter is a lambda it can be placed outside (or replace) the parentheses
- Kotlin ignores parentheses
- it is a reserved call operator
Answer: B. When the last parameter is a lambda it can be placed outside (or replace) the parentheses
Explanation
Trailing lambda syntax lets a final function-type argument go after the call parentheses, and the parentheses may be dropped if empty.
Step-by-step solution
- filter takes one lambda parameter.
- It is the last parameter.
- So braces alone are enough.
Why the other options are wrong
- A. filter is an ordinary function.
- C. They are required in other cases.
- D.
it is just the implicit parameter name.
Question 10Hard
What does the inline modifier on a higher-order function primarily do?
- Copies the function body and lambda into call sites, avoiding lambda allocation
- Makes the function run on a background thread pool automatically
- Makes the function execute at compile time only, never at runtime
- Prevents the function from being overridden in any subclass
Answer: A. Copies the function body and lambda into call sites, avoiding lambda allocation
Explanation
Inlining removes the overhead of creating function objects for lambdas and also permits non-local returns from them.
Step-by-step solution
- Inline modifier copies lambda body into call sites, avoiding object creation.
- This reduces memory usage by eliminating lambda allocation overhead.
- It allows direct execution of the lambda code at call site.
- No function object is created for the lambda expression.
- This improves performance by reducing indirection.
- Final answer: Copies the function body and lambda into call sites, avoiding lambda allocation.
Why the other options are wrong
- B. inline does not add threads; the function body still runs on the calling thread.
- C. inline copies code into call sites; the function still runs at runtime.
- D. Override control comes from open and final; inline does not change overriding.
Tip: Use inline for lambdas to avoid object creation and improve efficiency.