Interview prep · Stage 1: Kotlin and DSA basics

Kotlin visibility modifiers interview questions

Mid · 5 questions · 3 min read

One opener and four harder follow-ups. Each step builds on the last, the way a real interviewer digs.

Try each question out loud before you open the answer.

Opening question

What are the visibility modifiers in Kotlin, and which one is the default?

Show answer
fun isSameEmail(a: String, b: String) = ...   // public by default
private fun normalize(email: String) = ...      // this file only
internal fun buildUserAgent(version: String) = ... // this module only

Junior answer

public, private, protected and internal. The default is public.

Senior answer

public is the default and means everyone. private means the same class, or the same file for a top-level declaration. protected means the class and its subclasses. internal means the same module. Unlike Java, there is no package-private, and the default is public, not package-only.

What they're really checking: Saying the default is package-private, like Java.

Follow-up 1

What does private mean at the top of a file, and can you write protected there?

Show answer
// EmailUtils.kt
private fun normalize(email: String) = email.trim().lowercase()

protected fun nope() = 1 // compile error

Answer

A top-level private declaration can be used anywhere in that file and nowhere else, even in the same package. protected isn't allowed at the top level, because there is no class for a subclass to extend. The compiler says modifier 'protected' is not applicable to 'top level function'.

What they're really checking: Thinking top-level private means "private to the package".

Follow-up 2

What is a module for internal, and can unit tests use internal code?

Show answer

Junior answer

A module is a Gradle module. Tests can use it.

Senior answer

A module is a set of Kotlin files compiled together: a Gradle source set, a Maven project or an IntelliJ module. On Android, think of each Gradle module like :core:network. Tests in src/test of the same module can use its internal code, because Gradle passes the main code to the test compile as a friend. Another module, like :feature:checkout, gets a compile error.

What they're really checking: Thinking internal means "same package", or that it does anything in a single-module app.

Follow-up 3

What does internal look like in bytecode?

Show answer
// Decompiled, module "network"
public static final String buildUserAgent(String version)   // top level: same name
public final String sendRequest$network(String path)        // class member: renamed

Answer

The JVM only knows public, protected, package-private and private, so internal compiles to public. Internal members of a class or object get the module name added: sendRequest becomes sendRequest$network, and dashes in the module name become underscores. A top-level internal function keeps its plain name. Public members of an internal class keep their names too.

What they're really checking: Saying every internal function gets a mangled name. Top-level functions don't.

Follow-up 4

Can Java code call an internal function? How would you stop it?

Show answer
@JvmSynthetic
internal fun buildUserAgent(version: String) = "MyApp/$version"

Answer

Yes. Only the Kotlin compiler checks internal. javac doesn't read Kotlin metadata, so Java in another module can call a top-level internal function by its plain name, and a member by its $module name. If Java must stay out, add @JvmSynthetic: javac then can't see the function, and Kotlin in the same module still calls it. None of this is security: reflection can reach everything.

What they're really checking: Treating internal or private as a way to hide secrets.

Say it in 30 seconds

Show the answer

Kotlin has four visibility modifiers. public is the default and means everyone. private means the same class, or the same file for top-level code. protected means the class and its subclasses, and it can't be used at the top level. internal means the same module, like one Gradle module, and tests in src/test of that module can use it too. Kotlin has no package-private. The JVM has no module rule, so internal compiles to public. Internal members of classes get the module name added, like sendRequest$network, but a top-level internal function keeps its name. Java doesn't check internal, so Java code can call it. @JvmSynthetic hides it from Java.

How did you do?