Bird
Raised Fist0
Swiftprogramming~20 mins

Typealias for custom naming in Swift - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Swift Typealias Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of code using typealias with struct
What is the output of this Swift code using typealias for a custom name?
Swift
struct Point {
    var x: Int
    var y: Int
}
typealias Coordinate = Point

let p: Coordinate = Coordinate(x: 3, y: 4)
print("x: \(p.x), y: \(p.y)")
Ax: 3, y: 4
Bx: 4, y: 3
CCompile error: Cannot assign to typealias
DRuntime error: Unexpected nil value
Attempts:
2 left
💡 Hint
Remember that typealias just creates a new name for an existing type.
❓ Predict Output
intermediate
2:00remaining
Output of typealias with function type
What will this Swift code print when using typealias for a function type?
Swift
typealias Operation = (Int, Int) -> Int

func add(_ a: Int, _ b: Int) -> Int {
    return a + b
}

let op: Operation = add
print(op(5, 7))
ACompile error: Cannot assign function to typealias
B12
C57
DRuntime error: Function not found
Attempts:
2 left
💡 Hint
A typealias for a function type can be used to declare variables holding functions.
❓ Predict Output
advanced
2:00remaining
Output of nested typealias usage
What is the output of this Swift code using nested typealias inside a struct?
Swift
struct Container {
    typealias Item = String
    var items: [Item]
}

let c = Container(items: ["apple", "banana"])
print(c.items[1])
ARuntime error: Index out of range
Bapple
Cbanana
DCompile error: Cannot use typealias inside struct
Attempts:
2 left
💡 Hint
Typealias inside a struct can be used as a shorthand for types used in the struct.
❓ Predict Output
advanced
2:00remaining
Output of typealias with tuple type
What will this Swift code print when using typealias for a tuple type?
Swift
typealias NameAge = (name: String, age: Int)

let person: NameAge = (name: "John", age: 30)
print("\(person.name) is \(person.age) years old")
ARuntime error: Missing tuple element
B(John, 30)
CCompile error: Cannot use tuple with typealias
DJohn is 30 years old
Attempts:
2 left
💡 Hint
Typealias can be used to name tuple types for easier reuse.
❓ Predict Output
expert
3:00remaining
Output of typealias with generic type aliasing
What is the output of this Swift code using a generic typealias?
Swift
typealias Pair<T> = (first: T, second: T)

let numbers: Pair<Int> = (first: 10, second: 20)
let words: Pair<String> = (first: "hello", second: "world")
print(numbers.first + numbers.second)
print(words.first + " " + words.second)
A
30
hello world
B
1020
hello world
CCompile error: Generic typealias not supported
DRuntime error: Cannot add Int and String
Attempts:
2 left
💡 Hint
Generic typealias can create reusable tuple types with different element types.

Practice

(1/5)
1. What is the main purpose of using typealias in Swift?
easy
A. To create a new variable
B. To define a new class or struct
C. To declare a constant value
D. To create a new name for an existing type to improve code readability

Solution

  1. Step 1: Understand the role of typealias

    typealias is used to give an existing type a new name, making code easier to read and maintain.
  2. Step 2: Compare with other options

    Defining classes, constants, or variables are done with other keywords like class, let, or var, not typealias.
  3. Final Answer:

    To create a new name for an existing type to improve code readability -> Option D
  4. Quick Check:

    Typealias = new name for type [OK]
Hint: Remember: typealias renames types, not values or classes [OK]
Common Mistakes:
  • Confusing typealias with variable declaration
  • Thinking typealias creates new types instead of new names
  • Using typealias to declare constants or functions
2. Which of the following is the correct syntax to create a typealias for Int named Age in Swift?
easy
A. typealias Age = Int
B. alias type Age = Int
C. type Agealias = Int
D. let Age: Int

Solution

  1. Step 1: Recall the correct syntax for typealias

    The correct syntax is typealias NewName = ExistingType.
  2. Step 2: Check each option

    typealias Age = Int matches the correct syntax exactly. Options B and C have wrong keyword order or spelling. let Age: Int declares a constant, not a typealias.
  3. Final Answer:

    typealias Age = Int -> Option A
  4. Quick Check:

    Syntax: typealias Name = Type [OK]
Hint: Syntax is always: typealias NewName = ExistingType [OK]
Common Mistakes:
  • Swapping keywords like 'alias type' instead of 'typealias'
  • Using 'let' or 'var' instead of 'typealias'
  • Misspelling 'typealias'
3. What will be the output of this Swift code?
typealias Text = String
let greeting: Text = "Hello"
print(greeting)
medium
A. Text
B. Hello
C. String
D. Compilation error

Solution

  1. Step 1: Understand the typealias effect

    Text is just another name for String. So greeting is a String with value "Hello".
  2. Step 2: Analyze the print statement

    Printing greeting outputs the string value "Hello".
  3. Final Answer:

    Hello -> Option B
  4. Quick Check:

    typealias does not change value, prints "Hello" [OK]
Hint: typealias is just a name, output is the actual value [OK]
Common Mistakes:
  • Thinking typealias changes the value printed
  • Expecting the alias name to print instead of the value
  • Assuming a syntax error due to typealias
4. Identify the error in this Swift code:
typealias Number = Int
var value: Number = "10"
medium
A. Cannot assign a String to a variable of typealias Int
B. typealias syntax is incorrect
C. Variable declaration missing 'let'
D. No error, code runs fine

Solution

  1. Step 1: Understand typealias binding

    Number is an alias for Int, so value must be an integer.
  2. Step 2: Check assigned value type

    Assigning a string "10" to an integer type variable causes a type mismatch error.
  3. Final Answer:

    Cannot assign a String to a variable of typealias Int -> Option A
  4. Quick Check:

    Type mismatch error = Cannot assign a String to a variable of typealias Int [OK]
Hint: Alias type must match assigned value type exactly [OK]
Common Mistakes:
  • Thinking typealias allows different types to be assigned
  • Ignoring type mismatch errors
  • Confusing var and let keywords
5. Given this code, what is the type of operation?
typealias Operation = (Int, Int) -> Int
let operation: Operation = { a, b in a + b }
hard
A. An Int value
B. A tuple of two Ints
C. A function that takes two Ints and returns an Int
D. A syntax error due to incorrect closure

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]
Hint: 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
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