Schedule a 30 minute appointment with a client advisor today. Start
Engineering, Design, Marketing, and More

Swift interview questions

The most popular questions

Use our complementary questions and answers to filter and hire the best. Questions crowdsourced by our clients, answers by Punch. We provide these complementary questions to help our clients more quickly create tests for potential hires at their organizations.

Get a question answered
Punch offers four divisions of services: design, engineering, staffing, and demand

Interview questions for your next interview

Question
What is wrong with the following piece of code, and how can this be fixed?let count: Int = 33
let sum: UInt = 8452
let average: Double = 256.12

var checksum = count + sum + average
Answer
In Swift, there is no implicit casting between data types. Therefore an explicit conversion is required to covert all variables to the same data type before evaluating the expression. This can be fixed as follows:var checksum = Double(count) + Double(sum) + average
Question
What is the value of scoreA.score after this piece of code executes?struct ScoreBoard {
var score: Int = 5
}

var scoreA = ScoreBoard()
var scoreB = scoreA

scoreB.score += 1
Answer
The value of scoreA.score is 5 whereas the value of scoreB.score is 6. In swift, structs are value types and are therefore copied by value. When scoreA is assigned to scoreB, a copy is created due to which any change to scoreB is not reflected into scoreA.

Find developers today

Hire a Punch engineer

Punch offers four divisions of services: design, engineering, staffing, and demand. Our four divisions form the core of our People Forward Approach.

Contact us
Find developers today
Question
What is the output of this piece of code? Explain the difference in behavior of `color` and `weekday` variables.var color = "blue"
var weekday = "Monday"

let closure = { [color] in
print("My favorite color is \(color)")
print("Today is \(weekday)")
}

color = "green"
weekday = "Tuesday"

closure()
Answer
The output is:My favorite color is blue
Today is Tuesday
The capture list of the closure creates a copy of color when it is declared so changing it’s value afterwards has no effect on what the closure holds. The variable weekday is omitted in the capture list and it’s therefore used as a reference instead of a copy. Any changes made to the weekday variable is also reflected inside the closure when it is invoked.
Question
What are the type of integers in Swift?
Answer
Similar to how it is in C, Swift also has unsigned and signed integers which are provided in 8, 16, 32 and 64 bit forms.
Question
What is optional chaining?
Answer
Swift has a concept of Optional type which can have two possible value - nil or some value of the data type available in Swift. Optional chaining is a technique, or a query, which lets you run code only if your optional has a value. process of querying and calling properties. It is possible to chain multiple queries together, and if any link in the chain is nil then, the entire chain fails.
Question
What would cause this piece of code to crash the application and how would you fix it?var defaults = UserDefaults.standard
var username = defaults.string(forKey: "username")!

printString(string: username)

func printString(string: String) {
print(string)
}
Answer
The second line of code returns an optional to handle cases where the key is not found.

If the key exists, then the above code works without any errors. If the key doesn’t exist, or the corresponding value is not a string, the app crashes because the nil value is force unwrapped.

One solution to fix this issue is to use optional binding:if let username = defaults.string(forKey: "username") {
printString(string: username)
}
Question
Explain why a compile time error occurs. How can you fix it?struct Punch {
var names = [String]()
func add(x: String) {
names.append(x) // Compile time error here.
}
}
Answer
Compile error: “Cannot use mutating member on immutable value.”

The explanation is that in Swift, structs are value types and the properties of a value type cannot be modified from within its instance methods. This is the default behavior.

Fix: You can allow such modification to occur by declaring the instance methods as `mutating`.struct Punch {
var names = [String]()
mutating func add(x: String) {
names.append(x) // Compile time error here.
}
}

Ask a question

Ask a question, and one of our engineers will answer it.

We keep our questions nice and simple to be useful for everyone, and we may not answer or publish every question.

Your number is stored privately and never shared.
By submitting a question, you agree to our terms.
Request sent. Thank you!
Send another one