Bird
0
0

Identify the error in the following Swift code snippet:

medium📝 Debug Q14 of 15
Swift - Variables and Constants

Identify the error in the following Swift code snippet:

typealias Name = String

func greet(name: Name) {
    print("Hello, \(Name)!")
}
greet(name: "Alice")
AUsing <code>Name</code> inside the print instead of the parameter <code>name</code>
BIncorrect typealias syntax
CMissing return type in function
DCannot use typealias as function parameter type
Step-by-Step Solution
Solution:
  1. Step 1: Check the function parameter and usage

    The function parameter is named name, but inside print, Name (the type alias) is used instead.
  2. Step 2: Understand the difference between parameter and typealias

    Name is a type alias, not a variable holding the value. The print should use name to print the argument.
  3. Final Answer:

    Using Name inside the print instead of the parameter name -> Option A
  4. Quick Check:

    Use parameter name, not typealias name [OK]
Quick Trick: Use variable names, not typealias names inside functions [OK]
Common Mistakes:
  • Confusing type alias name with variable name
  • Assuming typealias can be used as variable
  • Ignoring case sensitivity

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes