0
0
Android Kotlinmobile~20 mins

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

Choose your learning style9 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.