Sample questions
Kt Android Memory Context LeaksDifficulty 1
What is the key practical difference between an Activity context and the Application context in Android?
- aThere is no difference; both are interchangeable in every situation. Both are plain instances of the same
Context base class under the hood. - bThe Application context can inflate themed layouts but the Activity context cannot.
- cThe Activity context is only available inside Fragments, never inside Activities.
- dThe Activity context is tied to that Activity's lifecycle; the Application context lives as long as the process.✓
Explanation:An Activity context is destroyed with its Activity; holding it longer than the Activity's lifetime is a classic leak source.
Kt Android Memory Context LeaksDifficulty 1
A singleton stores a reference to an Activity passed into its init() so it can show dialogs later. What is the risk?
- aThere is no risk; singletons are always garbage collected promptly. Any object without a manually managed lifetime is reclaimed as soon as its scope ends.
- bThe singleton outlives the Activity, keeping it (and everything it references) alive after it should be destroyed.✓
- cThe Activity will fail to compile because singletons cannot accept parameters.
- dThe dialog will always crash immediately regardless of the reference.
Explanation:A singleton's lifetime is tied to the process, not any single Activity; storing an Activity reference prevents it from being garbage collected.
Kt Android Memory Context LeaksDifficulty 1
In Kotlin, why does a non-static (inner) class implicitly hold a reference to its enclosing instance?
- aBecause an inner class instance is conceptually tied to a specific outer instance and needs its members.✓
- bBecause Kotlin requires every class to hold a reference to every other class in the file.
- cBecause inner classes are always singletons shared across the whole app. This is enforced by the Kotlin compiler for every nested class declaration.
- dBecause the compiler adds this reference only in debug builds for logging.
Explanation:A regular (non-companion) nested class that needs outer-instance access holds an implicit reference to it, which is why inner objects can leak the enclosing Activity.
Kt Android Memory Context LeaksDifficulty 2
An Activity registers an anonymous View.OnClickListener on a button. What does that anonymous listener implicitly hold a reference to?
- aNothing; anonymous objects never hold outer references.
- bOnly the button view, never the Activity itself.
- cThe Android framework's global ClassLoader. It is a single shared instance responsible for loading every class in the app.
- dThe enclosing Activity instance, because the anonymous class captures its enclosing scope.✓
Explanation:An anonymous inner class created inside an Activity method captures the enclosing Activity instance implicitly, like a named inner class would.
Kt Android Memory Context LeaksDifficulty 1
What is the general purpose of WeakReference in the context of avoiding memory leaks?
- aTo guarantee an object is never garbage collected under any circumstance.
- bTo automatically convert any object into a singleton. This conversion happens transparently the first time the reference is read.
- cTo hold a reference to an object without preventing that object from being garbage collected.✓
- dTo force immediate garbage collection of the referenced object.
Explanation:A WeakReference lets code access an object if it still exists, without counting as a strong reference — useful for holding a shorter-lived object (like an Activity) from something longer-lived.
Kt Android Memory Context LeaksDifficulty 2
A Handler posts a Runnable with postDelayed(runnable, 60_000) from an Activity, which is destroyed 2 seconds later. If the Runnable is a non-static inner class, what happens?
- aThe Runnable is automatically cancelled the moment the Activity is destroyed.
- bThe app crashes immediately when the Activity is destroyed. The runtime treats a destroyed Activity as an immediate fatal error source.
- cThe Handler silently discards the pending message right away.
- dThe Handler's queued message keeps the Runnable — and the destroyed Activity — alive until it fires, about a minute later.✓
Explanation:A pending delayed message on a Handler's queue holds a reference to its Runnable; a non-static inner Runnable keeps its Activity alive until the message is processed or removed.