You have a when expression for all of the subclasses of the class Attribute. To satisfy the when, you must include an else clause. Unfortunately, whenever a new subclass is added, it returns unknown. You would prefer to remove the else clause so the compiler generates an error for unknown subtypes. What is one simple thing you can do to achieve this?kotlinopen class Attributeclass Href: Attribute()class Src: Attribute()class Alt: Attribute()fun getAttribute(attribute: Attribute) : String { return when (attribute) { is Href -> 'href' is Alt -> 'alt' is Src -> 'src' else -> 'unknown' }}

🎲 Try a Random Question  |  Total Questions in Quiz: 83  |  🧠 Study this quiz with Flashcards
This question is part of a full practice quiz:
Kotlin Quiz — practice the complete quiz, review flashcards, or try a random question.

Kotlin MCQs For LinkedIn Skill Assessments.


You have a when expression for all of the subclasses of the class Attribute. To satisfy the when, you must include an else clause. Unfortunately, whenever a new subclass is added, it returns unknown. You would prefer to remove the else clause so the compiler generates an error for unknown subtypes. What is one simple thing you can do to achieve this?<br>kotlin<br>open class Attribute<br>class Href: Attribute()<br>class Src: Attribute()<br>class Alt: Attribute()<br>fun getAttribute(attribute: Attribute) : String {<br> return when (attribute) {<br> is Href -> 'href'<br> is Alt -> 'alt'<br> is Src -> 'src'<br> else -> 'unknown'<br> }<br>}<br>