Bird
0
0

Identify the error in this Kotlin code snippet:

medium📝 Debug Q14 of 15
Kotlin - Variables and Type System
Identify the error in this Kotlin code snippet:
fun printLength(obj: Any) {
    if (obj is String) {
        println(obj.length())
    }
}
ANo error, code is correct
BMissing else block after if
CUsing parentheses with <code>length</code> property
DIncorrect use of <code>is</code> operator
Step-by-Step Solution
Solution:
  1. Step 1: Check usage of length on String

    In Kotlin, length is a property, not a function, so it should not have parentheses.
  2. Step 2: Confirm other parts are correct

    The is operator is used correctly and else block is optional.
  3. Final Answer:

    Using parentheses with length property -> Option C
  4. Quick Check:

    Property access no parentheses = D [OK]
Quick Trick: Remember: Kotlin properties have no parentheses [OK]
Common Mistakes:
MISTAKES
  • Adding parentheses to properties like length
  • Thinking else block is mandatory
  • Misusing is operator syntax

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes