0
0
Kotlinprogramming~20 mins

Inner classes and nested classes in Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Inner and Nested Classes Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of nested vs inner class access
What is the output of this Kotlin code?
Kotlin
class Outer {
    private val outerVal = "Outer"
    class Nested {
        fun nestedFun() = "Nested"
    }
    inner class Inner {
        fun innerFun() = outerVal
    }
}

fun main() {
    val nested = Outer.Nested()
    val inner = Outer().Inner()
    println(nested.nestedFun())
    println(inner.innerFun())
}
A
Outer
Outer
B
Nested
Outer
C
Nested
Nested
D
Outer
Nested
Attempts:
2 left
💡 Hint
Remember that nested classes do not have access to outer class members, but inner classes do.
🧠 Conceptual
intermediate
1:30remaining
Difference between inner and nested classes
Which statement correctly describes the difference between inner and nested classes in Kotlin?
AInner classes can access members of the outer class, nested classes cannot.
BNested classes can access members of the outer class, inner classes cannot.
CBoth inner and nested classes can access members of the outer class.
DNeither inner nor nested classes can access members of the outer class.
Attempts:
2 left
💡 Hint
Think about which class type holds a reference to the outer class instance.
Predict Output
advanced
1:30remaining
Output of accessing outer class property from inner class
What will be printed when running this Kotlin code?
Kotlin
class Container(val name: String) {
    inner class Content {
        fun printName() = println(name)
    }
}

fun main() {
    val container = Container("Box")
    val content = container.Content()
    content.printName()
}
ARuntime exception
BContent
CCompilation error
DBox
Attempts:
2 left
💡 Hint
Inner classes can access outer class properties directly.
Predict Output
advanced
2:00remaining
Error type when accessing outer class property from nested class
What error will this Kotlin code produce?
Kotlin
class Example {
    private val value = 10
    class Nested {
        fun getValue() = value
    }
}

fun main() {
    val nested = Example.Nested()
    println(nested.getValue())
}
ACompilation error: Cannot access private property
BRuntime exception: NullPointerException
CCompilation error: Unresolved reference: value
DNo error, prints 10
Attempts:
2 left
💡 Hint
Nested classes do not have access to outer class instance members.
🧠 Conceptual
expert
2:30remaining
Memory implications of inner vs nested classes
Which statement about memory usage is true regarding Kotlin inner and nested classes?
AInner classes hold a reference to the outer class instance, potentially increasing memory usage.
BNested classes hold a reference to the outer class instance, increasing memory usage.
CBoth inner and nested classes do not hold references to the outer class instance.
DNeither inner nor nested classes affect memory usage related to the outer class.
Attempts:
2 left
💡 Hint
Consider which class type keeps a pointer to the outer class object.