Skip to main content
Journey Uncommon Logo
JourneyUncommon
mediumKotlinsealed class hierarchiesSingle-choice MCQ

What is printed by this code involving a sealed hierarchy and an exhaustive `when`?

sealed class Node { object Leaf : Node() data class Branch(val left: Node, val right: Node) : Node() } fun count(n: Node): Int = when (n) { Node.Leaf -> 1 is Node.Branch -> count(n.left) + count(n.right) } fun main() { val tree = Node.Branch(Node.Leaf, Node.Branch(Node.Leaf, Node.Leaf)) println(count(tree)) }