A Composable function is a special Kotlin function annotated with @Composable that describes part of the app's UI. It can be combined with other composables to build the interface.
Click to reveal answer
beginner
How do you mark a function as composable in Kotlin?
By adding the @Composable annotation before the function definition.
Click to reveal answer
intermediate
Why should composable functions be side-effect free and idempotent?
Because composable functions can be called many times during UI updates, they should not change data or state directly and should always produce the same UI output for the same inputs.
Click to reveal answer
beginner
What is the role of the setContent function in an Android app using Compose?
It sets the UI content of an activity by calling composable functions inside it, replacing the traditional XML layout inflation.
Click to reveal answer
beginner
Can composable functions call other composable functions?
Yes, composable functions can call other composable functions to build complex UI by combining smaller UI pieces.
Click to reveal answer
Which annotation marks a function as composable in Jetpack Compose?
A@UIFunction
B@Composable
C@AndroidEntryPoint
D@Layout
✗ Incorrect
The @Composable annotation tells the compiler that the function describes UI and can be used in Compose.
What does a composable function return?
AA View object
BA Layout XML
CNothing (Unit)
DA Fragment
✗ Incorrect
Composable functions return Unit and describe UI declaratively instead of returning View objects.
Which function is used to set the UI content in a Compose-based Android activity?
AsetContent()
BsetContentView()
Cinflate()
DsetLayout()
✗ Incorrect
setContent() replaces setContentView() in Compose to set composable UI content.
Why should composable functions avoid side effects?
ATo support older Android versions
BTo improve app performance
CTo reduce APK size
DBecause they can be called multiple times during recomposition
✗ Incorrect
Composable functions may be called many times during UI updates, so side effects can cause bugs or inconsistent UI.
Can composable functions have parameters?
AYes, to customize UI based on input
BOnly default parameters are allowed
COnly primitive types are allowed
DNo, they cannot take parameters
✗ Incorrect
Composable functions can take parameters to customize the UI they display.
Explain what a composable function is and how it is used in Jetpack Compose.
Think about how UI pieces are built in Compose.
You got /5 concepts.
Describe why composable functions should be side-effect free and what could happen if they are not.
Consider how UI updates happen in Compose.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of a @Composable function in Android Kotlin?
easy
A. To manage database operations
B. To handle network requests
C. To define app navigation routes
D. To build and display UI components
Solution
Step 1: Understand the role of @Composable
The @Composable annotation marks functions that create UI elements in Jetpack Compose.
Step 2: Identify the correct purpose
These functions are used to build and display UI components, not for networking or database tasks.
Final Answer:
To build and display UI components -> Option D
Quick Check:
@Composable = UI builder [OK]
Hint: Remember: @Composable means UI code [OK]
Common Mistakes:
Thinking @Composable handles logic like networking
Confusing @Composable with lifecycle methods
Assuming it manages navigation or data storage
2. Which of the following is the correct way to declare a composable function in Kotlin?
easy
A. @Composable fun MyComponent() { /* UI code */ }
B. fun MyComponent() { /* UI code */ }
C. @Composable MyComponent() { /* UI code */ }
D. fun @Composable MyComponent() { /* UI code */ }
Solution
Step 1: Recall the syntax for composable functions
Composable functions must be annotated with @Composable before the function keyword.
Step 2: Check each option
@Composable fun MyComponent() { /* UI code */ } correctly places @Composable before fun. The other options either lack the annotation entirely or place it incorrectly (after fun or without fun).
Final Answer:
@Composable fun MyComponent() { /* UI code */ } -> Option A
Quick Check:
Annotation before fun = correct syntax [OK]
Hint: Always put @Composable before fun [OK]
Common Mistakes:
Omitting the @Composable annotation
Placing @Composable after the function name
Using incorrect syntax like missing fun
3. What will be displayed when this composable function is called?
@Composable
fun Greeting() {
Text(text = "Hello, Compose!")
}
medium
A. A text label showing 'Hello, Compose!'
B. A compile-time error due to missing imports
C. An empty screen with no text
D. A button labeled 'Hello, Compose!'
Solution
Step 1: Understand the Text composable
The Text composable displays a text label on the screen with the given string.
Step 2: Analyze the function output
The function shows the text "Hello, Compose!" as a label, not a button or empty screen.
Final Answer:
A text label showing 'Hello, Compose!' -> Option A
Quick Check:
Text shows text label [OK]
Hint: Text composable always displays text on screen [OK]
Common Mistakes:
Confusing Text with Button composable
Assuming missing imports cause runtime error
Expecting no output from a valid composable
4. Identify the error in this composable function:
@Composable
fun Welcome() {
Text(text = Hello World)
}
medium
A. Missing @Composable annotation
B. Missing quotes around the string in Text
C. Function name should be lowercase
D. Text composable cannot be used inside functions
Solution
Step 1: Check the Text parameter
The string passed to Text must be in quotes. Here, "Hello World" is missing quotes.
Step 2: Verify other parts
The function has the @Composable annotation, function name casing is allowed, and Text is valid inside composables.
Final Answer:
Missing quotes around the string in Text -> Option B
Quick Check:
Strings need quotes in Kotlin [OK]
Hint: Strings must be in quotes inside Text composable [OK]
Common Mistakes:
Forgetting quotes around string literals
Thinking function names must be lowercase
Assuming @Composable is missing when it is present
5. How can you create a reusable composable that shows a greeting with a dynamic name? Choose the correct function declaration:
?
hard
A. @Composable fun Greeting() {
Text(text = "Hello, name!")
}
B. fun Greeting(name: String) {
Text(text = "Hello, ${name}!")
}
C. @Composable fun Greeting(name: String) {
Text(text = "Hello, ${name}!")
}
D. @Composable fun Greeting(name) {
Text(text = "Hello, ${name}!")
}
Solution
Step 1: Check for @Composable annotation and parameter
The function must be annotated with @Composable and accept a String parameter for the name.
Step 2: Verify string interpolation and parameter syntax
@Composable fun Greeting(name: String) {
Text(text = "Hello, ${name}!")
} correctly uses @Composable, declares name: String, and interpolates it inside the text. The version without @Composable misses the annotation, the one without a parameter hardcodes the name, and the one missing the parameter type is invalid.
Final Answer:
@Composable fun Greeting(name: String) {
Text(text = "Hello, ${name}!")
} -> Option C