0
0
Kotlinprogramming~20 mins

Nested class independence from outer in Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Nested Class Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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()
}
ACompilation error: Cannot access 'x' from nested class
BNested class cannot access outer class property directly
CRuntime error
D10
Attempts:
2 left
💡 Hint
Remember that Kotlin nested classes do not have access to outer class instance members unless marked inner.
Predict Output
intermediate
2: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()
}
A0
BCompilation error: Cannot access 'x' from inner class
C5
DRuntime error
Attempts:
2 left
💡 Hint
Inner classes hold a reference to the outer class instance.
🔧 Debug
advanced
2: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()
}
ANested class cannot access outer class property 'y' because it is not marked 'inner'.
BProperty 'y' is private and inaccessible.
CNested class must be instantiated with an outer class instance.
DMissing override keyword for printY() function.
Attempts:
2 left
💡 Hint
Check the difference between nested and inner classes in Kotlin.
📝 Syntax
advanced
2: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)
        }
    }
}
AAdd 'inner' keyword before 'class Nested' and instantiate with outer instance
BMake 'z' a companion object property
CDeclare 'Nested' as a top-level class outside 'Outer'
DAdd 'static' keyword before 'class Nested'
Attempts:
2 left
💡 Hint
Only inner classes hold a reference to the outer class instance.
🚀 Application
expert
3: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() }
AOne Outer instance and one Inner instance.
BOne Outer instance and two Inner instances sharing it.
CTwo Outer instances and one Inner instance shared between them.
DTwo Outer instances and two Inner instances; each Inner is tied to its Outer instance.
Attempts:
2 left
💡 Hint
Inner class instances hold a reference to their specific outer instance.