0
0
Kotlinprogramming~10 mins

Nested class independence from outer in Kotlin - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare a nested class inside Outer.

Kotlin
class Outer {
    class [1] {
        fun greet() = "Hello from Nested"
    }
}
Drag options to blanks, or click blank then click option'
ANested
BInner
COuterNested
DCompanion
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Inner' instead of 'Nested' which changes the class type.
Using 'Companion' which is for companion objects, not nested classes.
2fill in blank
medium

Complete the code to create an instance of the nested class.

Kotlin
val nested = Outer.[1]()
Drag options to blanks, or click blank then click option'
AInner
BCompanion
CNested
DOuterNested
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to create an instance of 'Inner' which requires an outer instance.
Using 'Companion' which is not a class.
3fill in blank
hard

Fix the error in accessing the outer class property from the nested class.

Kotlin
class Outer(val name: String) {
    class Nested {
        fun printName() = [1]
    }
}
Drag options to blanks, or click blank then click option'
Aname
B"Cannot access outer property"
Cthis@Outer.name
DOuter().name
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to use 'this@Outer.name' inside a nested class which is invalid.
Creating a new Outer instance to access name which is not the same as the outer instance.
4fill in blank
hard

Fill both blanks to create an inner class that can access the outer class property.

Kotlin
class Outer(val name: String) {
    inner class [1] {
        fun printName() = [2]
    }
}
Drag options to blanks, or click blank then click option'
AInner
BNested
Cthis@Outer.name
Dname
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Nested' instead of 'Inner' which does not allow access to outer properties.
Using just 'name' without 'this@Outer' which causes ambiguity.
5fill in blank
hard

Fill all three blanks to create a nested class with a function that returns a greeting including a passed outer class name.

Kotlin
class Outer {
    class [1] {
        fun greet(name: String) = "Hello, [2]!"
    }
}

val nested = Outer.[3]()
println(nested.greet("Alice"))
Drag options to blanks, or click blank then click option'
ANested
B$name
DInner
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Inner' instead of 'Nested' for the class name.
Using '\$name' instead of '$name' which prevents interpolation.
Trying to create an instance of 'Inner' without an outer instance.