An extension in Swift adds new functionality to an existing class, struct, enum, or protocol without modifying the original source code.
Click to reveal answer
beginner
How do you declare an extension for a struct named Person?
Use the syntax:
extension Person { // new methods or properties }
Click to reveal answer
intermediate
Can extensions add stored properties to types in Swift?
No, extensions cannot add stored properties. They can only add computed properties, methods, initializers, and conform to protocols.
Click to reveal answer
beginner
What is the benefit of using extensions in Swift?
Extensions help organize code better, add functionality to types you don’t own, and conform to protocols without subclassing.
Click to reveal answer
beginner
Show an example of adding a method greet() to a struct Person using extension.
extension Person { func greet() { print("Hello!") } }
Click to reveal answer
Which of the following can you NOT add using a Swift extension?
AComputed properties
BMethods
CStored properties
DInitializers
✗ Incorrect
Extensions cannot add stored properties, only computed properties, methods, and initializers.
How do you start an extension declaration for a class named Car?
Aextension Car { }
Bclass Car extension { }
Cextend Car { }
DCar extension { }
✗ Incorrect
The correct syntax is extension Car { }.
Can extensions in Swift add protocol conformance to existing types?
ANo
BYes
COnly for classes
DOnly for structs
✗ Incorrect
Extensions can add protocol conformance to any type.
Which keyword is used to declare an extension in Swift?
Amodify
Bextend
Cadd
Dextension
✗ Incorrect
The keyword extension is used to declare extensions.
What can extensions NOT do?
AOverride existing methods
BAdd computed properties
CAdd new methods
DAdd initializers
✗ Incorrect
Extensions cannot override existing methods.
Explain what Swift extensions are and why they are useful.
Think about how you can add features to existing types without changing their original code.
You got /5 concepts.
Describe the syntax of declaring an extension and what you can add inside it.
Remember the basic structure and what features extensions support.
You got /7 concepts.
Practice
(1/5)
1. What is the main purpose of using extension in Swift?
easy
A. To add new functionality to an existing type without modifying its original code
B. To create a new subclass from an existing class
C. To replace the original implementation of a type
D. To delete methods from a type
Solution
Step 1: Understand what extensions do
Extensions allow adding new features to existing types without changing their original source code.
Step 2: Compare options with this definition
Only adding new functionality to an existing type without modifying its original code correctly describes this purpose. Replacing original implementation refers to overriding, creating subclasses is inheritance, and deleting methods is unrelated to extensions.
Final Answer:
To add new functionality to an existing type without modifying its original code -> Option A
Quick Check:
Extension purpose = Add features without changing original code [OK]
Hint: Extensions add features without changing original code [OK]
Common Mistakes:
Confusing extensions with subclassing
Thinking extensions replace original code
Believing extensions can remove methods
2. Which of the following is the correct syntax to start an extension for the String type in Swift?
easy
A. extend String { }
B. extension String { }
C. extension String() { }
D. extension String[] { }
Solution
Step 1: Recall Swift extension syntax
The correct syntax starts with the keyword extension followed by the type name without parentheses or brackets.
In Swift, single-expression functions can omit the return keyword.
Step 2: Verify if extension on Double is allowed
Extensions can add methods to Double. The method name 'half' is valid.
Final Answer:
No error, code is correct -> Option D
Quick Check:
Single-expression methods can omit return [OK]
Hint: Single-expression methods can omit 'return' keyword [OK]
Common Mistakes:
Assuming 'return' is always required
Thinking extensions can't add methods to built-in types
Believing method names have restrictions
5. You want to add a computed property isEven to Int using an extension that returns true if the number is even, otherwise false. Which code correctly implements this?
hard
A. extension Int { var isEven: Bool { if self % 2 == 0 { return true } } }
B. extension Int { func isEven() -> Bool { return self % 2 == 0 } }
C. extension Int { var isEven: Bool { self % 2 == 0 } }
D. extension Int { var isEven: Bool { self / 2 == 0 } }
Solution
Step 1: Understand computed property syntax
Computed properties use var with a getter. extension Int { var isEven: Bool { self % 2 == 0 } } uses correct concise syntax; the func version declares a method, the if version lacks an else branch causing compile error, the division version has wrong logic.
Step 2: Check logic correctness
extension Int { var isEven: Bool { self % 2 == 0 } } uses concise expression self % 2 == 0 which returns true if even. extension Int { var isEven: Bool { if self % 2 == 0 { return true } } } omits else branch, causing compiler error. extension Int { var isEven: Bool { self / 2 == 0 } } incorrectly uses division instead of modulo.
Step 3: Verify distractors
The method requires parentheses to call like num.isEven(). Incomplete if fails to compile. Division fails for even numbers like 2 (2/2 == 1 != 0).
Final Answer:
extension Int { var isEven: Bool { self % 2 == 0 } } -> Option C
Quick Check:
Computed property with modulo check = extension Int { var isEven: Bool { self % 2 == 0 } } [OK]
Hint: Use computed property with 'self % 2 == 0' for even check [OK]
Common Mistakes:
Using division instead of modulo
Confusing method with computed property
Forgetting to return a Bool value
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.