Challenge - 5 Problems
Nested Class Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of nested class accessing outer class members
What is the output of this Kotlin code?
Kotlin
class Outer { val x = 10 class Nested { fun printX() { // println(x) // Can this access x? println("Nested class cannot access outer class property directly") } } } fun main() { val nested = Outer.Nested() nested.printX() }
Attempts:
2 left
💡 Hint
Remember that Kotlin nested classes do not have access to outer class instance members unless marked inner.
✗ Incorrect
In Kotlin, a nested class is static by default and cannot access members of the outer class instance. To access outer class members, the nested class must be marked with the 'inner' keyword.
❓ Predict Output
intermediate2:00remaining
Output of inner class accessing outer class property
What will this Kotlin program print?
Kotlin
class Outer { val x = 5 inner class Inner { fun printX() { println(x) } } } fun main() { val outer = Outer() val inner = outer.Inner() inner.printX() }
Attempts:
2 left
💡 Hint
Inner classes hold a reference to the outer class instance.
✗ Incorrect
The 'inner' keyword allows the nested class to access members of the outer class instance. So 'x' is accessible and prints 5.
🔧 Debug
advanced2:00remaining
Why does this nested class code fail to compile?
Identify the reason for the compilation error in this Kotlin code.
Kotlin
class Outer { val y = 20 class Nested { fun printY() { println(y) } } } fun main() { val nested = Outer.Nested() nested.printY() }
Attempts:
2 left
💡 Hint
Check the difference between nested and inner classes in Kotlin.
✗ Incorrect
A nested class in Kotlin is static by default and cannot access instance members of the outer class. Marking it 'inner' allows access.
📝 Syntax
advanced2:00remaining
Correct syntax to make nested class access outer class property
Which option correctly modifies the nested class to access the outer class property 'z'?
Kotlin
class Outer { val z = 100 // Modify the nested class below class Nested { fun printZ() { println(z) } } }
Attempts:
2 left
💡 Hint
Only inner classes hold a reference to the outer class instance.
✗ Incorrect
Marking the nested class as 'inner' allows it to access instance properties of the outer class. Companion object properties are accessed differently.
🚀 Application
expert3:00remaining
How many instances are created and why?
Consider this Kotlin code. How many instances of Outer and Inner are created, and why?
class Outer {
val a = 1
inner class Inner {
val b = 2
}
}
fun main() {
val outer1 = Outer()
val inner1 = outer1.Inner()
val outer2 = Outer()
val inner2 = outer2.Inner()
}
Attempts:
2 left
💡 Hint
Inner class instances hold a reference to their specific outer instance.
✗ Incorrect
Each 'Outer()' call creates a new Outer instance. Each Inner instance is tied to the Outer instance it was created from, so two Outer and two Inner instances exist.