0
0
Kotlinprogramming~20 mins

Visibility modifiers (public, private, internal, protected) in Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Visibility Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Kotlin code with visibility modifiers?

Consider the following Kotlin code snippet. What will be printed when Main().test() is called?

Kotlin
open class Main {
    private val secret = "hidden"
    internal val internalInfo = "inside module"
    protected val protectedInfo = "protected data"
    val publicInfo = "visible everywhere"

    fun test() {
        println(secret)
        println(internalInfo)
        println(publicInfo)
    }
}

fun main() {
    Main().test()
}
A
hidden
inside module
protected data
B
hidden
inside module
visible everywhere
C
visible everywhere
inside module
hidden
DError: Cannot access protectedInfo
Attempts:
2 left
💡 Hint

Remember that private members are accessible inside the same class, internal inside the same module, and protected only in subclasses.

Predict Output
intermediate
2:00remaining
What error occurs when accessing a private property from outside the class?

What happens if you try to access a private property from outside its class in Kotlin?

Kotlin
class Box {
    private val secret = "top secret"
}

fun main() {
    val box = Box()
    println(box.secret)
}
ACompilation error: Cannot access 'secret': it is private in 'Box'
BPrints: top secret
CRuntime error: IllegalAccessException
DPrints: null
Attempts:
2 left
💡 Hint

Think about what private means in Kotlin.

🔧 Debug
advanced
2:00remaining
Why does this Kotlin code fail to compile due to visibility?

Examine the code below. Why does it fail to compile?

Kotlin
open class Parent {
    protected val data = "parent data"
}

class Child : Parent() {
    fun printData() {
        println(data)
    }
}

fun main() {
    val child = Child()
    println(child.data)
}
ARuntime error: IllegalAccessException
BPrints: parent data parent data
CCompilation error: 'data' is private in 'Parent'
DCompilation error: Cannot access 'data': it is protected in 'Parent'
Attempts:
2 left
💡 Hint

Remember where protected members can be accessed from.

Predict Output
advanced
2:00remaining
What is the output when accessing internal members from another module?

Assume ModuleA defines this class:

class Example {
    internal val info = "module info"
    val publicInfo = "public info"
}

And ModuleB tries to run:

fun main() {
    val ex = Example()
    println(ex.info)
    println(ex.publicInfo)
}

What happens?

ACompilation error: Cannot access 'info': it is internal in 'Example'
B
Prints:
module info
public info
CRuntime error: IllegalAccessException
D
Prints:
public info
module info
Attempts:
2 left
💡 Hint

Think about what internal means across modules.

🧠 Conceptual
expert
2:00remaining
Which Kotlin visibility modifier allows access only within the same class and its subclasses?

Choose the visibility modifier that fits this description:

  • Accessible inside the class and subclasses
  • Not accessible from unrelated classes in the same module
Aprivate
Binternal
Cprotected
Dpublic
Attempts:
2 left
💡 Hint

Think about which modifier restricts access to subclasses only.