Bird
Raised Fist0
Android Kotlinmobile~20 mins

Text composable in Android Kotlin - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Text Composable Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate
2:00remaining
What is the output of this Text composable code?
Consider this Kotlin Compose code snippet:
Text(text = "Hello, Compose!", color = Color.Red, fontSize = 20.sp)

What will the user see on the screen?
Android Kotlin
Text(text = "Hello, Compose!", color = Color.Red, fontSize = 20.sp)
ANo text will be displayed because color must be set with a hex string.
BThe text "Hello, Compose!" displayed in default black color with font size 20sp.
CThe text "Hello, Compose!" displayed in red color with default font size.
DThe text "Hello, Compose!" displayed in red color with font size 20sp.
Attempts:
2 left
💡 Hint
Remember that the color parameter controls text color and fontSize controls size.
📝 Syntax
intermediate
2:00remaining
Which option will cause a syntax error in this Text composable usage?
Look at these Text composable calls. Which one will cause a syntax error?
AText(text = "Welcome", fontWeight = FontWeight.Bold, color = Color.Blue)
BText(text = "Welcome", fontWeight = Bold)
CText(text = "Welcome", fontWeight = FontWeight.Bold)
DText(text = "Welcome")
Attempts:
2 left
💡 Hint
Check if all parameters are properly referenced with their full names.
❓ lifecycle
advanced
2:00remaining
What happens if you update the text parameter of a Text composable dynamically?
Suppose you have a state variable holding a string and you pass it to Text composable's text parameter. What happens when you update that state?
Android Kotlin
var message by remember { mutableStateOf("Hello") }
Text(text = message)
// Later message = "Hi"
AThe Text composable automatically re-renders to show the updated message.
BThe Text composable will not update until the app is restarted.
CThe Text composable shows both old and new messages simultaneously.
DThe Text composable throws a runtime error when the state changes.
Attempts:
2 left
💡 Hint
Think about how Compose handles state and recomposition.
❓ navigation
advanced
2:00remaining
How to make a Text composable clickable to navigate to another screen?
You want to make a Text composable that when tapped navigates to a new screen. Which approach is correct?
AUse Text(text = "Click me", navigate = "destination") to specify navigation.
BSet Text's onClick parameter to navController.navigate("destination").
CWrap Text in a Clickable modifier and call navController.navigate("destination") inside onClick.
DText composable cannot be clickable; use Button instead.
Attempts:
2 left
💡 Hint
Modifiers can add click behavior to composables.
🔧 Debug
expert
2:00remaining
Why does this Text composable not show the expected color?
Given this code:
Text(text = "Hello", color = Color(255, 0, 0))

The text appears black instead of red. Why?
Android Kotlin
Text(text = "Hello", color = Color(255, 0, 0))
AColor constructor expects values from 0f to 1f floats, not 0-255 integers.
BColor constructor is deprecated and ignored by Text composable.
CText composable only accepts predefined colors like Color.Red.
DThe color parameter must be a hex string, not a Color object.
Attempts:
2 left
💡 Hint
Check the expected parameter types for Color constructor in Compose.

Practice

(1/5)
1. What is the main purpose of the Text composable in Jetpack Compose?
easy
A. To display text on the screen
B. To create buttons
C. To handle user input
D. To manage app navigation

Solution

  1. Step 1: Understand the role of Text composable

    The Text composable is designed to show words or sentences visibly on the app screen.
  2. Step 2: Compare with other UI elements

    Buttons handle clicks, input fields take user input, and navigation manages screen changes, so these are not the Text composable's job.
  3. Final Answer:

    To display text on the screen -> Option A
  4. Quick Check:

    Text composable = display text [OK]
Hint: Text composable always shows text content [OK]
Common Mistakes:
  • Confusing Text with Button composable
  • Thinking Text handles user input
  • Assuming Text manages navigation
2. Which of the following is the correct syntax to display the text "Hello World" using the Text composable?
easy
A. Text: "Hello World"
B. Text(content = "Hello World")
C. Text("Hello World")
D. Text { "Hello World" }

