Bird
Raised Fist0
Android Kotlinmobile~20 mins

Clean Architecture layers in Android Kotlin - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Clean Architecture Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Identify the correct Clean Architecture layer for UI logic
In Clean Architecture for Android, which layer should handle user interface logic such as displaying data and capturing user input?
AFramework layer
BData layer
CDomain layer
DPresentation layer
Attempts:
2 left
💡 Hint
Think about where the app interacts directly with the user.
❓ ui_behavior
intermediate
2:00remaining
Effect of violating Dependency Rule in Clean Architecture
What happens if the Data layer directly depends on the Presentation layer in a Clean Architecture Android app?
AThe app will crash at runtime due to circular dependencies
BIt improves performance by reducing abstraction layers
CIt breaks the Dependency Rule, causing tight coupling and harder testing
DIt is recommended for faster UI updates
Attempts:
2 left
💡 Hint
Consider the direction of dependencies in Clean Architecture.
❓ lifecycle
advanced
2:00remaining
Correct lifecycle owner for ViewModel in Clean Architecture
In an Android app using Clean Architecture, which lifecycle owner should be used to scope a ViewModel in the Presentation layer?
AActivity or Fragment lifecycle
BApplication lifecycle
CDomain layer lifecycle
DData layer lifecycle
Attempts:
2 left
💡 Hint
ViewModels are tied to UI components' lifecycles.
❓ navigation
advanced
2:00remaining
Where to handle navigation logic in Clean Architecture
In Clean Architecture, where should navigation logic (e.g., moving between screens) be implemented in an Android app?
AData layer, to fetch navigation data
BPresentation layer, to handle UI navigation events
CDomain layer, because navigation is a business rule
DFramework layer, to access Android navigation components
Attempts:
2 left
💡 Hint
Think about which layer controls user interactions and screen changes.
🔧 Debug
expert
2:00remaining
Diagnose the cause of a runtime crash due to layer violation
An Android app using Clean Architecture crashes at runtime with a ClassCastException when the Data layer tries to cast a Presentation layer class. What is the most likely cause?
AThe Data layer depends on Presentation layer classes, violating the Dependency Rule
BThe Presentation layer is incorrectly referencing Domain entities
CThe Domain layer is missing interface implementations
DThe Framework layer is not initialized before use
Attempts:
2 left
💡 Hint
ClassCastException often happens when layers depend on each other incorrectly.

Practice

(1/5)
1. Which layer in Clean Architecture is responsible for business rules and logic?
domain, data, or presentation?
easy
A. Presentation layer
B. Domain layer
C. Data layer
D. UI layer

Solution

  1. Step 1: Understand the role of each layer

    The domain layer contains business rules and logic, independent of UI or data sources.
  2. Step 2: Match the layer to its responsibility

    Since business logic is in the domain layer, it is the correct answer.
  3. Final Answer:

    Domain layer -> Option B
  4. Quick Check:

    Business logic = Domain layer [OK]
Hint: Business rules live in domain layer, not UI or data [OK]
Common Mistakes:
  • Confusing data layer with domain layer
  • Thinking UI handles business logic
  • Mixing presentation with domain
2. Which Kotlin syntax correctly defines a repository interface in the domain layer?
?
easy
A. interface UserRepository { fun getUser(id: Int): User }
B. class UserRepository { fun getUser(id: Int): User }
C. fun UserRepository() = User()
D. val UserRepository = object { fun getUser() = User() }

Solution

  1. Step 1: Identify interface syntax in Kotlin

    Interfaces use the keyword interface followed by function signatures without bodies.
  2. Step 2: Check each option

    interface UserRepository { fun getUser(id: Int): User } correctly declares an interface with a function signature. Others define classes or objects incorrectly for an interface.
  3. Final Answer:

    interface UserRepository { fun getUser(id: Int): User } -> Option A
  4. Quick Check:

    Kotlin interface syntax = interface UserRepository { fun getUser(id: Int): User } [OK]
Hint: Interfaces use 'interface' keyword with function signatures [OK]
Common Mistakes:
  • Using class instead of interface for repository
  • Defining function bodies inside interface
  • Using val or fun incorrectly for interface
3. Given this code snippet in the data layer:
class UserRepositoryImpl(private val api: UserApi) : UserRepository {
  override fun getUser(id: Int): User {
    return api.fetchUser(id)
  }
}

What does getUser(5) return?
medium
A. Null because fetchUser returns nothing
B. An error because UserRepositoryImpl lacks a constructor
C. A User object fetched from the API with id 5
D. A compile-time syntax error

Solution

  1. Step 1: Understand class and method behavior

    UserRepositoryImpl implements UserRepository and calls api.fetchUser(id) to get a User.
  2. Step 2: Analyze getUser(5) call

    Calling getUser(5) returns the User object fetched by api.fetchUser(5), assuming api works correctly.
  3. Final Answer:

    A User object fetched from the API with id 5 -> Option C
  4. Quick Check:

    Method calls API and returns User [OK]
Hint: Implementation calls API method and returns result [OK]
Common Mistakes:
  • Assuming missing constructor causes error
  • Thinking fetchUser returns null by default
  • Confusing syntax errors with runtime behavior
4. Identify the error in this presentation layer code snippet:
class UserViewModel(private val repo: UserRepository) {
  fun loadUser(id: Int) {
    val user = repo.getUser(id)
    println(user.name)
  }
}
medium
A. UserViewModel should not call repository directly
B. User object might be null causing a crash
C. No error; code is correct
D. UserRepository interface is not implemented

Solution

  1. Step 1: Recall Clean Architecture dependencies

    The presentation layer (ViewModel) should depend on use cases in the domain layer, not directly on repository interfaces.
  2. Step 2: Identify the violation

    Directly injecting and calling UserRepository in ViewModel bypasses use cases, mixing concerns and violating layer separation.
  3. Final Answer:

    UserViewModel should not call repository directly -> Option A
  4. Quick Check:

    ViewModel -> UseCase -> Repository [OK]
Hint: ViewModels call UseCases, not Repositories directly [OK]
Common Mistakes:
  • Focusing on null safety (type system prevents null here)
  • Thinking no architecture error in the code
  • Assuming missing interface implementation
5. You want to add caching to your app using Clean Architecture. Where should the cache logic be placed?
Options:
A) In the domain layer
B) In the data layer
C) In the presentation layer
D) In the UI components
hard
A. Domain layer to keep business rules
B. UI components for faster display
C. Presentation layer to update UI directly
D. Data layer to manage data sources including cache

Solution

  1. Step 1: Understand caching responsibility

    Caching is a data management concern, related to how data is stored and retrieved.
  2. Step 2: Match caching to Clean Architecture layer

    The data layer handles data sources like network, database, and cache, so caching logic belongs here.
  3. Final Answer:

    Data layer to manage data sources including cache -> Option D
  4. Quick Check:

    Cache logic = Data layer [OK]
Hint: Cache belongs in data layer managing data sources [OK]
Common Mistakes:
  • Putting cache in domain layer (business logic)
  • Adding cache in UI or presentation layers
  • Mixing UI concerns with data management
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard