Swift Interview Questions Hindi mein

Swift कोर्स के 25 सवाल, हर सवाल के सही जवाब और explanation के साथ। जवाब देखने से पहले ख़ुद सोचें।

Getting Started with Swift

1. Which feature is central to Swift's safety against null-pointer crashes?

  1. Optionals that make the absence of a value explicit
  2. Garbage collection
  3. Manual memory management
  4. Dynamic typing
जवाब देखें

जवाब: Optionals that make the absence of a value explicit

Optionals force code to handle missing values explicitly.

पाठ: What Swift Is and Where It Runs

2. Which command runs the tests of a Swift package?

  1. swift run
  2. swift package init
  3. xcodebuild install
  4. swift test
जवाब देखें

जवाब: swift test

swift test builds and runs the package's test targets.

पाठ: Xcode, Playgrounds and the Swift Package Manager

3. What happens when you add an Int and a Double directly in Swift?

  1. The Int is converted automatically
  2. The Double is truncated
  3. It is a compile error; you must convert one explicitly
  4. The result is a String
जवाब देखें

जवाब: It is a compile error; you must convert one explicitly

Swift has no implicit numeric conversions, so you write Double(x) or Int(y).

पाठ: Constants, Variables, Types and Strings

Optionals, Control Flow and Collections

4. What does `guard let user else { return }` do when user is nil?

  1. Crashes the program
  2. Assigns a default user
  3. Exits the current scope early via the else branch
  4. Continues with user as nil
जवाब देखें

जवाब: Exits the current scope early via the else branch

guard requires the else branch to exit; after it, user is unwrapped for the rest of the scope.

पाठ: Optionals

5. Which statement about Swift switch is true?

  1. Cases fall through by default
  2. switch must be exhaustive and cases do not fall through
  3. switch only works with integers
  4. Every case needs break
जवाब देखें

जवाब: switch must be exhaustive and cases do not fall through

Exhaustiveness and no implicit fall-through make switch safer than in C.

पाठ: Control Flow and Pattern Matching

6. What does copy-on-write mean for Swift arrays?

  1. Copies share storage until one of them is modified, then the data is actually copied
  2. Arrays cannot be copied
  3. Every assignment copies all elements immediately
  4. Arrays are reference types
जवाब देखें

जवाब: Copies share storage until one of them is modified, then the data is actually copied

Copy-on-write gives value semantics with the efficiency of shared storage.

पाठ: Arrays, Dictionaries, Sets and Functional Operations

Structs, Enums and Classes

7. Why must some struct methods be marked mutating?

  1. Because they modify the struct's own properties, which value types only allow explicitly
  2. To make them public
  3. To make them asynchronous
  4. Because structs cannot have methods
जवाब देखें

जवाब: Because they modify the struct's own properties, which value types only allow explicitly

mutating marks methods that change self, and they require a var instance.

पाठ: Structs and Value Semantics

8. What advantage do associated values give enums?

  1. They make enums faster
  2. They allow inheritance
  3. They remove the need for switch
  4. Each case can carry its own relevant data, making invalid states unrepresentable
जवाब देखें

जवाब: Each case can carry its own relevant data, making invalid states unrepresentable

Associated values keep exactly the data each case needs.

पाठ: Enums with Associated Values

9. What does the === operator check for class instances?

  1. Equal property values
  2. Whether two classes share a superclass
  3. Whether two references point to the same instance
  4. Whether instances are Equatable
जवाब देखें

जवाब: Whether two references point to the same instance

=== is identity comparison; == uses Equatable value equality.

पाठ: Classes, Inheritance and Initialisation

Functions, Closures and Errors

10. How do you let a function modify a caller's variable in Swift?

  1. Use a var parameter
  2. Return a pointer
  3. Declare the parameter inout and pass the argument with &
  4. It is not possible
जवाब देखें

जवाब: Declare the parameter inout and pass the argument with &

inout parameters write their final value back to the caller's variable.

पाठ: Functions and Argument Labels

11. When must a closure parameter be marked @escaping?

  1. Always
  2. When the function stores the closure or calls it after returning
  3. When the closure returns a value
  4. When it uses $0
जवाब देखें

जवाब: When the function stores the closure or calls it after returning

Escaping closures outlive the function call, which affects memory management and capturing.

पाठ: Closures and Capture Lists

12. What does try? do with a thrown error?

  1. Converts the result to nil when an error is thrown
  2. Crashes the app
  3. Rethrows it automatically
  4. Prints it
जवाब देखें

