Vidyalelo
Computer Science · Q255

Kotlin Programming MCQ for Competitive Exams, Interviews, and Certifications

Engineering and GATE · Computer Science · question 255

Q255

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? open class Attribute class 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"

A.
Replace open with closed
B.
Replace open with sealed
Answer
C.
Replace open with private
D.
Replace open with public

Answer: Option B

Solution

Answer: Option B
No explanation is given for this question Let's Discuss on Board