What does the public visibility modifier mean in Kotlin?
The public modifier means the declaration is visible everywhere. It is the default visibility if no modifier is specified.
Click to reveal answer
beginner
Explain the private visibility modifier in Kotlin.
The <strong>private</strong> modifier restricts the visibility to the containing class or file. It means the declaration cannot be accessed outside that scope.
Click to reveal answer
intermediate
What does internal visibility mean in Kotlin?
internal means the declaration is visible everywhere in the same module but not outside it. A module is a set of Kotlin files compiled together.
Click to reveal answer
intermediate
Describe the protected visibility modifier in Kotlin.
<strong>protected</strong> means the declaration is visible in the class and its subclasses. It is not visible outside these classes, even if in the same package.
Click to reveal answer
beginner
Which visibility modifier is the default in Kotlin if none is specified?
The default visibility modifier in Kotlin is public, meaning the declaration is visible everywhere.
Click to reveal answer
Which Kotlin visibility modifier allows access only within the same file?
Aprotected
Bpublic
Cinternal
Dprivate
✗ Incorrect
private restricts access to the containing file or class.
What does the internal modifier restrict visibility to?
AOnly the containing class
BThe same module
CAnywhere in the project
DOnly subclasses
✗ Incorrect
internal means visible within the same module.
Which modifier allows visibility in subclasses but not outside the class hierarchy?
Aprotected
Bpublic
Cprivate
Dinternal
✗ Incorrect
protected allows access in the class and its subclasses.
If no visibility modifier is specified in Kotlin, what is the default?
Apublic
Binternal
Cprotected
Dprivate
✗ Incorrect
The default visibility is public.
Can a private member be accessed from another file in the same module?
AYes
BOnly if it is a subclass
CNo
DOnly if marked internal
✗ Incorrect
private members are not accessible outside their file or class.
Describe the four visibility modifiers in Kotlin and give a simple example of when to use each.
Think about who should see your code: everyone, only inside, inside module, or subclasses.
You got /5 concepts.
Explain why you might choose internal over public for a Kotlin class.
Consider controlling access in bigger projects with multiple modules.
You got /4 concepts.
Practice
(1/5)
1. Which visibility modifier in Kotlin allows a class member to be accessed from anywhere in the project?
easy
A. internal
B. private
C. public
D. protected
Solution
Step 1: Understand the meaning of public
The public modifier means the member is visible everywhere in the project.
Step 2: Compare with other modifiers
private restricts to class/file, internal restricts to module, protected restricts to class and subclasses only.
Final Answer:
public -> Option C
Quick Check:
Visible everywhere = public [OK]
Hint: Public means visible everywhere in the project [OK]
Common Mistakes:
Confusing private with public visibility
Thinking internal is visible everywhere
Assuming protected is public
2. Which of the following is the correct syntax to declare a private property named count in a Kotlin class?
easy
A. private val count: Int = 0
B. val private count: Int = 0
C. val count private: Int = 0
D. private count val: Int = 0
Solution
Step 1: Recall Kotlin property declaration syntax
Visibility modifier comes before val or var, so private val count: Int = 0 is correct.
Step 2: Check other options for syntax errors
Options B, C, and D have incorrect order of keywords, which is invalid in Kotlin.
Final Answer:
private val count: Int = 0 -> Option A
Quick Check:
Modifier before val/var = correct syntax [OK]
Hint: Put visibility modifier before val or var [OK]
Common Mistakes:
Placing private after val
Mixing order of keywords
Omitting colon before type
3. Consider the following Kotlin code:
open class Parent {
protected val secret = "hidden"
}
class Child : Parent() {
fun reveal() = secret
}
fun main() {
val child = Child()
println(child.reveal())
// println(child.secret) // Line X
}
What will happen if the commented Line X is uncommented?
medium
A. Compilation error: Cannot access 'secret' outside the class or subclass
B. Prints 'hidden' without error
C. Runtime error: Illegal access
D. Compilation error: 'secret' is private
Solution
Step 1: Understand protected visibility
protected members are accessible inside the class and its subclasses, but not outside.
Step 2: Analyze access in main function
Accessing child.secret from outside the class hierarchy causes a compilation error.
Final Answer:
Compilation error: Cannot access 'secret' outside the class or subclass -> Option A
Quick Check:
Protected blocks external access [OK]
Hint: Protected is accessible only inside class and subclasses [OK]
Common Mistakes:
Assuming protected is public
Expecting runtime error instead of compile error
Confusing private and protected
4. Given this Kotlin code snippet:
class Example {
private val secret = "top secret"
}
fun main() {
val ex = Example()
println(ex.secret)
}
What is the error and how to fix it?
medium
A. No error; code runs and prints 'top secret'
B. Error: Missing constructor; fix by adding constructor
C. Error: Syntax error; fix by adding semicolon
D. Error: Cannot access private property outside class; fix by changing to public
Solution
Step 1: Identify visibility of secret
The property secret is declared private, so it cannot be accessed outside the class.
Step 2: Understand the error cause and fix
Trying to print ex.secret from outside causes a compilation error. Changing private to public or providing a public method to access it fixes the error.
Final Answer:
Error: Cannot access private property outside class; fix by changing to public -> Option D
Quick Check:
Private blocks external access [OK]
Hint: Private hides members outside class; use public or accessor [OK]
Common Mistakes:
Thinking private allows outside access
Confusing syntax errors with visibility errors
Ignoring need for accessor methods
5. You have two Kotlin files in the same module: FileA.kt and FileB.kt. In FileA.kt, there is an internal function fun greet() = "Hello". Which of the following statements is true about calling greet() from FileB.kt?
hard
A. greet() cannot be called because internal means private
B. greet() can be called from FileB.kt because internal allows access within the same module
C. greet() cannot be called because internal restricts access to the same file only
D. greet() can be called only if FileB.kt inherits from FileA.kt
Solution
Step 1: Understand internal visibility
internal means visible anywhere inside the same module, regardless of file.
Step 2: Apply to given files
Since FileA.kt and FileB.kt are in the same module, greet() is accessible from FileB.kt.
Final Answer:
greet() can be called from FileB.kt because internal allows access within the same module -> Option B
Quick Check:
Internal = visible in module [OK]
Hint: Internal means visible anywhere in the same module [OK]
Common Mistakes:
Confusing internal with private or file-private
Thinking internal restricts to one file
Assuming inheritance needed for internal access
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.