Bird
Raised Fist0
Android Kotlinmobile~10 mins

Composable functions in Android Kotlin - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a simple composable function that displays "Hello World" text.

Android Kotlin
import androidx.compose.material.Text
import androidx.compose.runtime.Composable

@Composable
fun Greeting() {
    Text(text = [1])
}
Drag options to blanks, or click blank then click option'
AText("Hello World")
BHello World
Ctext = "Hello World"
D"Hello World"
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting quotes around the string
Passing the whole Text call inside Text
2fill in blank
medium

Complete the code to call the Greeting composable inside another composable function.

Android Kotlin
import androidx.compose.runtime.Composable

@Composable
fun MainScreen() {
    [1]()
}
Drag options to blanks, or click blank then click option'
AText
BGreeting
CsetContent
DButton
Attempts:
3 left
💡 Hint
Common Mistakes
Calling setContent inside a composable
Using UI elements like Button or Text instead of the composable name
3fill in blank
hard

Fix the error in the composable function by completing the code to add a parameter for the name and display it.

Android Kotlin
import androidx.compose.material.Text
import androidx.compose.runtime.Composable

@Composable
fun Greeting(name: String) {
    Text(text = "Hello, [1]!")
}
Drag options to blanks, or click blank then click option'
A$name
Buser
CName
D"name"
Attempts:
3 left
💡 Hint
Common Mistakes
Putting the variable name inside quotes, making it a literal string
Using a different variable name not declared
4fill in blank
hard

Fill the blank to create a composable that shows a button with a click action that updates a counter.

Android Kotlin
import androidx.compose.material.Button
import androidx.compose.material.Text
import androidx.compose.runtime.*

@Composable
fun Counter() {
    var count by [1]
    Button(onClick = { count++ }) {
        Text(text = "Clicked: $count")
    }
}
Drag options to blanks, or click blank then click option'
Aremember { mutableStateOf }
BmutableStateOf
Cremember { mutableStateOf(0) }
Dremember { mutableStateOf(0) }()
Attempts:
3 left
💡 Hint
Common Mistakes
Not using remember to keep state
Calling mutableStateOf without initial value
Incorrect lambda syntax for onClick
5fill in blank
hard

Fill all three blanks to create a composable that displays a list of names using a Column and Text composables.

Android Kotlin
import androidx.compose.foundation.layout.Column
import androidx.compose.material.Text
import androidx.compose.runtime.Composable

@Composable
fun NameList(names: List<String>) {
    Column {
        for (name in [1]) {
            Text(text = [2])
        }
    }
}
Drag options to blanks, or click blank then click option'
Anames
Bname
C"name"
DnamesList
Attempts:
3 left
💡 Hint
Common Mistakes
Using a string literal "name" instead of the variable
Using a wrong variable name for the list

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

  1. Step 1: Understand the role of @Composable

    The @Composable annotation marks functions that create UI elements in Jetpack Compose.
  2. Step 2: Identify the correct purpose

    These functions are used to build and display UI components, not for networking or database tasks.
  3. Final Answer:

    To build and display UI components -> Option D
  4. 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

  1. Step 1: Recall the syntax for composable functions

    Composable functions must be annotated with @Composable before the function keyword.
  2. 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).
  3. Final Answer:

    @Composable fun MyComponent() { /* UI code */ } -> Option A
  4. 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

  1. Step 1: Understand the Text composable

    The Text composable displays a text label on the screen with the given string.
  2. Step 2: Analyze the function output

    The function shows the text "Hello, Compose!" as a label, not a button or empty screen.
  3. Final Answer:

    A text label showing 'Hello, Compose!' -> Option A
  4. 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

  1. Step 1: Check the Text parameter

    The string passed to Text must be in quotes. Here, "Hello World" is missing quotes.
  2. Step 2: Verify other parts

    The function has the @Composable annotation, function name casing is allowed, and Text is valid inside composables.
  3. Final Answer:

    Missing quotes around the string in Text -> Option B
  4. 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

  1. Step 1: Check for @Composable annotation and parameter

    The function must be annotated with @Composable and accept a String parameter for the name.
  2. 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.
  3. Final Answer:

    @Composable fun Greeting(name: String) { Text(text = "Hello, ${name}!") } -> Option C
  4. Quick Check:

    Composable + typed parameter + string interpolation = correct [OK]
Hint: Composable needs annotation and typed parameters [OK]
Common Mistakes:
  • Omitting @Composable annotation
  • Not declaring parameter type
  • Hardcoding text instead of using parameter
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