Bird
Raised Fist0
Android Kotlinmobile~20 mins

Data types and type inference in Android Kotlin - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Data Types Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
📝 Syntax
intermediate
2:00remaining
Kotlin Type Inference Behavior
What is the inferred type of the variable result in the following Kotlin code?
Android Kotlin
val result = listOf(1, 2, 3).map { it * 2 }
AList<Double>
BArray<Int>
CList<Int>
DIntArray
Attempts:
2 left
💡 Hint
Think about what the map function returns when applied to a list of integers.
❓ ui_behavior
intermediate
2:00remaining
Kotlin Variable Type and UI Display
Given this Kotlin code snippet in an Android app, what will be displayed in the TextView?
Android Kotlin
val message = "Hello" + 123
textView.text = message
AHello123
BHello 123
CHello+123
DCompilation error
Attempts:
2 left
💡 Hint
Consider how Kotlin handles string concatenation with numbers.
❓ lifecycle
advanced
2:00remaining
Type Inference Impact on Android Lifecycle
Consider this Kotlin property in an Android Activity:
val count = 0
What is the type of count and what happens if you try to assign count = 5 later in the Activity lifecycle?
A<code>count</code> is inferred as Any and can hold any type.
B<code>count</code> is a nullable Int and can be assigned null.
C<code>count</code> is a mutable Int and can be reassigned without error.
D<code>count</code> is an Int and cannot be reassigned; trying to assign causes a compilation error.
Attempts:
2 left
💡 Hint
Look at the keyword used to declare count and what it means in Kotlin.
❓ navigation
advanced
2:00remaining
Type Inference in Navigation Arguments
In Kotlin Android Navigation, you pass an argument to a destination like this:
val action = HomeFragmentDirections.actionToDetail(itemId = 42)
findNavController().navigate(action)
What is the inferred type of itemId in the generated code?
AInt
BString
CAny
DLong
Attempts:
2 left
💡 Hint
Look at the value passed to itemId and how Kotlin infers types from literals.
🔧 Debug
expert
2:00remaining
Debugging Type Inference with Nullable Types
What error will this Kotlin code produce when compiled?
Android Kotlin
val name = null
val length = name.length
ACompilation error: Only safe (?.) or non-null asserted (!!.) calls are allowed on a nullable receiver
BCompilation error: Variable 'name' must have a type explicitly specified
CNo error, length is 0
DRuntime NullPointerException
Attempts:
2 left
💡 Hint
Consider what Kotlin infers when you assign null without a type.

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