Bird
Raised Fist0
Swiftprogramming~20 mins

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

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Swift 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 Swift code using type inference?

Consider the following Swift code snippet. What will be printed?

Swift
let number = 42
let message = "The answer is \(number)"
print(message)
A42
BThe answer is number
CThe answer is 42
DError: Cannot infer type
Attempts:
2 left
💡 Hint

Look at how Swift infers the type of number and uses string interpolation.

❓ Predict Output
intermediate
2:00remaining
What type does the compiler infer for this variable?

Given this Swift code, what type does the compiler infer for value?

Swift
let value = 3.14
AString
BFloat
CInt
DDouble
Attempts:
2 left
💡 Hint

Swift defaults to a certain floating-point type when no explicit type is given.

🔧 Debug
advanced
2:00remaining
Why does this Swift code cause a compile error?

Examine the code below. Why does the compiler produce an error?

Swift
let x = nil
print(x)
Aprint requires a non-optional argument
BCannot infer type for nil without explicit type annotation
Cnil is not allowed in Swift
Dx is immutable and cannot be printed
Attempts:
2 left
💡 Hint

Think about what type nil represents and how Swift infers types.

❓ Predict Output
advanced
2:00remaining
What is the inferred type and output of this Swift closure?

Consider this Swift code snippet. What is the inferred type of increment and what will be printed?

Swift
let increment = { (number: Int) in number + 1 }
print(increment(5))
AType: (Int) -> Int, Output: 6
BType: (Int) -> Int, Output: 5
CType: (Int) -> Void, Output: 6
DType: () -> Int, Output: 6
Attempts:
2 left
💡 Hint

Look at the closure parameter and return expression.

🧠 Conceptual
expert
2:00remaining
How does Swift's type inference handle mixed numeric literals in an expression?

What is the inferred type of result in the following Swift code?

let result = 5 + 3.0
ADouble
BInt
CFloat
DError: Cannot mix Int and Double without casting
Attempts:
2 left
💡 Hint

Swift promotes numeric types to the more general type in mixed expressions.

Practice

(1/5)
1. What does type inference mean in Swift?
easy
A. The program runs slower because types are guessed.
B. You must always write the type explicitly.
C. The compiler automatically figures out the variable's type.
D. You cannot use variables without specifying types.

Solution

  1. Step 1: Understand type inference concept

    Type inference means the compiler guesses the type of a variable based on the value assigned.
  2. Step 2: Compare options with definition

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

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

    Type inference = automatic type detection [OK]
Hint: Type inference means compiler guesses type automatically [OK]
Common Mistakes:
  • Thinking you must always write types explicitly
  • Believing type inference slows down the program
  • Confusing type inference with dynamic typing
2. Which of the following Swift variable declarations uses type inference correctly?
easy
A. let number = 10
B. let number: = 10
C. let number Int = 10
D. let number : Int 10

Solution

  1. Step 1: Check syntax for variable declaration with type inference

    Type inference allows omitting the type, so let number = 10 is correct.
  2. Step 2: Identify syntax errors in other options

    Options B, C, and D have syntax errors: missing type name, wrong order, or missing assignment operator.
  3. Final Answer:

    let number = 10 -> Option A
  4. Quick Check:

    Correct syntax with inference = let number = 10 [OK]
Hint: Look for correct Swift syntax without explicit type [OK]
Common Mistakes:
  • Adding colon without type name
  • Writing type before variable name incorrectly
  • Omitting assignment operator
3. What is the type of variable value in this Swift code?
let value = 3.14
medium
A. Double
B. Int
C. String
D. Float

Solution

  1. Step 1: Identify the literal type assigned

    The number 3.14 is a decimal number, which Swift treats as Double by default.
  2. Step 2: Confirm Swift default for floating-point literals

    Swift infers floating-point literals as Double unless specified otherwise.
  3. Final Answer:

    Double -> Option A
  4. Quick Check:

    3.14 literal = Double type [OK]
Hint: Decimal numbers default to Double in Swift [OK]
Common Mistakes:
  • Assuming 3.14 is Float by default
  • Confusing Int with floating-point types
  • Thinking literals default to String
4. Identify the error in this Swift code snippet:
var name = "Alice"
name = 42
medium
A. No error, type inference allows changing type.
B. Error: Cannot assign Int to a variable inferred as String.
C. Error: Missing semicolon after first line.
D. Error: Variable must be declared with explicit type.

Solution

  1. Step 1: Check initial type inferred for variable

    Variable name is assigned a String "Alice", so compiler infers type String.
  2. Step 2: Analyze reassignment with different type

    Assigning an Int (42) to a String variable causes a type mismatch error.
  3. Final Answer:

    Error: Cannot assign Int to a variable inferred as String. -> Option B
  4. Quick Check:

    Type mismatch on reassignment = Error [OK]
Hint: Variable type fixed by first assignment, cannot change later [OK]
Common Mistakes:
  • Thinking type inference allows changing variable type
  • Assuming semicolons are required in Swift
  • Believing explicit type declaration is always needed
5. Given this Swift code, what is the type of result?
let numbers = [1, 2, 3]
let result = numbers.map { $0 * 2 }
hard
A. Array<Any>
B. Int
C. [Double]
D. [Int]

Solution

  1. Step 1: Understand the type of numbers

    Array numbers contains Int values, so its type is [Int].
  2. Step 2: Analyze the map operation and type inference

    The map multiplies each Int by 2, producing another Int for each element, so the result is an array of Ints.
  3. Step 3: Confirm inferred type of result

    Since map returns an array of transformed elements, result is inferred as [Int].
  4. Final Answer:

    [Int] -> Option D
  5. Quick Check:

    map on [Int] with Int output = [Int] [OK]
Hint: map returns array of same type as transformed elements [OK]
Common Mistakes:
  • Thinking map returns a single Int
  • Assuming map changes type to Double automatically
  • Believing result is Array<Any> without type inference
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