Bird
Raised Fist0
Android Kotlinmobile~20 mins

Data types and type inference in Android Kotlin - Mini App: Build & Ship

Choose your learning style10 modes available
Build: Data Types Demo
This screen shows examples of Kotlin data types and how Kotlin infers types automatically.
Target UI
-------------------------
| Data Types Demo       |
|-----------------------|
| Int value:            |
| String value:         |
| Boolean value:        |
| Double value:         |
| Inferred Int:         |
| Inferred String:      |
| Inferred Boolean:     |
| Inferred Double:      |
|                       |
| [Show Values]         |
-------------------------
Display labels for Int, String, Boolean, and Double variables with explicit types.
Display labels for variables with inferred types (no explicit type declared).
Add a button labeled 'Show Values' that when tapped, shows the values of all variables below their labels.
Use Kotlin's type inference for the inferred variables.
Use a simple vertical layout with clear spacing.
Starter Code
Android Kotlin
package com.example.datatypesdemo

import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // TODO: Define variables with explicit types
        // TODO: Define variables with inferred types

        val showButton: Button = findViewById(R.id.showButton)

        showButton.setOnClickListener {
            // TODO: Show variable values in TextViews
        }
    }
}
Task 1
Task 2
Task 3
Task 4
Solution
Android Kotlin
package com.example.datatypesdemo

import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // Explicitly typed variables
        val explicitInt: Int = 42
        val explicitString: String = "Hello Kotlin"
        val explicitBoolean: Boolean = true
        val explicitDouble: Double = 3.14

        // Inferred type variables
        val inferredInt = 100
        val inferredString = "Type Inference"
        val inferredBoolean = false
        val inferredDouble = 2.718

        val showButton: Button = findViewById(R.id.showButton)

        val explicitIntValue: TextView = findViewById(R.id.explicitIntValue)
        val explicitStringValue: TextView = findViewById(R.id.explicitStringValue)
        val explicitBooleanValue: TextView = findViewById(R.id.explicitBooleanValue)
        val explicitDoubleValue: TextView = findViewById(R.id.explicitDoubleValue)

        val inferredIntValue: TextView = findViewById(R.id.inferredIntValue)
        val inferredStringValue: TextView = findViewById(R.id.inferredStringValue)
        val inferredBooleanValue: TextView = findViewById(R.id.inferredBooleanValue)
        val inferredDoubleValue: TextView = findViewById(R.id.inferredDoubleValue)

        showButton.setOnClickListener {
            explicitIntValue.text = explicitInt.toString()
            explicitStringValue.text = explicitString
            explicitBooleanValue.text = explicitBoolean.toString()
            explicitDoubleValue.text = explicitDouble.toString()

            inferredIntValue.text = inferredInt.toString()
            inferredStringValue.text = inferredString
            inferredBooleanValue.text = inferredBoolean.toString()
            inferredDoubleValue.text = inferredDouble.toString()
        }
    }
}

This app screen demonstrates Kotlin data types and type inference.

We declare variables with explicit types like Int, String, Boolean, and Double. Then we declare variables without specifying types, letting Kotlin infer them automatically.

When the user taps the 'Show Values' button, the app displays the values of all variables below their labels. This shows how Kotlin handles both explicit and inferred types clearly.

Final Result
Completed Screen
-------------------------
| Data Types Demo       |
|-----------------------|
| Int value: 42         |
| String value: Hello Kotlin |
| Boolean value: true   |
| Double value: 3.14    |
| Inferred Int: 100     |
| Inferred String: Type Inference |
| Inferred Boolean: false|
| Inferred Double: 2.718|
|                       |
| [Show Values]         |
-------------------------
User taps 'Show Values' button.
Values of all variables appear next to their labels.
Screen updates instantly showing explicit and inferred values.
Stretch Goal
Add a toggle switch to switch between light and dark theme for the screen.
💡 Hint
Use AppCompatDelegate.setDefaultNightMode() with MODE_NIGHT_YES and MODE_NIGHT_NO to switch themes.

Practice

(1/5)
1. Which keyword in Kotlin declares a variable whose value cannot be changed after assignment?
easy
A. val
B. var
C. let
D. const

Solution

  1. Step 1: Understand Kotlin variable keywords

    Kotlin uses val for immutable variables and var for mutable ones.
  2. Step 2: Identify the immutable keyword

    val means the value cannot be changed after assignment.
  3. Final Answer:

    val -> Option A
  4. Quick Check:

    Immutable variable keyword = val [OK]
Hint: Remember: val = fixed, var = changeable [OK]
Common Mistakes:
  • Confusing val and var
  • Using let which is not a Kotlin keyword
  • Thinking const is used like val
2. Which of the following is the correct way to declare an integer variable with value 10 using type inference in Kotlin?
easy
A. val number: Int = 10
B. val number = 10.0
C. var number = "10"
D. val number = 10

Solution

  1. Step 1: Understand type inference in Kotlin

    Kotlin can guess the type from the assigned value, so val number = 10 infers Int.
  2. Step 2: Check each option

    val number: Int = 10 declares type explicitly, which is correct but not type inference. val number = 10 uses type inference correctly. var number = "10" assigns a String, not Int. val number = 10.0 assigns a Double, not Int.
  3. Final Answer:

    val number = 10 -> Option D
  4. Quick Check:

    Type inference assigns Int from 10 [OK]
Hint: Omit type to let Kotlin infer it [OK]
Common Mistakes:
  • Adding explicit type when not needed
  • Assigning wrong data type
  • Confusing Int and Double
3. What will be the output of this Kotlin code?
val x = 5
val y = 2
val result = x / y
println(result)
medium
A. 2.0
B. 2.5
C. 2
D. Compilation error

Solution

  1. Step 1: Identify variable types and operation

    Both x and y are Int by type inference. Integer division truncates decimals.
  2. Step 2: Calculate integer division

    5 / 2 = 2 (decimal part discarded).
  3. Final Answer:

    2 -> Option C
  4. Quick Check:

    Integer division truncates decimals = 2 [OK]
Hint: Integer division drops decimals in Kotlin [OK]
Common Mistakes:
  • Expecting floating point result
  • Using variables as Double without declaration
  • Thinking println shows decimal automatically
4. Identify the error in this Kotlin code snippet:
val name = "Alice"
name = "Bob"
medium
A. Missing semicolon after first line
B. Cannot reassign a val variable
C. String values must use single quotes
D. Variable name is a reserved keyword

Solution

  1. Step 1: Check variable declaration

    val name = "Alice" declares an immutable variable.
  2. Step 2: Check reassignment

    Trying to assign "Bob" to name causes error because val cannot be reassigned.
  3. Final Answer:

    Cannot reassign a val variable -> Option B
  4. Quick Check:

    val variables are immutable [OK]
Hint: val means no reassignment allowed [OK]
Common Mistakes:
  • Thinking val allows reassignment
  • Confusing quotes for strings
  • Assuming semicolons are mandatory
5. Given this Kotlin code, what is the inferred type of result?
val a = 3
val b = 4.5
val result = a + b
hard
A. Double
B. Int
C. Float
D. String

Solution

  1. Step 1: Identify types of operands

    a is Int, b is Double (4.5 is a Double literal).
  2. Step 2: Understand type promotion in arithmetic

    When adding Int and Double, Int is promoted to Double, so result is Double.
  3. Final Answer:

    Double -> Option A
  4. Quick Check:

    Int + Double results in Double [OK]
Hint: Mixed numeric types promote to the wider type [OK]
Common Mistakes:
  • Assuming result is Int
  • Confusing Float and Double
  • Thinking result is String
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