An inner class in Kotlin is a class defined inside another class that can access the members of its outer class directly.
Click to reveal answer
intermediate
How does an inner class access the outer class's members?
An inner class can access the outer class's properties and functions directly using the <code>this@OuterClassName</code> syntax if needed.
Click to reveal answer
beginner
What keyword is used to declare an inner class in Kotlin?
The <code>inner</code> keyword is used before the class declaration to make it an inner class that holds a reference to the outer class.
Click to reveal answer
intermediate
What happens if you declare a nested class without the <code>inner</code> keyword?
A nested class without <code>inner</code> is static and cannot access the outer class's members directly.
Click to reveal answer
intermediate
Show how to access an outer class property named <code>name</code> from an inner class in Kotlin.
Inside the inner class, use this@OuterClass.name to access the outer class's name property.
Click to reveal answer
Which keyword makes a Kotlin nested class an inner class with access to outer members?
Ainner
Bouter
Cnested
Dstatic
✗ Incorrect
The inner keyword allows the nested class to access the outer class's members.
How do you refer to the outer class instance from inside an inner class?
Athis.outer
Bthis@OuterClassName
Csuper
Douter.this
✗ Incorrect
Use this@OuterClassName to explicitly refer to the outer class instance.
What is true about a nested class without the inner keyword?
AIt cannot access outer class members directly
BIt can access outer class members directly
CIt is the same as an inner class
DIt automatically inherits outer class
✗ Incorrect
A nested class without inner is static and cannot access outer class members.
Which of these is a valid way to declare an inner class in Kotlin?
Astatic inner class InnerClass {}
Bclass inner InnerClass {}
Cclass InnerClass inner {}
Dinner class InnerClass {}
✗ Incorrect
The correct syntax is inner class InnerClass {}.
Why would you use an inner class instead of a nested class?
ATo make the class static
BTo prevent access to outer class
CTo access outer class members easily
DTo avoid creating an instance of outer class
✗ Incorrect
Inner classes hold a reference to the outer class and can access its members.
Explain how an inner class in Kotlin can access the members of its outer class. Include syntax examples.
Think about how the inner class keeps a reference to the outer class instance.
You got /3 concepts.
Describe the difference between a nested class and an inner class in Kotlin regarding access to outer class members.
Focus on the presence or absence of the inner keyword.
You got /3 concepts.
Practice
(1/5)
1. In Kotlin, what keyword must you use to allow an inner class to access members of its outer class?
easy
A. inner
B. outer
C. this
D. nested
Solution
Step 1: Understand inner class behavior
By default, nested classes in Kotlin cannot access outer class members directly.
Step 2: Identify the keyword for access
The keyword inner allows the nested class to hold a reference to the outer class and access its members.
Final Answer:
inner -> Option A
Quick Check:
Use inner for access [OK]
Hint: Remember: only 'inner' classes access outer members directly [OK]
Common Mistakes:
Confusing 'inner' with 'nested' keyword
Using 'this' instead of 'inner'
Assuming all nested classes access outer members
2. Which of the following Kotlin class declarations correctly allows the inner class to access the outer class's property name?
easy
A. class Outer { val name = \"Kotlin\"; inner class Inner { fun printName() = println(name) } }
B. class Outer { val name = \"Kotlin\"; class Inner { fun printName() = println(name) } }
C. class Outer { val name = \"Kotlin\"; nested class Inner { fun printName() = println(name) } }
D. class Outer { val name = \"Kotlin\"; inner class Inner { fun printName() = println(this.name) } }
Solution
Step 1: Check class declaration syntax
Only the class declared with inner keyword can access outer class properties directly.
Step 2: Analyze each option
class Outer { val name = \"Kotlin\"; inner class Inner { fun printName() = println(name) } } uses inner and accesses name correctly. class Outer { val name = \"Kotlin\"; class Inner { fun printName() = println(name) } } lacks inner. class Outer { val name = \"Kotlin\"; nested class Inner { fun printName() = println(name) } } uses invalid nested keyword. class Outer { val name = \"Kotlin\"; inner class Inner { fun printName() = println(this.name) } } tries to access this.name which refers to inner class, not outer.
Final Answer:
B -> Option A
Quick Check:
Use inner and direct property name [OK]
Hint: Use 'inner' keyword and access outer property by name [OK]
Common Mistakes:
Omitting 'inner' keyword
Using 'nested' instead of 'inner'
Using 'this.name' inside inner class incorrectly
3. What is the output of the following Kotlin code?
class Outer(val x: Int) {
inner class Inner {
fun printX() = println(x)
}
}
fun main() {
val outer = Outer(10)
val inner = outer.Inner()
inner.printX()
}
medium
A. Runtime exception
B. 0
C. Compilation error
D. 10
Solution
Step 1: Understand inner class access
The inner class can access the outer class property x because it is declared with inner.
Step 2: Trace the code execution
An Outer object is created with x=10. The inner class instance calls printX() which prints x value 10.
Final Answer:
10 -> Option D
Quick Check:
Inner class prints outer property value [OK]
Hint: Inner class prints outer property directly if 'inner' keyword used [OK]
Common Mistakes:
Assuming inner class cannot access outer property
Expecting 0 instead of 10
Thinking code causes error
4. Identify the error in this Kotlin code snippet:
class Outer {
val message = "Hello"
class Inner {
fun greet() = println(message)
}
}
medium
A. Incorrect println syntax
B. Missing semicolon after 'message' declaration
C. Inner class cannot access 'message' without 'inner' keyword
D. Outer class must be declared as 'inner'
Solution
Step 1: Check nested class access rules
Nested classes without inner keyword cannot access outer class properties.
Step 2: Identify cause of error
The Inner class tries to print message but cannot see it, causing a compilation error.
Final Answer:
Inner class cannot access 'message' without 'inner' keyword -> Option C
Quick Check:
Use 'inner' to access outer members [OK]
Hint: Add 'inner' keyword to nested class to fix access error [OK]
Common Mistakes:
Thinking semicolon is required in Kotlin
Assuming println syntax is wrong
Confusing outer class declaration with inner
5. Given this Kotlin code, what will be the output?
class Outer(val number: Int) {
inner class Inner {
fun multiply(factor: Int) = number * factor
}
}
fun main() {
val outer = Outer(5)
val inner = outer.Inner()
println(inner.multiply(3))
}
hard
A. 53
B. 15
C. Compilation error due to access
D. Runtime exception
Solution
Step 1: Understand inner class access and method
The inner class can access the outer class property number because it is declared with inner. The method multiplies number by the given factor.
Step 2: Calculate the output
Outer number is 5, factor is 3, so 5 * 3 = 15 is printed.
Final Answer:
15 -> Option B
Quick Check:
Inner class method uses outer property correctly [OK]
Hint: Inner class can use outer properties in calculations [OK]
Common Mistakes:
Concatenating instead of multiplying
Expecting access error without 'inner'
Confusing output with string '53'
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.