← All Swift Flashcard Decks

Case Studies & Practical Application Flashcards

7 cards from real Swift practice questions. Tap to flip, then mark Knew It or Still Learning — missed cards come back until you master them.

Read the first 7 Case Studies & Practical Application flashcards as text
  1. You're building a feature flag system. Flags are checked thousands of times per second. A dictionary-backed solution with `String` keys works but a colleague proposes using an enum. What is the runtime advantage?

    Answer: Enum cases are compared as integers, making switch exhaustive and O(1) with no hashing

    Switching on an enum compiles to an integer comparison or jump table, which is faster and exhaustively checked by the compiler, unlike string-keyed dictionary lookups.

  2. A `RecipeParser` must support JSON, XML, and CSV formats. You want to add new formats without modifying the parser. Which design embodies the Open/Closed Principle in Swift?

    Answer: Define a `FormatParser` protocol; create `JSONParser`, `XMLParser`, `CSVParser` conformances injected at runtime

    Protocol-based strategy injection lets you add `YAMLParser` later by writing a new conformance, leaving `RecipeParser` itself unchanged — open for extension, closed for modification.

  3. Your app's test suite uses real `URLSession` calls and intermittently fails due to network conditions. What is the most effective fix without rewriting the production API layer?

    Answer: Introduce a `URLProtocol` subclass that intercepts requests and returns canned responses

    A custom `URLProtocol` subclass registered with the test session intercepts all URLSession requests at the protocol level, returning deterministic mock responses without changing production code.

  4. You profile an app with Instruments and find a hot path allocates a new `TransformMatrix` struct on every animation frame. What optimization should you investigate first?

    Answer: Cache or reuse the struct across frames rather than reallocating each time

    Avoiding per-frame allocation by caching the struct (or using a pre-allocated buffer) reduces memory pressure and eliminates the initialization cost on the hot path.

  5. A Swift property wrapper `@Clamped(range: 0...100)` should constrain any `Int` to the given range on set. Which stored property inside the wrapper holds the backing value?

    Answer: `wrappedValue` with a setter that clamps before storing to a private variable

    Property wrappers use `wrappedValue` as the primary interface; a custom setter there can clamp the incoming value before assigning it to a private backing store.

  6. You need to conditionally include debug-only diagnostic code in a Swift framework without any runtime cost in release builds. Which tool achieves this?

    Answer: Wrapping code in `#if DEBUG ... #endif` compiler directives

    `#if DEBUG` is a compile-time conditional: the enclosed code is completely stripped from release builds, adding zero binary size or runtime cost.

  7. A `StreamProcessor` conforms to `AsyncSequence` and yields values over time. A view controller consumes it with `for await value in stream`. What happens to the loop when the view controller is deallocated?

    Answer: If the `Task` running the loop is cancelled (e.g., in `deinit`), the loop exits at the next `await` suspension point

    Structured concurrency requires explicit cancellation; storing the `Task` and calling `.cancel()` in `deinit` causes the `for await` loop to exit cleanly at its next suspension point.

How this page was made: written with AI tools and published by Nathan Beja, founder of Practice Test Geeks, who is responsible for its content. We have not yet matched this exam's facts to an official source, so confirm fees, dates and rules with the exam body. Not reviewed by a subject-matter expert. Editorial policy