Interview questions on Swift programming language asked in multinational corporations (MNCs), along with explanations:
- What is Optionals in Swift? Explain with an example.
- Answer: Optionals in Swift are a type that represents either a wrapped value or nil, indicating that a value may or may not be present. This helps in handling situations where a value might be missing. For example:
swift
var anotherOptionalString: String? = nilvar optionalString: String? = "Hello"
// optionalString has a value of "Hello"
// anotherOptionalString has no value (nil)
- Answer: Optionals in Swift are a type that represents either a wrapped value or nil, indicating that a value may or may not be present. This helps in handling situations where a value might be missing. For example:
- What is the difference between ‘let’ and ‘var’ in Swift?
- Answer: In Swift, ‘let’ is used to declare constants, which cannot be changed once initialized, while ‘var’ is used to declare variables, which can be changed later. For example:
swift
variableValue = 30 // Validlet constantValue = 10
var variableValue = 20
constantValue = 15 // Error: Cannot assign to value: ‘constantValue’ is a ‘let’ constant
- Answer: In Swift, ‘let’ is used to declare constants, which cannot be changed once initialized, while ‘var’ is used to declare variables, which can be changed later. For example:
- Explain the concept of Automatic Reference Counting (ARC) in Swift.
- Answer: Automatic Reference Counting (ARC) is a memory management mechanism used in Swift to automatically track and manage the allocation and deallocation of memory for class instances. ARC keeps track of how many references are held to each instance. When an instance is no longer referenced, ARC automatically deallocates the memory occupied by that instance. Developers do not need to manage memory manually as in languages like Objective-C. Example:
swift
var reference1: Person?class Person {
var name: String
init(name: String) {
self.name = name
print("\(name) is being initialized")
}
deinit {
print("\(name) is being deinitialized")
}
}
var reference2: Person?
var reference3: Person?
reference1 = Person(name: “John”)
reference2 = reference1
reference3 = reference1
reference1 = nil
reference2 = nil
// Person instance “John” is still referenced by reference3
reference3 = nil
// Person instance “John” is deallocated since no more references exist
- Answer: Automatic Reference Counting (ARC) is a memory management mechanism used in Swift to automatically track and manage the allocation and deallocation of memory for class instances. ARC keeps track of how many references are held to each instance. When an instance is no longer referenced, ARC automatically deallocates the memory occupied by that instance. Developers do not need to manage memory manually as in languages like Objective-C. Example:
- What is a closure in Swift? Provide an example.
- Answer: A closure in Swift is a self-contained block of functionality that can be passed around and used in your code. It can capture and store references to any constants and variables from the context in which it is defined. Closures can be written in a lightweight syntax, making them easy to use. Example:
swift
let numbers = [1, 2, 3, 4, 5]
let doubledNumbers = numbers.map({ (number: Int) -> Int in
return number * 2
})
print(doubledNumbers) // Output: [2, 4, 6, 8, 10]
- Answer: A closure in Swift is a self-contained block of functionality that can be passed around and used in your code. It can capture and store references to any constants and variables from the context in which it is defined. Closures can be written in a lightweight syntax, making them easy to use. Example:
- Explain the difference between value types and reference types in Swift.
- Answer: In Swift, value types are copied when they are assigned to a variable or passed to a function. They include basic data types like Int, Float, String, and also structs and enums. On the other hand, reference types are not copied when they are assigned to a variable or passed to a function. They include classes and closures. When you change a value type, you are working with a copy, while when you change a reference type, you are modifying the same instance that other variables might also be referring to. Example:
swift
// Reference Type (Class)// Value Type (Struct)
struct Point {
var x: Int
var y: Int
}
var point1 = Point(x: 1, y: 2)
var point2 = point1
point2.x = 5
print(point1.x) // Output: 1 (unchanged)
print(point2.x) // Output: 5 (changed)
class Person {
var name: String
init(name: String) {
self.name = name
}
}
var person1 = Person(name: “Alice”)
var person2 = person1
person2.name = “Bob”
print(person1.name) // Output: Bob (changed)
print(person2.name) // Output: Bob (changed)
- Answer: In Swift, value types are copied when they are assigned to a variable or passed to a function. They include basic data types like Int, Float, String, and also structs and enums. On the other hand, reference types are not copied when they are assigned to a variable or passed to a function. They include classes and closures. When you change a value type, you are working with a copy, while when you change a reference type, you are modifying the same instance that other variables might also be referring to. Example:
These questions cover some fundamental concepts of Swift and are often asked in interviews at MNCs. Understanding them thoroughly can greatly enhance your chances of success in Swift-related interviews.