InterviewSolution
This section includes InterviewSolutions, each offering curated multiple-choice questions to sharpen your knowledge and support exam preparation. Choose a topic below to get started.
| 1. |
This below given difficult code sorts an array of fruits. Write down ways to code the given logic in a simpler way than the code given below. |
|
Answer» var fruits = ["apples", "oranges", "PAPAYA", "kiwi"]fruits.sort { (a: String, b: String) -> Bool in return a < b}print(fruits) There are three ways in which we can simplify the given code snippet: fruits.sort { (a,b) in return a < b }fruits.sort { $0 < $1 }fruits.sort(by: <)Conclusion:Apple gadgets are used by BILLIONS of people all over the world. The number of iOS users has been constantly INCREASING, which is good news for iOS application developers. iOS Swift developers MUST also keep up with the latest developments in the iOS Swift community. Make sure you are keeping up with Apple developer news, podcasts, and blogs. This is unlikely to be a question in an interview, but it distinguishes you. We hope that the Swift Interview questions answered here are extremely helpful in studying the fundamentals and advanced topics of iOS Swift. Any beginner or experienced professional who understands these Swift and iOS developer interview questions will be able to pass the interview on the first try. All the best for your upcoming interviews! |
|
| 2. |
There is a compile time error in the following code. Are you able to recognise it and explain why it occurs? What are some possible solutions? |
|
Answer» STRUCT CAT{}FUNC showCat(cat: Cat?) { guard let c = cat else { print("No presence of cat") } print(c)} The problem with the given code is that a guard's else block requires an escape path, which can be achieved by RETURNING, throwing an exception, or calling a @noreturn. Adding a return statement is the SIMPLEST approach: func showCat(cat: Cat?) { guard let c = Cat else { print("No presence of cat") return } print(c)}A fatalError() can also be called which is a @noreturn function: struct Cat{}func showCat(cat: Cat?) { guard let c = Cat else { print("No presence of cat") fatalError() } print(c)} |
|
| 3. |
You are on a two-dimensional infinite grid where you can move in any of the eight directions (x,y) to (x-1, y-1), (x-1, y), (x-1, y+1), (x , y-1), (x , y+1), (x+1, y-1), (x+1, y) , (x+1, y+1). |
|
Answer» You are given a list of points to cover and the order in which you must do so. Write an ios Swift code to give the shortest NUMBER of steps you can take to ACHIEVE it. You begin from the first point of the given list. Because the order in which the points are covered is already known, the problem is reduced to figuring out how to calculate the distance between two points (A, B) and the distance between two points (A, B) and the distance between two points (A, B) and the distance between two points (A, B) and the distance between two points (A, B) and the distance (C, D). It is worth noting that just X = ABS(A-C) and Y = abs(A-C) are important (B-D). You will progress along the diagonal while X and Y are both positive, and X and Y will both decrease by one. When one of them reaches zero, you move on to the next stage, reducing the remaining number by one. In other words, the total number of steps would be equal to the maximum number of steps (X, Y) The code in ios Swift to solve this given problem is given below: import Foundationclass Solution { func coverEveryPoint(_ X: INOUT [Int], _ Y: inout [Int]) -> Int { if X.count != Y.count || X.count == 0 || X.count == 1 { return 0 } VAR dist = 0 for j in 0 ..< X.count - 1 { let xOne = X[j] let yOne = Y[j] let xTwo = X[j + 1] let yTwo = Y[j + 1] dist += distanceFinder((xOne , yOne), (xTwo, yTwo)) } return dist } func distanceFinder(_ X: (x: Int, y: Int), _ Y: (x: Int, y: Int)) -> Int { return (abs(X.y - Y.y) - abs(X.x - Y.x)) > 0 ? abs(X.y - Y.y) : abs(X.x - Y.x) }} |
|
| 4. |
To conduct arithmetic or logic tasks, Swift offers a collection of predefined operators. It also enables for the construction of bespoke unary and binary operators. |
|
Answer» Define and implement a custom power ^^ operator that satisfies the following requirements:
There are two steps to creating a new custom operator:
It can be noted that as the program does not take overflows into consideration, in the event of the OPERATION producing a result that Int can't represent, for example, a value more than Int.max, then a runtime error occurs. |
|
| 5. |
Take into consideration the following struct: |
|
Answer» PUBLIC struct Temperature{ public var temp: DOUBLE public init(temp: Double) { self.temp = temp }} Can we do an initialization using the following code: var temp: Temperature = 60.5Yes, Swift SPECIFIES protocols that allow you to use the assignment operator to initialize a type with literal values. Literal initialization of a certain type is possible by adopting the corresponding protocol and supplying a public initializer. You implement ExpressibleByFloatLiteral as follows in the example of Temperature: extension Temperature: ExpressibleByFloatLiteral { public init(floatLiteral VALUE: FloatLiteralType) { self.init(temp: value) }}After this, we can do the above-given initialization without any errors. |
|
| 6. |
Predict the output of the ios Swift code snippet given below |
|
Answer» var item = "apples"let closure = { print("He WANTED to EAT \(item)")}item = "oranges"closure() The above-given code SNIPPET PRINTS "He wanted to eat oranges". If you do not include a capture list in the closure, the compiler will use a reference rather than a copy. As a result, any change to the variable is REFLECTED when the closure is invoked. |
|
| 7. |
Predict the output of the following ios Swift program: |
|
Answer» var item = "apples"let CLOSURE = { [item] in print("He wanted to eat \(item)")}item = "oranges"closure() The output of the given code snippet will be "He wanted to eat apples". When we define the closure, the capture list generates a duplicate of the item. This MEANS that EVEN if you MODIFY the value of an OBJECT, the captured value remains the same. |
|
| 8. |
Take a look at the code snippet given below. Will the given code fail to compile? If yes, why? |
|
Answer» public class TemperatureClass { private(SET) var temp: Double = 50.0 public func changeTemperature(_ temp: Double) { self.temp = temp }}let temperatureClass = TemperatureClass()temperatureClass.changeTemperature(72.3)public struct TemperatureStruct { private(set) var temp: Double = 50.0 public MUTATING func changeTemperature(_ temp: Double) { self.temp = temp }}let temperatureStruct = TemperatureStruct ()temperatureStruct.changeTemperature(72.3) Yes, the code given will FAIL to compile because of the last line of the code. The TemperatureStruct has a mutating method to change its INTERNAL variable temp, which is correctly declared. Because we called changeTemperature on an object generated using let, which is immutable, the compiler gives an error. To make the example compile, change let to var. Methods that affect the internal state of a structure must be MARKED as mutating, but they cannot be invoked from immutable variables. |
|
| 9. |
In the given code snippet, we have used var to declare viewOne and let to create viewTwo. Will the last line compile? |
|
Answer» import UIKitvar viewOne = UIView()viewOne.alpha = 0.7let viewTwo = UIView()viewTwo.alpha = 0.7 // Does this line COMPILE? The last line will, in fact, compile. viewOne is a variable that we can reassign to a new UIView object. Because we can only assign a value once with let, the FOLLOWING code will not compile: viewTwo = viewOne // Error: viewTwo is immutableHowever, because UIView is a reference-based CLASS, we can change the properties of viewTwo — which means the following code will compile: let viewTwo = UIView()viewTwo.alpha = 0.7 // Yes, this COMPILES! |
|
| 10. |
Let us take into consideration the following code snippet in ios Swift: |
|
Answer» struct Course{ var toughness: INT = 3}var courseOne = Course()var courseTwo = courseOnecourseTwo.toughness = 4 What will be the respective values of courseOne.toughness and courseTwo.toughness? If Course was a class instead of a struct, would the values be any DIFFERENT? The value of courseOne.toughness will be 3 and the value of courseTwo.toughness will be 4 if Course is a structure since Structures in ios Swift are value types and the following line just copies the courseOne to courseTwo by value and not by reference: var courseTwo = courseOneIf Course was a class instead of a struct, then the above given line would copy courseOne to courseTwo by reference and therefore, the values of both courseOne and courseTwo would be 4 by the end of the code snippet as they have the same ADDRESS. CLASSES in ios Swift are Reference types therefore both the given courses have the same address. |
|