📘 Swift Language – iOS Interview Questions

0.0(0)
studied byStudied by 0 people
0.0(0)
full-widthCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/33

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

34 Terms

1
New cards

What are the key features of the Swift language?

Swift is a statically typed compiled language focused on safety, expressiveness, and performance. It provides strong type checking, optionals for null safety, and value semantics by default. The language supports protocol-oriented programming, generics, and modern concurrency. Swift is optimized using LLVM and interoperates directly with Objective-C. It favors correctness over permissiveness.

2
New cards

What is a type-safe language and how does Swift enforce it?

A type-safe language prevents operations between incompatible types at compile time. Swift enforces this using static typing, generics, and strict compiler checks. Implicit type coercion is largely disallowed. This shifts many runtime errors to compile time. Type safety significantly improves reliability.

3
New cards

What is type inference in Swift?

Type inference lets the compiler deduce types from context without explicit annotations. Despite inference, Swift remains fully statically typed. This reduces boilerplate while preserving safety. Explicit types are still recommended in public APIs and complex logic. Inference does not relax type correctness.

4
New cards

What is null safety in Swift and how are Optionals used to achieve it?

Swift eliminates implicit null references by introducing Optionals. A value that may be absent must be wrapped in an Optional type. The compiler requires explicit unwrapping before usage. This guarantees handling of missing values. It removes entire classes of runtime crashes.

5
New cards

What are Optionals in Swift and why are they important?

Optionals represent the presence or absence of a value explicitly. They force developers to consider failure paths. Optionals integrate directly into the type system. Unsafe access is prevented unless explicitly overridden. This leads to safer APIs.

6
New cards

What is the difference between Optional Binding and Optional Chaining?

Optional binding unwraps a value conditionally and assigns it to a local variable. Optional chaining allows conditional access to properties or methods. Binding is control-flow oriented. Chaining is expression-oriented. Both safely handle nil propagation.

7
New cards

What makes Enumerations powerful in Swift?

Swift enums support associated values, methods, protocols, and pattern matching. They can model finite state precisely. Associated values allow payloads to vary per case. Exhaustive switch statements enforce completeness. Enums replace many class hierarchies.

8
New cards

What is the difference between let and var in Swift?

let creates an immutable binding, while var allows mutation. Immutability improves safety and reasoning. The compiler can optimize immutable values better. Swift encourages using let by default. Mutability must be explicit.

9
New cards

Can a let constant reference a mutable object?

Yes, let only prevents reassignment of the reference. If the object is a class, its internal state can still mutate. This is due to reference semantics. The distinction between reference and value semantics is critical. This often confuses beginners.

10
New cards

What happens when you declare a reference type using let?

The reference becomes immutable, not the object. ARC behavior remains unchanged. The object can mutate internally. Only reassignment is forbidden. Identity and state are distinct concepts.

11
New cards

How do you create read-only properties in Swift?

Read-only properties can be created using let or computed properties with only a getter. Another approach is private(set). This allows controlled mutation. It improves encapsulation. The choice depends on whether the value is stored or derived.

12
New cards

What is the difference between using let and a computed property with only a get?

let stores a precomputed immutable value. A computed get-only property recalculates on each access. Computed properties can derive from other state. let is more efficient if recomputation is unnecessary. Semantics differ significantly.

13
New cards

How do you make a property read-only outside the class but writable inside it?

Use private(set) access control. External callers can read the value. Internal code can modify it freely. This pattern preserves invariants. It is common for model state.

14
New cards

How does Swift support both value and reference types?

Swift provides structs and enums as value types. Classes implement reference semantics. Value types are copied on assignment with Copy-on-Write optimization. Reference types share identity. Both coexist intentionally.

15
New cards

What is the difference between a value type and a reference type?

Value types are copied when assigned or passed. Reference types share a single instance. Value semantics reduce shared mutable state. Reference semantics enable identity and polymorphism. This affects concurrency and safety.

16
New cards

What is the difference between a class and a struct in Swift?

Classes support inheritance, ARC, and identity. Structs are value types with implicit copying. Structs get memberwise initializers. Classes are heap allocated. Structs are preferred when identity is unnecessary.

