Architecture & ViewModel Questions with Answers

17 practice questions on Architecture & ViewModel (Android (Kotlin & Compose)) with answers and step-by-step solutions. Try 10 samples below, then practise all 17 free in the AptiNest app.

Sample questions

Question 1Easy

In Hilt, which annotation goes on the Application class?

  1. @HiltAndroidApp
  2. @Module
  3. @HiltViewModel
  4. @AndroidEntryPoint

Answer: A. @HiltAndroidApp

Explanation

It triggers Hilt's code generation and the application-level component.

Step-by-step solution

  1. @HiltAndroidApp goes on the Application subclass.
  2. It triggers Hilt's code generation and the app-level container.
  3. Other Android classes then use @AndroidEntryPoint.

Why the other options are wrong

  • B. @Module marks classes that provide bindings.
  • C. @HiltViewModel marks ViewModels.
  • D. @AndroidEntryPoint is for Activities, Fragments and similar.

Question 2Easy

An Android ViewModel is designed to:

  1. Replace the Activity
  2. Draw views on the screen
  3. Store data permanently on disk
  4. Hold UI state across rotation

Answer: D. Hold UI state across rotation

Explanation

A ViewModel outlives Activity recreation (e.g. rotation) and is cleared when its owner is finished.

Step-by-step solution

  1. Rotation destroys and recreates the Activity.
  2. A ViewModel lives in a ViewModelStore that survives this.
  3. So UI state is kept across configuration changes.

Why the other options are wrong

  • A. The Activity is still needed.
  • B. The UI layer draws views.
  • C. ViewModels are in memory, not on disk.

Question 3Easy

In MVVM on Android, the View layer is typically:

  1. The UI code
  2. The Repository
  3. The REST API
  4. The Room database

Answer: A. The UI code

Explanation

The View renders state from the ViewModel and forwards user events.

Step-by-step solution

  1. MVVM = Model, View, ViewModel.
  2. The View is the UI: Activities, Fragments or composables.
  3. It renders state and forwards user events.

Why the other options are wrong

  • B. The Repository is part of the data layer.
  • C. REST APIs are data sources.
  • D. Room is a data source.

Question 4Easy

MVVM stands for:

  1. Model-Version-View-Manager
  2. Main-View-Visual-Model
  3. Model-View-ViewModel
  4. Modular View Verification Method

Answer: C. Model-View-ViewModel

Explanation

The ViewModel exposes state that the View observes.

Step-by-step solution

  1. MVVM splits an app into three layers.
  2. Model: data and business logic.
  3. View: the UI. ViewModel: holds UI state and exposes it to the View.

Why the other options are wrong

  • A. 'Version' and 'Manager' are not parts of MVVM.
  • B. These words are made up.
  • D. Not an architecture pattern.

Tip: MVVM: View observes, ViewModel exposes, Model provides.

Question 5Medium

How does a Hilt-injected ViewModel receive its dependencies?

  1. Using reflection at runtime only
  2. Through setter methods called by the Activity
  3. @HiltViewModel plus an @Inject constructor
  4. From a static singleton

Answer: C. @HiltViewModel plus an @Inject constructor

Explanation

In Compose it is obtained with hiltViewModel(); in Activities with by viewModels().

Step-by-step solution

  1. Annotate the ViewModel class with @HiltViewModel.
  2. Mark its constructor with @Inject and list the dependencies.
  3. Get it with by viewModels() in an @AndroidEntryPoint screen or hiltViewModel() in Compose.

Why the other options are wrong

  • A. Hilt generates code at compile time; it does not rely on runtime reflection.
  • B. Constructor injection is the Hilt way.
  • D. Statics bypass DI.

Question 6Medium

In the recommended app architecture, the Repository's role is to:

  1. Render UI components
  2. Define navigation routes
  3. Mediate data sources
  4. Handle permissions

Answer: C. Mediate data sources

Explanation

Repositories combine network and local database sources and expose data to ViewModels.

Step-by-step solution

  1. The data layer may have several sources: network, database, cache.
  2. The Repository hides them behind one interface.
  3. It decides where to read from and keeps sources in sync.

Why the other options are wrong

  • A. Rendering is the UI's job.
  • B. Navigation belongs to the UI layer.
  • D. Permissions belong to the UI layer.

Question 7Medium

Which method of a ViewModel is called just before it is destroyed?

  1. onCleared()
  2. finish()
  3. onStop()
  4. onDestroy()

Answer: A. onCleared()

Explanation

Use it to release resources; viewModelScope is cancelled at this point.

Step-by-step solution

  1. A ViewModel lives until its owner is finished for good.
  2. Just before it is destroyed, onCleared() is called.
  3. Use it to cancel work and release resources; viewModelScope is cancelled automatically.

Why the other options are wrong

  • B. finish() is an Activity method.
  • C. onStop() is an Activity lifecycle callback.
  • D. onDestroy() belongs to Activities and Fragments, not ViewModels.

Tip: ViewModel end = onCleared().

Question 8Medium

AndroidViewModel differs from ViewModel because it:

  1. Can hold an Activity safely
  2. Survives process death
  3. Runs on a background thread
  4. Gets the Application

Answer: D. Gets the Application

Explanation

The Application context is safe to hold because it lives as long as the app.

Step-by-step solution

  1. AndroidViewModel takes an Application in its constructor.
  2. That gives it a context that lives as long as the app.
  3. So it is safe to keep, unlike an Activity context.

Why the other options are wrong

  • A. Holding an Activity leaks in any ViewModel.
  • B. It does not survive process death.
  • C. Threading is unrelated.

Question 9Hard

A screen shows Loading, Success(data) or Error(message). Which Kotlin construct models this best?

  1. A String status field
  2. An Int with magic numbers
  3. A sealed interface UiState
  4. Three separate Boolean flags

Answer: C. A sealed interface UiState

Explanation

Sealed types make impossible combinations unrepresentable and give exhaustive when checks.

Step-by-step solution

  1. The three states are mutually exclusive.
  2. A sealed interface with Loading, Success(data) and Error(message) models exactly that.
  3. when then handles each case, checked by the compiler.

Why the other options are wrong

  • A. Strings are easy to mistype and cannot carry typed data.
  • B. Magic numbers are unclear and unchecked.
  • D. Booleans allow impossible combinations, like loading and error at once.

Question 10Hard

In the recommended architecture, the optional domain layer usually contains:

  1. Use cases
  2. Room entities only
  3. XML layout files
  4. Compose themes

Answer: A. Use cases

Explanation

Use cases (interactors) sit between ViewModels and repositories when logic is shared or complex.

Step-by-step solution

  1. The domain layer sits between UI and data.
  2. It holds use cases (interactors).
  3. Each use case wraps one piece of business logic, often combining repositories.

Why the other options are wrong

  • B. Room entities belong in the data layer.
  • C. Layouts are UI.
  • D. Themes are UI.

Practise all 17 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all Android (Kotlin & Compose) topics · All topics