Complete the code to declare a public function named greet.
[1] fun greet() { println("Hello!") }
In Kotlin, functions are public by default, but explicitly using public makes it clear the function is accessible everywhere.
Complete the code to declare a private variable named secret.
class Vault { [1] val secret = "1234" }
The private modifier restricts access to the variable within the class only.
Fix the error in the code by choosing the correct visibility modifier for the function.
open class Base { [1] fun show() { println("Base show") } }
The protected modifier allows the function to be visible in subclasses but not outside the class hierarchy.
Fill both blanks to declare an internal class with a private property.
[1] class Data { [2] val data = "hidden" }
The class is internal, so it is visible within the module. The property is private, so it is only accessible inside the class.
Fill all three blanks to declare a protected open class with an internal function and a public property.
class Outer { [1] open class MyClass { [2] fun internalFunc() { println("Internal function") } [3] val name = "Kotlin" } }
The class is protected and open, so it can be subclassed and is visible to subclasses. The function is internal, visible within the module. The property is public, visible everywhere.