Solution

  1. Step 1: Recall Text composable syntax

    The Text composable accepts the text content as the first positional parameter.
  2. Step 2: Check each option

    A uses invalid colon syntax. B correctly uses positional argument Text("Hello World"). C uses invalid content parameter. D uses lambda syntax which is incorrect for Text.
  3. Final Answer:

    Text("Hello World") -> Option C
  4. Quick Check:

    Text("string") = positional first arg [OK]
Hint: Use Text("your text") for simple display [OK]
Common Mistakes:
  • Using content= parameter name
  • Using colon instead of parentheses
  • Trying to pass text inside braces
3. What will be displayed on the screen when this code runs?
Text(text = "Welcome", fontSize = 24.sp, color = Color.Red)
medium
A. The word "Welcome" in default color and size
B. The word "Welcome" in red color with font size 24 sp
C. An error because fontSize is not a valid parameter
D. The word "Welcome" in blue color with font size 24 sp

Solution

  1. Step 1: Understand parameters used

    The fontSize parameter sets the text size, and color sets the text color. Both are valid for Text composable.
  2. Step 2: Analyze the values

    Font size is 24.sp (scaled pixels), color is Color.Red, so text appears red and sized 24 sp.
  3. Final Answer:

    The word "Welcome" in red color with font size 24 sp -> Option B
  4. Quick Check:

    fontSize and color change text appearance [OK]
Hint: fontSize and color change text look visibly [OK]
Common Mistakes:
  • Assuming default color is applied ignoring color param
  • Thinking fontSize is invalid
  • Confusing color values
4. Identify the error in this Text composable code:
Text(text = Hello World)
medium
A. fontSize parameter is required
B. Incorrect parameter name, should be content instead of text
C. Text composable cannot display multiple words
D. Missing quotes around the string "Hello World"

Solution

  1. Step 1: Check string syntax

    In Kotlin, string literals must be enclosed in double quotes. Here, "Hello World" is missing quotes.
  2. Step 2: Validate parameter name and requirements

    The parameter name text is correct. Text composable can display multiple words. fontSize is optional.
  3. Final Answer:

    Missing quotes around the string "Hello World" -> Option D
  4. Quick Check:

    Strings need quotes in Kotlin [OK]
Hint: Always put text strings inside double quotes [OK]
Common Mistakes:
  • Forgetting quotes around text
  • Using wrong parameter names
  • Thinking fontSize is mandatory
5. You want to display a greeting message that changes color based on a Boolean variable isMorning. Which code correctly uses the Text composable to show "Good Morning" in green if isMorning is true, otherwise "Good Evening" in blue?
hard
A. Text(text = if (isMorning) "Good Morning" else "Good Evening", color = if (isMorning) Color.Green else Color.Blue)
B. Text(text = "Good Morning" or "Good Evening", color = Color.Green or Color.Blue)
C. Text(text = "Good Morning" && "Good Evening", color = Color.Green && Color.Blue)
D. Text(text = isMorning ? "Good Morning" : "Good Evening", color = isMorning ? Color.Green : Color.Blue)

Solution

  1. Step 1: Understand Kotlin conditional expressions

    Kotlin uses if (condition) value1 else value2 for inline conditions, not the ternary operator.
  2. Step 2: Analyze each option

    Text(text = if (isMorning) "Good Morning" else "Good Evening", color = if (isMorning) Color.Green else Color.Blue) correctly uses Kotlin's if-else expressions for both text and color. Text(text = "Good Morning" or "Good Evening", color = Color.Green or Color.Blue) uses invalid syntax with 'or'. Text(text = "Good Morning" && "Good Evening", color = Color.Green && Color.Blue) uses '&&' which is logical AND, not for choosing values. Text(text = isMorning ? "Good Morning" : "Good Evening", color = isMorning ? Color.Green : Color.Blue) uses ternary operator syntax which Kotlin does not support.
  3. Final Answer:

    Text(text = if (isMorning) "Good Morning" else "Good Evening", color = if (isMorning) Color.Green else Color.Blue) -> Option A
  4. Quick Check:

    Kotlin uses if-else, not ?: for inline conditions [OK]
Hint: Use Kotlin if-else, not ?:, for inline conditions [OK]
Common Mistakes:
  • Using ?: ternary operator syntax
  • Using logical operators instead of conditions
  • Trying to combine strings or colors with 'or' or '&&'
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