2. Which of the following is the correct syntax to create an anonymous object in Kotlin?
easy
A. val obj = object() { val x = 10 }
B. val obj = class { val x = 10 }
C. val obj = object { val x = 10 }
D. val obj = fun() { val x = 10 }
Solution
Step 1: Recall Kotlin object expression syntax
The correct syntax uses object { ... } without parentheses.
Step 2: Check each option
val obj = object { val x = 10 } matches the correct syntax. val obj = class { val x = 10 } uses class which is invalid here. val obj = fun() { val x = 10 } uses fun() which is a function, not an object. val obj = object() { val x = 10 } incorrectly adds parentheses after object.
Final Answer:
val obj = object { val x = 10 } -> Option C
Quick Check:
Correct syntax = object { ... } [OK]
Hint: Use 'object { }' without parentheses for anonymous objects [OK]
Common Mistakes:
Adding parentheses after 'object'
Using 'class' instead of 'object'
Confusing functions with object expressions
3. What will be the output of this Kotlin code?
val obj = object {
val name = "Alice"
fun greet() = "Hello, $name!"
}
println(obj.greet())
medium
A. Runtime error
B. Hello, name!
C. Compilation error
D. Hello, Alice!
Solution
Step 1: Analyze the anonymous object
The object has a property name with value "Alice" and a function greet() that returns "Hello, $name!".
Step 2: Evaluate the print statement
Calling obj.greet() returns "Hello, Alice!" which is printed.
4. Identify the error in this Kotlin code using an object expression:
val obj = object {
val x = 5
fun printX() {
println(y)
}
}
medium
A. Property 'y' is not defined in the object
B. Missing parentheses after 'object'
C. Function printX() should return a value
D. Cannot use println inside object expressions
Solution
Step 1: Check properties and functions inside the object
The object defines property x but tries to print y which is not defined.
Step 2: Understand the error cause
Accessing undefined property y causes a compilation error.
Final Answer:
Property 'y' is not defined in the object -> Option A
Quick Check:
Undefined property access = error [OK]
Hint: Check all properties used inside object functions exist [OK]
Common Mistakes:
Assuming missing parentheses after 'object' cause error
Thinking functions must return a value
Believing println is disallowed inside objects
5. How can you use an object expression to create a one-time comparator for sorting a list of strings by length in Kotlin?
hard
A. list.sortedWith(object : Comparator<String> { override fun compare(a: String, b: String) = a.length - b.length })
B. list.sortedWith(object Comparator<String> { fun compare(a: String, b: String) = a.length - b.length })
C. list.sortedWith(object : Comparator<String> { fun compare(a: String, b: String) = a.length - b.length })
D. list.sortedWith(object Comparator<String> { override fun compare(a: String, b: String) = a.length - b.length })
Solution
Step 1: Understand object expression with interface implementation
To create a comparator, use object : Comparator<String> and override compare method.
Step 2: Check syntax correctness
list.sortedWith(object : Comparator<String> { override fun compare(a: String, b: String) = a.length - b.length }) correctly uses override fun compare. Options B and D miss the colon before interface or override keyword. list.sortedWith(object : Comparator<String> { fun compare(a: String, b: String) = a.length - b.length }) misses override keyword.
Final Answer:
list.sortedWith(object : Comparator<String> { override fun compare(a: String, b: String) = a.length - b.length }) -> Option A