Bird
Raised Fist0
Kotlinprogramming~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main difference between a nested class and an inner class in Kotlin?
easy
A. An inner class can access members of the outer class, but a nested class cannot.
B. A nested class can access members of the outer class, but an inner class cannot.
C. Both inner and nested classes can access outer class members equally.
D. Inner classes are static by default, nested classes are not.

Solution

  1. Step 1: Understand nested class behavior

    Nested classes are static by default and cannot access outer class members directly.
  2. Step 2: Understand inner class behavior

    Inner classes use the inner keyword and can access outer class members.
  3. Final Answer:

    An inner class can access members of the outer class, but a nested class cannot. -> Option A
  4. Quick Check:

    Inner class = access outer members [OK]
Hint: Inner classes use 'inner' keyword to access outer class [OK]
Common Mistakes:
  • Thinking nested classes can access outer members
  • Confusing inner classes as static
  • Assuming both classes behave the same
2. Which of the following is the correct syntax to declare an inner class in Kotlin?
easy
A. class Outer { class Inner { } }
B. class Outer { open class Inner { } }
C. class Outer { inner class Inner { } }
D. class Outer { static class Inner { } }

Solution

  1. Step 1: Recall inner class syntax

    In Kotlin, inner classes require the inner keyword to access outer class members.
  2. Step 2: Check options for correct keyword

    class Outer { inner class Inner { } } uses inner class Inner, which is the correct syntax.
  3. Final Answer:

    class Outer { inner class Inner { } } -> Option C
  4. Quick Check:

    Use 'inner' keyword for inner classes [OK]
Hint: Use 'inner' keyword to declare inner classes [OK]
Common Mistakes:
  • Omitting 'inner' keyword for inner classes
  • Using 'static' keyword which is invalid in Kotlin
  • Confusing 'open' with 'inner'
3. What will be the output of the following Kotlin code?
class Outer(val name: String) {
    inner class Inner {
        fun greet() = "Hello, $name"
    }
}

fun main() {
    val outer = Outer("Kotlin")
    val inner = outer.Inner()
    println(inner.greet())
}
medium
A. Hello, null
B. Hello, Kotlin
C. Compilation error
D. Hello,

Solution

  1. Step 1: Understand inner class access

    The inner class can access the outer class property name because it is declared with inner.
  2. Step 2: Trace the code execution

    Creating outer with name "Kotlin" and then inner instance calls greet() which returns "Hello, Kotlin".
  3. Final Answer:

    Hello, Kotlin -> Option B
  4. Quick Check:

    Inner class accesses outer property correctly [OK]
Hint: Inner classes access outer properties directly [OK]
Common Mistakes:
  • Assuming inner class cannot access outer properties
  • Expecting null or empty output
  • Thinking code causes compilation error
4. Identify the error in the following Kotlin code snippet:
class Outer {
    class Nested {
        fun printMessage() = println("Hello from Nested")
    }
}

fun main() {
    val nested = Outer.Nested()
    nested.printMessage()
    println(nested.printMessage())
}
medium
A. Calling println on a function that returns Unit causes an error.
B. Nested class cannot be instantiated without an Outer instance.
C. Nested class must be declared with 'inner' keyword to access printMessage.
D. No errors; code runs and prints correctly.

Solution

  1. Step 1: Check nested class instantiation

    Nested classes are static by default and can be instantiated without an Outer instance, so no error here.
  2. Step 2: Analyze println usage

    The function printMessage() prints a message and returns Unit. Calling println(nested.printMessage()) prints the message and then prints kotlin.Unit, which is allowed.
  3. Step 3: Confirm no errors

    There is no compilation or runtime error; the code runs and produces output as written.
  4. Final Answer:

    No errors; code runs and prints correctly. -> Option D
  5. Quick Check:

    Nested class instantiation and Unit printing both OK [OK]
Hint: Nested classes don't require outer instance [OK]
Common Mistakes:
  • Thinking nested class needs Outer instance
  • Expecting compilation error on nested class
  • Ignoring Unit return type printing
5. Given the following Kotlin code, what will be the output?
class Outer(val x: Int) {
    inner class Inner {
        fun multiply(y: Int) = x * y
    }
    class Nested {
        fun multiply(y: Int) = y * 2
    }
}

fun main() {
    val outer = Outer(5)
    val inner = outer.Inner()
    val nested = Outer.Nested()
    println(inner.multiply(3))
    println(nested.multiply(3))
}
hard
A. 15 6
B. 8 6
C. 15 9
D. Compilation error due to nested class

Solution

  1. Step 1: Calculate inner class multiply

    Inner class accesses outer property x=5, so multiply(3) returns 5*3=15.
  2. Step 2: Calculate nested class multiply

    Nested class method multiplies input by 2, so multiply(3) returns 6.
  3. Step 3: Confirm output lines

    Printing inner.multiply(3) prints 15, printing nested.multiply(3) prints 6 on next line.
  4. Final Answer:

    15 6 -> Option A
  5. Quick Check:

    Inner uses outer x, nested doubles input [OK]
Hint: Inner uses outer data; nested does not [OK]
Common Mistakes:
  • Assuming nested class can access outer x
  • Mixing up multiply results
  • Expecting compilation error
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard