0
0
Kotlinprogramming~10 mins

Visibility modifiers (public, private, internal, protected) 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 public function named greet.

Kotlin
[1] fun greet() {
    println("Hello!")
}
Drag options to blanks, or click blank then click option'
Aprivate
Bpublic
Cinternal
Dprotected
Attempts:
3 left
💡 Hint
Common Mistakes
Using private or protected which restrict access.
Using internal which limits visibility to the module.
2fill in blank
medium

Complete the code to declare a private variable named secret.

Kotlin
class Vault {
    [1] val secret = "1234"
}
Drag options to blanks, or click blank then click option'
Aprivate
Bprotected
Cpublic
Dinternal
Attempts:
3 left
💡 Hint
Common Mistakes
Using public which exposes the variable everywhere.
Using internal which allows access within the module.
3fill in blank
hard

Fix the error in the code by choosing the correct visibility modifier for the function.

Kotlin
open class Base {
    [1] fun show() {
        println("Base show")
    }
}
Drag options to blanks, or click blank then click option'
Aprivate
Binternal
Cprotected
Dpublic
Attempts:
3 left
💡 Hint
Common Mistakes
Using private which hides the function from subclasses.
Using internal which exposes it to the whole module.
4fill in blank
hard

Fill both blanks to declare an internal class with a private property.

Kotlin
[1] class Data {
    [2] val data = "hidden"
}
Drag options to blanks, or click blank then click option'
Ainternal
Bpublic
Cprivate
Dprotected
Attempts:
3 left
💡 Hint
Common Mistakes
Using public for the class which exposes it everywhere.
Using protected for the property, which allows access from subclasses.
5fill in blank
hard

Fill all three blanks to declare a protected open class with an internal function and a public property.

Kotlin
class Outer {
    [1] open class MyClass {
        [2] fun internalFunc() {
            println("Internal function")
        }
        [3] val name = "Kotlin"
    }
}
Drag options to blanks, or click blank then click option'
Aprotected
Binternal
Cpublic
Dprivate
Attempts:
3 left
💡 Hint
Common Mistakes
Using private for the class, which hides it from subclasses.
Using public for the function which exposes it everywhere.
Using internal for the property which limits visibility.