जवाब: Converts the result to nil when an error is thrown

try? turns failures into nil optionals, discarding the error value.

पाठ: Error Handling

Protocols, Extensions and Generics

13. How can a protocol provide behaviour shared by all conforming types?

  1. Through a protocol extension with default implementations
  2. Through inheritance only
  3. By making the protocol a class
  4. It cannot
जवाब देखें

जवाब: Through a protocol extension with default implementations

Protocol extensions supply default implementations that conforming types inherit.

पाठ: Protocols and Extensions

14. When do you need `any Protocol` rather than `some Protocol`?

  1. Never
  2. When returning from SwiftUI body
  3. When using generics with constraints
  4. When values of different concrete types must be stored together, such as in [any Shape]
जवाब देखें

जवाब: When values of different concrete types must be stored together, such as in [any Shape]

Existentials allow heterogeneous values; opaque types stand for one fixed concrete type.

पाठ: Generics, Associated Types, some and any

15. Which protocol lets a Swift type be converted to and from JSON with JSONEncoder and JSONDecoder?

  1. Hashable
  2. Identifiable
  3. Codable
  4. Comparable
जवाब देखें

जवाब: Codable

Codable combines Encodable and Decodable for serialisation.

पाठ: Codable, Equatable, Hashable and Comparable

Memory, Access Control and Language Features

16. What happens to a weak reference when the object it refers to is deallocated?

  1. The app crashes
  2. It keeps the object alive
  3. It automatically becomes nil
  4. It points to a copy of the object
जवाब देखें

जवाब: It automatically becomes nil

Weak references are zeroed automatically, which is why they must be optional variables.

पाठ: Automatic Reference Counting

17. What is the default access level in Swift?

  1. private
  2. internal
  3. public
  4. open
जवाब देखें

जवाब: internal

Declarations are internal by default: visible throughout their module.

पाठ: Access Control and Modules

18. Which Swift feature makes SwiftUI's declarative view syntax inside VStack { ... } possible?

  1. Result builders
  2. Property wrappers
  3. Typed throws
  4. Access control
जवाब देखें

जवाब: Result builders

Result builders combine the statements in a closure into a single view value.

पाठ: Property Wrappers, Result Builders and Macros

Concurrency

19. What does `async let` do?

  1. Starts a child task that runs concurrently and is awaited later
  2. Runs code synchronously
  3. Creates a detached task with no parent
  4. Blocks the thread until done
जवाब देखें

जवाब: Starts a child task that runs concurrently and is awaited later

async let creates a concurrent child task bound to the current scope.

पाठ: async/await and Structured Concurrency

20. What does the Swift 6 language mode add to concurrency?

  1. A garbage collector
  2. Automatic multithreading of all loops
  3. Removal of async/await
  4. Compile-time checking of isolation and Sendable to prevent data races
जवाब देखें

जवाब: Compile-time checking of isolation and Sendable to prevent data races

Swift 6 turns potential data races into compile-time errors.

पाठ: Actors, MainActor, Sendable and Swift 6 Data-Race Safety

21. How does cancellation work in Swift concurrency?

  1. It forcibly kills the thread
  2. It restarts the task
  3. It is cooperative: tasks check for cancellation and stop themselves
  4. It is not supported
जवाब देखें

जवाब: It is cooperative: tasks check for cancellation and stop themselves

cancel() sets a flag that code observes via checkCancellation or isCancelled.

पाठ: Cancellation and AsyncSequence

Apps, Testing, Ecosystem and Revision

22. In SwiftUI, what does `var body: some View` use?

  1. An existential any View
  2. A class hierarchy
  3. An opaque return type: one specific view type hidden from callers
  4. A property wrapper
जवाब देखें

जवाब: An opaque return type: one specific view type hidden from callers

some View lets the compiler know the exact type while keeping it hidden and efficient.

पाठ: A First Look at SwiftUI

23. In the Swift Testing framework, which macro checks a condition and reports sub-expression values on failure?

  1. XCTAssertTrue
  2. #expect
  3. @Test
  4. @testable
जवाब देखें

जवाब: #expect

#expect is Swift Testing's primary expectation macro.

पाठ: Testing with Swift Testing and XCTest

25. Which keyword marks a class reference that does not keep the object alive and becomes nil automatically?

  1. strong
  2. unowned
  3. lazy
  4. weak
जवाब देखें

जवाब: weak

weak references are zeroing and do not increase the reference count.

पाठ: Revision and Interview Questions

← कोर्स पर वापस जाएँ