17
New cards

What are the practical use cases where a struct is preferred over a class?

Data models, immutable state, configuration objects, and SwiftUI views. Structs are thread-safe by default. They reduce unexpected side effects. They scale well in concurrent environments. Performance is predictable.

18
New cards

Why does SwiftUI use structs for defining views?

SwiftUI views are descriptions, not objects. Structs are cheap to create and destroy. Value semantics allow fast diffing. There is no persistent view identity. State is stored externally.

19
New cards

How is memory management handled in Swift?

Swift uses Automatic Reference Counting for class instances. ARC tracks strong references at runtime. Objects are deallocated when reference count reaches zero. Value types bypass ARC entirely. Ownership rules are explicit.

20
New cards

How is memory management different for classes and structs?

Classes live on the heap and are ARC-managed. Structs are stack allocated or optimized via Copy-on-Write. Structs avoid retain cycles. This makes value types safer by default. Memory ownership is simpler.

21
New cards

What is Copy-on-Write in Swift?

Copy-on-Write delays copying until mutation occurs. Multiple values share storage initially. On mutation, a unique copy is created. This combines value semantics with performance. It is heavily used in Swift stdlib.

22
New cards

How do Array, Dictionary, and String implement Copy-on-Write in Swift?

They share internal buffers across copies. Before mutation, uniqueness is checked. A mutation triggers allocation of new storage. Reads remain cheap. Copies are deferred until needed.

23
New cards

What is protocol-oriented programming and how is it different from object-oriented programming?

POP focuses on behavior composition instead of inheritance. Protocols define behavior without implementation. Extensions supply default implementations. This reduces deep class hierarchies. Swift favors POP for flexibility.

24
New cards

How do Generics work in Swift and why are they useful?

Generics allow writing code independent of concrete types. The compiler enforces type constraints. Generics are fully resolved at compile time. They improve reuse without sacrificing safety. Performance is not impacted.

25
New cards

What are collection types in Swift?

Array stores ordered elements. Set stores unique unordered values. Dictionary maps unique keys to values. All conform to Collection protocols. They use Copy-on-Write internally.

26
New cards

What are the types of access control modifiers available in Swift?

open, public, internal, fileprivate, private. They control visibility across modules and scopes. Swift enforces access rules at compile time. Proper scoping improves API design. internal is the default.

27
New cards

Which access modifier is the default in Swift?

internal. It allows access within the same module. It balances encapsulation and usability. Public APIs require explicit marking. Defaults discourage accidental API exposure.

28
New cards

What is the difference between open and public access modifiers?

public allows access but forbids subclassing or overriding externally. open allows both. open is required for framework extensibility. public is safer by default. The distinction exists for API stability.

29
New cards

What is the difference between private and fileprivate in Swift?

private restricts access to the enclosing declaration. fileprivate allows access within the same file. private is preferred for stronger encapsulation. fileprivate exists for legacy and edge cases. Scope matters.

30
New cards

How does the private(set) access modifier help in encapsulating data while allowing read-only access?

It restricts write access while exposing read access. External code can observe state but not mutate it. Internal logic maintains control. This prevents invalid modification. It improves invariant safety.

31
New cards

What are the key differences between stored properties and computed properties in Swift?

Stored properties hold actual values. Computed properties calculate values on access. Computed properties may have getters and setters. Stored properties require memory allocation. Computed properties do not store data.

32
New cards

What is the purpose of the lazy keyword in Swift, and in which scenarios is it best used?

lazy delays initialization until first access. It improves performance for expensive setup. lazy is only allowed for vars. It captures self during initialization. Common for heavy resources.

33
New cards

What is the purpose of the lazy keyword in Swift, and in which scenarios is it best used?

lazy delays initialization until first access. It improves performance for expensive setup. lazy is only allowed for vars. It captures self during initialization. Common for heavy resources.

34
New cards

What is the role of the defer keyword in Swift, and how is it typically used for resource management?

defer schedules code to run at scope exit. It ensures cleanup executes reliably. Execution is LIFO based on declaration order. Common for closing files or releasing locks. It improves safety.