Bird
Raised Fist0
Kotlinprogramming~20 mins

Type inference by the compiler in Kotlin - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Kotlin Type Inference Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this Kotlin code with type inference?
Consider the following Kotlin code snippet. What will be printed when it runs?
Kotlin
fun main() {
    val x = listOf(1, 2, 3).map { it * 2 }
    println(x)
}
A[2, 4, 6]
B[1, 2, 3]
C[1, 4, 9]
DCompilation error
Attempts:
2 left
💡 Hint
The map function applies the lambda to each element, doubling it.
❓ Predict Output
intermediate
2:00remaining
What type does the compiler infer for this variable?
Given the Kotlin code below, what is the inferred type of variable result?
Kotlin
val result = listOf("apple", "banana", "cherry").filter { it.startsWith('b') }
AMutableList<String>
BList<String>
CSet<String>
DArray<String>
Attempts:
2 left
💡 Hint
The filter function returns the same collection type as the original list but filtered.
🔧 Debug
advanced
2:00remaining
What error does this Kotlin code produce due to type inference?
Examine the Kotlin code below. What error will the compiler report?
Kotlin
fun main() {
    val x = null
    println(x)
}
AType inference error: Null can not be a value of a non-null type
BRuntime NullPointerException
CCompilation succeeds and prints 'null'
DSyntax error: missing type declaration
Attempts:
2 left
💡 Hint
Kotlin requires explicit type for null without context.
❓ Predict Output
advanced
2:00remaining
What is the output of this Kotlin code using type inference with lambdas?
What will this Kotlin program print when executed?
Kotlin
fun main() {
    val numbers = listOf(1, 2, 3, 4)
    val doubled = numbers.map { it * 2 }
    val filtered = doubled.filter { it > 4 }
    println(filtered)
}
A[1, 2, 3, 4]
B[2, 4]
C[6, 8]
DCompilation error due to type mismatch
Attempts:
2 left
💡 Hint
First double each number, then keep only those greater than 4.
🧠 Conceptual
expert
3:00remaining
Which statement about Kotlin's type inference is correct?
Choose the correct statement about how Kotlin infers types in the following scenario.
Kotlin
val x = listOf(1, 2, 3).map { if (it % 2 == 0) it else "odd" }
AThe inferred type of x is List<String> because of the else branch
BThe inferred type of x is List<Int> because all elements are numbers
CThe code causes a compilation error due to mixed types in the lambda
DThe inferred type of x is List<Any> because the lambda returns Int or String
Attempts:
2 left
💡 Hint
Kotlin infers the closest common supertype when lambda returns different types.

Practice

(1/5)
1. What does type inference mean in Kotlin?
easy
A. The compiler automatically figures out the variable's type.
B. You must always specify the variable type manually.
C. The program runs slower because types are guessed at runtime.
D. It means variables cannot change their values.

Solution

  1. Step 1: Understand type inference concept

    Type inference means the compiler looks at the value assigned and decides the variable's type automatically.
  2. Step 2: Compare options with definition

    Only The compiler automatically figures out the variable's type. correctly describes this automatic type detection by the compiler.
  3. Final Answer:

    The compiler automatically figures out the variable's type. -> Option A
  4. Quick Check:

    Type inference = automatic type detection [OK]
Hint: Type inference means no need to write types explicitly [OK]
Common Mistakes:
  • Thinking types are guessed at runtime
  • Believing you must always write types
  • Confusing type inference with variable mutability
2. Which of the following Kotlin variable declarations uses type inference correctly?
easy
A. val number: String = 10
B. val number: Int = "10"
C. val number = 10
D. val number =

Solution

  1. Step 1: Check each declaration for correctness

    val number = 10 assigns 10 to 'number' without specifying type, so compiler infers Int type correctly.
  2. Step 2: Identify errors in other options

    val number: Int = "10" assigns a String to Int type explicitly - error. val number: String = 10 assigns Int to String type - error. val number = is incomplete syntax.
  3. Final Answer:

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

    Correct syntax with inferred type = val number = 10 [OK]
Hint: Look for variable with value but no explicit type [OK]
Common Mistakes:
  • Mixing types in assignment
  • Leaving declaration incomplete
  • Assigning wrong type to explicit type
3. What is the inferred type of result in this Kotlin code?
val result = listOf(1, 2, 3).map { it * 2 }
medium
A. Array<Int>
B. List<Int>
C. Int
D. List<String>

Solution

  1. Step 1: Analyze the expression assigned to result

    listOf(1, 2, 3) creates a List<Int>. The map function multiplies each element by 2, so the result is a List<Int>.
  2. Step 2: Confirm the inferred type

    Since map returns a List of the transformed elements, the inferred type is List<Int>.
  3. Final Answer:

    List<Int> -> Option B
  4. Quick Check:

    Mapping Int list with Int operation = List<Int> [OK]
Hint: map keeps list type, changes element type if needed [OK]
Common Mistakes:
  • Confusing List with Array
  • Thinking map returns a single Int
  • Assuming type changes to String
4. Identify the error in this Kotlin code snippet related to type inference:
val x = null
val y = x + 5
medium
A. Cannot add Int to null value
B. x cannot be null without explicit type
C. Type inference infers x as Int automatically
D. No error, code runs fine

Solution

  1. Step 1: Understand type inference for null assignment

    Assigning null without type makes x's type Nothing?, which cannot be used in arithmetic.
  2. Step 2: Check the operation y = x + 5

    Adding 5 to null is invalid; Kotlin does not allow arithmetic with null values.
  3. Final Answer:

    Cannot add Int to null value -> Option A
  4. Quick Check:

    Arithmetic with null causes error [OK]
Hint: Null needs explicit type; can't do math with null [OK]
Common Mistakes:
  • Assuming null has Int type
  • Ignoring null safety rules
  • Thinking code compiles without error
5. Given this Kotlin code, what is the inferred type of data?
val data = mapOf("a" to 1, "b" to 2).mapValues { it.value * 10 }
hard
A. List>
B. List<Int>
C. Map
D. Map

Solution

  1. Step 1: Analyze mapOf and mapValues usage

    mapOf creates a Map. mapValues transforms each value by multiplying by 10, keeping keys same.
  2. Step 2: Determine the resulting type

    mapValues returns a Map with the same key type and transformed value type, so Map.
  3. Final Answer:

    Map -> Option D
  4. Quick Check:

    mapValues keeps Map type, changes values [OK]
Hint: mapValues keeps Map type, changes only values [OK]
Common Mistakes:
  • Confusing mapValues with map returning List
  • Assuming values become Strings
  • Thinking result is a List instead of Map
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