Swift interview questions and answers

  • 2020-05-27 07:18:41
  • OfStack

preface

Swift has been around for more than a year and is now one of the most popular languages. Although its syntax is simple and easy to use, Swift is actually a very complex language. Because it is not only object-oriented but also a functional programming language. This article introduces some common interview questions of Swift. You can use these questions to ask the interviewer or to test your current knowledge of Swift. Don't worry if you don't know the answers, because there are corresponding answers at the bottom of each question.

1. For an array, write a function and swap the two elements in the array

2X programmer:

That's easy. Just write down the results


func swap(_ nums: inout [Int], _ p: Int, _ q: Int) {
 let temp = nums[p]
 nums[p] = nums[q]
 nums[q] = temp 
}

Ordinary programmer:

First talk to the interviewer. What kind of array is it? The interviewer will say, whatever. The average programmer smiles and writes the following code


func swap<T>(_ nums: inout [T], _ p: Int, _ q: Int) {
 let temp = nums[p]
 nums[p] = nums[q]
 nums[q] = temp 
}

Literary programmer:

To communicate with the interviewer, what kind of array is it? What are the other requirements and restrictions? The interviewer will say, this is an Swift interview question. The literary programmer understood and wrote the following answers


func swap<T>(_ nums: inout [T], _ p: Int, _ q: Int) {
 (nums[p], nums[q]) = (nums[q], nums[p])
}

At the same time, write corresponding tests on the above code to detect various boundary conditions. After confirming that, I will say, "I have finished this problem".

This seemingly simple question actually examines the programmer's awareness of topic review, communication, and testing. The generics of Swift and the properties of Tuple are technically investigated.

2. What's wrong with the following code


public class Node {
 public var value: Int
 public var prev: Node?
 public var post: Node?

 public init(_ value: Int) {
 self.value = value
 }
}

Answer: yes var prev or var post weak.

Reason: on the face of it, the above code is perfectly fine. But if I write it like this, the question is:


let head = Node(0)
let tail = Node(1)
head.post = tail
tail.prev = head

At this point, head and tail point to each other, forming a circular reference (retain cycle).

3. Implement 1 function, input is any 1 integer, output to return the input integer + 2

A lot of people come up and say,


func addTwo(_ num: Int) -> Int {
 return num + 2
}

And then the interviewer says, well, what if I want to do plus 4? The programmer thinks for 1 and then defines another method:


func addFour(_ num: Int) -> Int {
 return num + 4
}

And the interviewer says, well, what if I want to return + 6, + 8? Can you just define a method once? The correct way to write it is to use the cauchy property of Swift:


func add(_ num: Int) -> (Int) -> Int {
 return { val in
 return num + val
 }
}

let addTwo = add(2), addFour = add(4), addSix = add(6), addEight = add(8)

4. Simplify the following code


func divide(dividend: Double?, by divisor: Double?) -> Double? { 
 if dividend == nil { 
 return nil 
 } 
 if divisor == nil { 
 return nil 
 } 
 if divisor == 0 { 
 return nil
 } 
 return dividend! / divisor!
}

This question looks at the guard let statement and optional chaining statements. The best answer is


func divide(dividend: Double?, by divisor: Double?) -> Double? { 
 guard let dividend = dividend, let divisor = divisor, divisor != 0 else {
 return nil
 }

 return dividend / divisor
}

5. What does the following function print out?


func swap<T>(_ nums: inout [T], _ p: Int, _ q: Int) {
 let temp = nums[p]
 nums[p] = nums[q]
 nums[q] = temp 
}
0

Since clousre has declared that it has copied car ([car]]), car in clousre is a local variable and no longer related to car outside, so it will print out "I drive Benz".

At this point the interviewer smiled and slightly changed the topic as follows:


func swap<T>(_ nums: inout [T], _ p: Int, _ q: Int) {
 let temp = nums[p]
 nums[p] = nums[q]
 nums[q] = temp 
}
1

At this point, closure does not declare copy copy car, so clousre still USES the global car variable. At this point, I drive Tesla will print out "I drive Tesla".

6. What does the following code print out?


func swap<T>(_ nums: inout [T], _ p: Int, _ q: Int) {
 let temp = nums[p]
 nums[p] = nums[q]
 nums[q] = temp 
}
2

Answer: print out the following two lines


["tomato", "basil", "mozzarella"]
["tomato", "basil", "mozzarella"]

In the code of Lombardis, the code of makeMargherita is overridden, so makeMargherita in Lombardis is always called.

One step further, we put protocol Pizzeria func makeMargherita() Get rid of that, and the code becomes


func swap<T>(_ nums: inout [T], _ p: Int, _ q: Int) {
 let temp = nums[p]
 nums[p] = nums[q]
 nums[q] = temp 
}
4

Print out the following results:


func swap<T>(_ nums: inout [T], _ p: Int, _ q: Int) {
 let temp = nums[p]
 nums[p] = nums[q]
 nums[q] = temp 
}
5

Because lombardis1 is Pizzeria makeMargherita() There is a default implementation, at which point we call the default implementation.

7. What is the difference between defining constants in Swift and Objective-C?

1 people will feel that there is no difference, because written as if there is no difference.

OC defines constants like this:


func swap<T>(_ nums: inout [T], _ p: Int, _ q: Int) {
 let temp = nums[p]
 nums[p] = nums[q]
 nums[q] = temp 
}
6

Swift defines constants like this:


func swap<T>(_ nums: inout [T], _ p: Int, _ q: Int) {
 let temp = nums[p]
 nums[p] = nums[q]
 nums[q] = temp 
}
7

First of all, the first difference is that OC USES const for constants, while Swift USES let for constants.

The above distinction is further stated that the constant types and values indicated by const in OC are determined at compilation time; In Swift, let only indicates a constant (which can only be assigned once), whose type and value can be either static or a dynamic calculation method, which is determined at runtime.

8. What is the difference between struct and class in Swift? Let's take an example from an application

struct is the value type and class is the reference type.

As anyone who has seen WWDC will know, struct was recommended by apple because it is more secure than class for small data model delivery and copying, and especially useful for multithreading and network requests.

Let's look at a simple example:


class A {
 var val = 1
}

var a = A()
var b = a
b.val = 2

At this point a's val has also been changed to 2, because a and b are both reference types, essentially pointing to the same memory. The solution to this problem is to use struct:


func swap<T>(_ nums: inout [T], _ p: Int, _ q: Int) {
 let temp = nums[p]
 nums[p] = nums[q]
 nums[q] = temp 
}
9

Now A is struct, the value type, b and a are different things, changing b has no effect on a.

9. Is Swift an object-oriented or functional programming language?

Swift is both an object-oriented and a functional programming language.

The reason Swift is Object-oriented is because Swift supports class encapsulation, inheritance, and polymorphism, and in this respect is almost identical to a purely object-oriented language like Java.

Said Swift is functional programming language, because Swift support map reduce, filter, flatmap this kind of method to remove the intermediate state, the mathematical function type, more emphasis on computing results rather than the middle process.

conclusion

The above is all about Swift interview questions, I hope the content of this article can bring you a definite help in your study or work, if you have any questions, you can leave a message to communicate.


Related articles: