Intermediate Swift test (Intermediate)
30 questions · 35 minutes · Free
Protocols, generics, error handling and SwiftUI basics.
Take the Intermediate Swift test
What it covers
- Protocols and extensions
- Generics
- Error handling
- ARC and memory
- SwiftUI basics
Interview format
- Questions: 30 questions — 18 multiple choice, 9 multi-answer, 3 predict-the-output
- Time: 35 minutes, timed from the first question
- Difficulty: Intermediate
- Skill area: Frontend
- Price: Free
Earn a certificate
The Intermediate Swift test is one of the 4 tests of the iOS Developer: Intermediate learning path. Pass it and the other 3 tests at 70% or more and readyforhires issues you a verifiable certificate in your name, with its own code and verify link to share or add to LinkedIn.
Sample questions
5 sample questions from the Intermediate Swift test, with the answers. Samples are taken out of the live test, so you will not meet these when you sit it.
What is true about a generic function that uses a protocol as a type parameter?
- The function can accept any type that conforms to the protocol
- The function can only accept types that are subclasses of a particular class
- The function can only accept one specific type
- The function cannot accept any type
Answer: A. The function can accept any type that conforms to the protocol
When a generic function uses a protocol as a type parameter, it can accept any type that conforms to that protocol. This allows for flexibility and reusability of the function across multiple types that share common behavior defined by the protocol. The other options are either incorrect or not directly related to the behavior of generic function parameters.
What happens when an error is thrown in a function in Swift?
- The error is propagated to the system level and handled by the operating system.
- The error is propagated to the caller, who must handle it using a do-catch block or rethrow it.
- The error is automatically resolved by the compiler and no action is required from the caller.
- The error is ignored and the function continues execution without any impact.
Answer: B. The error is propagated to the caller, who must handle it using a do-catch block or rethrow it.
In Swift, when a function throws an error, it is propagated to the caller. The caller must handle the error using a do-catch block or rethrow it using the 'rethrow' keyword. This is the correct behavior for error propagation in Swift.
You are working on a SwiftUI app and want to apply a view modifier to a specific Text view to change its font size to 20. Which of the following statements accurately describes what happens when the modifier is applied?
- The view modifier is applied to the Text view and changes its font size to 20.
- The view modifier is not applied, and the font size remains unchanged.
- The view modifier changes the font size of all views in the app to 20.
- The view modifier is applied to the parent view and affects all child views.
Answer: A. The view modifier is applied to the Text view and changes its font size to 20.
In SwiftUI, view modifiers are applied using the .modifier() function or directly as a trailing closure. When a modifier such as .font(.system(size: 20)) is applied to a view, it changes the font size of that specific view. The other options are incorrect because they either describe a different behavior (e.g., changing font size globally) or do not accurately reflect how modifiers are applied in SwiftUI.
Which of the following best describes the purpose of extending a protocol in Swift?
- Extensions are used to define new protocols.
- Extensions allow adding new functionality to a protocol without modifying its original definition.
- Extensions allow a protocol to inherit requirements from another protocol.
- Extensions allow combining multiple protocols into one.
Answer: B. Extensions allow adding new functionality to a protocol without modifying its original definition.
Extensions of a protocol allow adding new functionality without changing the protocol's original definition, making it a powerful tool for augmenting existing protocols. Protocol composition allows combining multiple protocols into one, while protocol inheritance allows a protocol to inherit requirements from another protocol. Protocol extensions are ideal when you want to add default implementations or additional functionality to existing protocols without modifying their source code.
Which of the following statements about closures and strong reference cycles is correct?
- A closure that captures a reference to self strongly can lead to a strong reference cycle.
- A closure that captures a reference to self weakly can cause a crash if self is deallocated.
- A closure that captures a reference to self weakly can lead to a strong reference cycle.
- A closure that captures a reference to self strongly cannot retain any objects.
Answer: A. A closure that captures a reference to self strongly can lead to a strong reference cycle.
A closure that captures a reference to self strongly can lead to a strong reference cycle. This is because the closure retains a strong reference to self, and self retains a strong reference to the closure, creating a cycle. To avoid this, it's common to use weak or unowned references when capturing self in closures.
Frequently asked questions
- How many questions are in the Intermediate Swift test?
- 30 questions (18 multiple choice, 9 multi-answer, 3 predict-the-output), with 35 minutes to answer them.
- What does the Intermediate Swift test cover?
- Protocols and extensions, Generics, Error handling, ARC and memory, SwiftUI basics.
- Is the Intermediate Swift test free?
- Yes. It is free to take with a readyforhires account.
- What level is the Intermediate Swift test?
- Intermediate, in Frontend.