Bird
Raised Fist0
Kotlinprogramming~20 mins

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

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

Practice

(1/5)
1. What is true about a nested class in Kotlin compared to an inner class?
easy
A. A nested class can access all members of the outer class directly.
B. A nested class does not have access to the outer class's members.
C. A nested class is the same as an inner class.
D. A nested class must be declared with the inner keyword.

Solution

  1. Step 1: Understand nested class independence

    In Kotlin, a nested class is static by default and cannot access outer class members.
  2. Step 2: Compare with inner class

    Only inner classes (declared with inner) can access outer class properties and functions.
  3. Final Answer:

    A nested class does not have access to the outer class's members. -> Option B
  4. Quick Check:

    Nested class independence = A [OK]
Hint: Remember: nested = no outer access, inner = has outer access [OK]
Common Mistakes:
  • Confusing nested and inner classes
  • Thinking nested classes access outer members
  • Assuming nested classes require 'inner' keyword
2. Which of the following is the correct syntax to declare a nested class inside an outer class in Kotlin?
easy
A. class Outer { inner class Nested { } }
B. class Outer { nested class Nested { } }
C. class Outer { static class Nested { } }
D. class Outer { class Nested { } }

Solution

  1. Step 1: Recall Kotlin nested class syntax

    In Kotlin, nested classes are declared simply inside the outer class without any keyword.
  2. Step 2: Identify incorrect options

    inner declares an inner class, static and nested keywords do not exist in Kotlin.
  3. Final Answer:

    class Outer { class Nested { } } -> Option D
  4. Quick Check:

    Nested class syntax = B [OK]
Hint: Nested class = just class inside class, no keywords needed [OK]
Common Mistakes:
  • Using 'static' keyword (Java style)
  • Using 'nested' keyword (not valid Kotlin)
  • Confusing inner and nested class syntax
3. What will be the output of this Kotlin code?
class Outer {
    val x = 10
    class Nested {
        fun printX() = "Cannot access x"
    }
}
fun main() {
    val nested = Outer.Nested()
    println(nested.printX())
}
medium
A. Cannot access x
B. NullPointerException
C. Compilation error
D. 10

Solution

  1. Step 1: Analyze nested class access

    The nested class cannot access the outer class property x because it is not an inner class.
  2. Step 2: Check printX() function

    It returns the string "Cannot access x" explicitly, so that will be printed.
  3. Final Answer:

    Cannot access x -> Option A
  4. Quick Check:

    Nested class method output = D [OK]
Hint: Nested classes can't access outer properties directly [OK]
Common Mistakes:
  • Expecting nested class to print 10
  • Assuming compilation error due to access
  • Confusing inner and nested class behavior
4. Identify the error in this Kotlin code snippet:
class Outer {
    val y = 5
    class Nested {
        fun printY() = y
    }
}
medium
A. Nested class cannot access 'y' directly; must be inner class.
B. Missing 'inner' keyword before Nested class declaration.
C. Property 'y' must be static to be accessed.
D. No error; code is correct.

Solution

  1. Step 1: Check nested class access rules

    A nested class cannot access outer class properties directly because it is static by default.
  2. Step 2: Understand how to fix

    To access outer properties, the class must be declared as inner, but here it is not.
  3. Final Answer:

    Nested class cannot access 'y' directly; must be inner class. -> Option A
  4. Quick Check:

    Nested class access error = A [OK]
Hint: Nested class can't access outer vars; use inner class instead [OK]
Common Mistakes:
  • Thinking 'inner' keyword is optional
  • Assuming 'y' can be static in Kotlin
  • Ignoring nested class static nature
5. Given this Kotlin code, what is the correct way to allow Nested to access outerValue from Outer class?
class Outer(val outerValue: Int) {
    class Nested {
        fun printValue() = "Cannot access outerValue"
    }
}
hard
A. Pass outerValue as a parameter to Nested's constructor.
B. Make outerValue static to access from Nested.
C. Change Nested to an inner class and access outerValue directly.
D. Use a companion object inside Nested to access outerValue.

Solution

  1. Step 1: Understand nested vs inner class access

    Nested classes cannot access outer class properties unless declared inner.
  2. Step 2: Apply inner class to access outerValue

    Changing Nested to inner class Nested allows direct access to outerValue.
  3. Final Answer:

    Change Nested to an inner class and access outerValue directly. -> Option C
  4. Quick Check:

    Use inner class for outer access = C [OK]
Hint: Use 'inner' keyword to access outer class properties [OK]
Common Mistakes:
  • Trying to make properties static (not Kotlin style)
  • Passing outerValue manually instead of inner class
  • Using companion object incorrectly for instance data
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