PLAY READ BLOG

Assess your experience with Swift

I estimate it would take you 38 steps in 16 minutes to Assess your experience with Swift.
What happens when you extend an existing type, like e.g. String?
1. The new functionality will be available to all instances of that type created in the same file, after the extension was defined.

2. The new functionality becomes available to all instances of that type created in the same file where the extension was defined.

3. The new functionality will be available to all instances of that type created after the extension was defined.

4. The new functionality becomes available to all instances of that type, even if they were created before defining the extension.
  • 1.
  • 2.
  • 3.
  • 4.
What might you change in this code to get rid of the compiler warning?

let n = Int("42")
print(n)
1.
let n = Int("42") as! Int
print(n)

2.
if let n = try? Int("42") {
print(n)
}

3.
var n = Int("42")
print(n)

4.
if let n = Int("42") {
print(n)
}
  • 1.
  • 2.
  • 3.
  • 4.
What is the right way to declare an immutable Set that stores String values?
1. let set: Set<String>

2. readonly set: Set<String>

3. const set: Set<String>

4. let set: [String]
  • 1.
  • 2.
  • 3.
  • 4.
What is the purpose of optionals in Swift?
1. To express that a variable might point to a nonexistent object

2. To indicate that a variable might not contain any value

3. To allow referencing an instance before its entirely constructed

4. To represent an instance of any reference or value type
  • 1.
  • 2.
  • 3.
  • 4.
What Swift type is a good choice to represent custom errors?
1. The class

2. The enumeration

3. The tuple

4. The structure
  • 1.
  • 2.
  • 3.
  • 4.
Why do we need to unwrap an optional?
1. To check whether it’s indeed an optional

2. To assign it a non-nil, valid value

3. To make sure that the optional contains a value

4. To make its value available as a temporary constant
  • 1.
  • 2.
  • 3.
  • 4.
What is the purpose of error handling?
1. To avoid errors at runtime by catching them at compile time

2. To respond to error conditions that may occur during execution

3. To prevent writing faulty code by adhering to coding best practices

4. To collect all programming and system errors that occur at runtime
  • 1.
  • 2.
  • 3.
  • 4.
What does the following code do?

let result = [1, 2, 3].filter{ $0 % 2 == 1 }
1. Sums the elements in the collection, divides the sum by two, and returns the result if it’s odd

2. Divides each element in the collection by two and includes it in the result

3. Divides each element in the collection by two, sums them up and returns the result

4. Checks each element in the collection and includes it in the result if it’s odd
  • 1.
  • 2.
  • 3.
  • 4.
What happens if you use forced unwrapping to access a nonexistent optional's value?

var data: Data? = nil
data!.append(contentsOf: [1, 2, 3])
1. The error is ignored and the execution continues.

2. It triggers a runtime error.

3. The code does not compile.

4. The error gets logged and the execution continues.
  • 1.
  • 2.
  • 3.
  • 4.
What does the following code print to the console and why?

protocol Shape {}
class Triangle: Shape {}
class Circle {}
class Rectangle: Shape {}
var count = 0
[Circle(), Rectangle(), Triangle()].forEach { (item) in
if item is Shape {
count += 1
}
}
print(count)
1. It will print: 1, because the three objects are identical

2. It will print: 2, because not all types conform to Shape

3. It will print: 3, since we’ve got an array of three shape instances

4. It will print: 0, none of the instances are of type Shape
  • 1.
  • 2.
  • 3.
  • 4.
What is the value of result after executing this code?

let result = [4, 5, 1, 3].map { (value) -> Bool in value % 2 == 0 }
1. [0, 1, 1, 1]

2. [true, false, false, false]

3. [false, true, true, true]

4. [1, 3, 4, 5]
  • 1.
  • 2.
  • 3.
  • 4.
Which function can return an HTTP error code and the error's description?
1. func evaluate(errorCode: inout Int, description: inout String)

2. func evaluate(errorCode: Int, description: String) -> Int

3. func evaluate(errorCode: Int) -> (http: Int, description: String)

4. func evaluate(errorCode: Int, description: inout String)
  • 1.
  • 2.
  • 3.
  • 4.
What is the right way to evaluate the result of calling this function?

func decrypt(_ data: Data) -> String?
1. print(decrypt(input))

2. if let result = decrypt(input) {
print(result)
}

3. if decrypt(input) != nil {
let result = decrypt(input)
print(result!)
}

4. let result = decrypt(input)
print(result)
  • 1.
  • 2.
  • 3.
  • 4.
What does this code print to the console?

var ints = ["1", "2", "3"]
ints = []
ints.append("4")
for n in ints {
print(n)
}
1. 1 2 3

2. 1 2 3 4

3. 4

4. 4 3 2 1
  • 1.
  • 2.
  • 3.
  • 4.
What are the contents of the three arrays after this code?

var languages = ["Swift", "Objective-C", "Smalltalk"]
var firstCopy = languages
var secondCopy = firstCopy
firstCopy[0] = "Python"
secondCopy[0] = "C++"
1. languages is [“C++”, "Objective-C", "Smalltalk"]
firstCopy is [“C++”, "Objective-C", "Smalltalk"]
secondCopy is [“C++”, "Objective-C", "Smalltalk"]
2. languages is ["Swift", "Objective-C", "Smalltalk"]
firstCopy is [“C++”, "Objective-C", "Smalltalk"]
secondCopy is [“C++”, "Objective-C", "Smalltalk"]
3. languages is ["Swift", "Objective-C", "Smalltalk"]
firstCopy is [“Python”, "Objective-C", "Smalltalk"]
secondCopy is [“C++”, "Objective-C", "Smalltalk"]
4. languages is ["Swift", "Objective-C", "Smalltalk"]
firstCopy is ["Swift", "Objective-C", "Smalltalk"]
secondCopy is ["Swift", "Objective-C", "Smalltalk"]
  • 1.
  • 2.
  • 3.
  • 4.
What is the safest way to check that x was properly initialized and use its value?

let x = Int("42")
1. if let t = x! {
print("\(t)")
}

2. if x {
print(“\(x)”)
}

3. if x == nil {
print(“\(x)”)
}

4. if let t = x {
print("\(t)")
}
  • 1.
  • 2.
  • 3.
  • 4.
Which function can be used in this code?

guard let data = convert(text: "Hello", using: .utf8) else {
fatalError()
}
1. func convert(text: String, using encoding: String.Encoding, result: inout Data?)

2. func convert(text: String, using encoding: String.Encoding = .utf8) -> Data

3. func convert(text: String, using encoding: String.Encoding) -> Data?

4. func convert(text: String, using encoding: String.Encoding) -> Datav
  • 1.
  • 2.
  • 3.
  • 4.
How can we omit the argument label for a function parameter?
1. Write an underscore (_) instead of an argument label

2. Write three period characters (…) instead of an argument label

3. Use an asterisk (*) instead of an argument label

4. Write a question mark (?) instead of an argument label
  • 1.
  • 2.
  • 3.
  • 34.
Get Started   

Or consider 72 other intentions.