Bird
0
0

Given this code, what is the type of operation?

hard📝 Application Q15 of 15
Swift - Data Types
Given this code, what is the type of operation?
typealias Operation = (Int, Int) -> Int
let operation: Operation = { a, b in a + b }
AAn Int value
BA tuple of two Ints
CA function that takes two Ints and returns an Int
DA syntax error due to incorrect closure
Step-by-Step Solution
Solution:
  1. Step 1: Understand the typealias definition

    Operation is defined as a function type that takes two Int parameters and returns an Int.
  2. Step 2: Analyze the assigned closure

    The closure matches the Operation type: it takes two Ints and returns their sum as Int.
  3. Final Answer:

    A function that takes two Ints and returns an Int -> Option C
  4. Quick Check:

    Function typealias = A function that takes two Ints and returns an Int [OK]
Quick Trick: Function typealias defines input and output types clearly [OK]
Common Mistakes:
  • Confusing function type with tuple type
  • Thinking closure syntax is incorrect
  • Assuming the alias creates a new type instead of a function signature

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes