1. What does Swift's type inference mean?
Swift's type inference lets you omit the explicit type annotation and the compiler will deduce the type from the assigned value. This helps reduce boilerplate while maintaining type safety.
Get the Preplance app for a seamless learning experience. Practice offline, get daily streaks, and stay ahead with real-time interview updates.
Get it on
Google Play
4.9/5 Rating on Store
Swift · Question Set
Swift Basics & Syntax interview questions for placements and exams.
Questions
11
Included in this set
Subject
Swift
Explore more sets
Difficulty
Mixed
Level of this set
Go through each question and its explanation. Use this set as a focused practice pack for Swift.
Swift's type inference lets you omit the explicit type annotation and the compiler will deduce the type from the assigned value. This helps reduce boilerplate while maintaining type safety.
For complete preparation, combine this set with full subject-wise practice for Swift. You can also explore other subjects and sets from the links below.
Optional chaining uses `?.` to attempt method or property access on an optional. If the optional is nil, the result is nil and the call safely returns without crash. This allows safe, concise traversal of optional-rich APIs.
In Swift you define an `enum` (or `struct`/`class`) that conforms to the `Error` protocol to represent error types. /n/n Functions or methods that can throw must be marked with `throws`. When calling such a function you use `try`, and you wrap calls inside a `do { … } catch { … }` block to handle errors. /n/n You can also use `try?` to get an optional result or `try!` if you are certain there will be no error (though risky). For example: `enum FileError: Error { case notFound case unreadable } func readFile(path: String) throws -> String { … } do { let text = try readFile(path: path) } catch FileError.notFound { … } catch { … }`. This mechanism ensures that error-handling is explicit and safe.
Value semantics mean that when you assign or pass a struct instance you get a copy; changes to the copy don't affect the original. Reference semantics mean that when you assign or pass a class instance you pass a reference to the same object; changes through one reference affect all. /n/n In Swift structs are value types, classes are reference types. Choosing a `struct` means you get safer, simpler copying behaviour and less unintended sharing; choosing a `class` enables identity, inheritance and shared mutable state. Understanding these differences helps you design robust code and avoid bugs like unintended mutation.
Swift offers five main access levels: `open`, `public`, `internal`, `fileprivate` and `private`. /n/n `open` allows subclassing and use outside the module; `public` allows use outside but not subclassing; `internal` (the default) allows use within the module; `fileprivate` restricts use to the same file; `private` restricts use to the enclosing declaration. /n/n You choose levels based on encapsulation: use `private` for internal implementation, `fileprivate` for multi-type cooperation in one file, `internal` for typical app-module use, `public`/`open` when building frameworks. Clear access control helps maintain modularity and hides implementation details from consumers.
A `guard` statement tests a condition and if it fails, it must exit the current scope (via `return`, `break`, etc.). It helps reduce nested code and makes safe unwrapping of optionals cleaner.
In Swift `let` is used to define a constant whose value cannot change after assignment, whereas `var` defines a variable whose value can change. Using `let` whenever possible signals intent and improves safety.
When you write `Int?` in Swift it means the variable is an optional — it can either hold an `Int` value or `nil`. This helps avoid runtime nil-pointer crashes by making absence explicit.
In Swift, `struct` defines a value type: when assigned or passed, it is copied. `class` defines a reference type: when assigned or passed, it shares the same instance. This difference affects mutability and memory behaviour.
Closures in Swift are blocks of code that can be passed as variables, stored, and can capture constants or variables from their surrounding context. They are fundamental to asynchronous callbacks and functional patterns.
In Swift `defer` declares a block of code that will run when execution leaves the current scope (whether via return, error, or normal path). It's useful for cleanup tasks and ensures deterministic execution.