let val = (Double)6
- A syntax issue
- Typecasting
- Assignment
- Initialization
Reference: The Swift Programming Language: Language Guide: The Basics: Constants and Variables
let x = 5
guard x == 5 { return }
- The
guard
is missing theelse
- Nothing is wrong
- The
guard
is missing athen
- The comparison is wrong
Reference: The Swift Programming Language: Language Guide: Control Flow: Early Exit
enum Direction {
case north, south, east, west
}
- There is none
-
String
-
Any
-
Int
Reference: The Swift Programming Language: Language Guide: Enumerations: Raw Values
- It allows multiple synchronous or asynchronous operations to run on different queues.
- It allows track and control execution of multiple operations together.
- It allows operations to wait for each other as desired.
- All of these answers.
Reference: Apple Developer: Documentation: Dispatch: Dispatch Group
let val = 5
print("value is: \(val)")
- String interpolation
- String compilation
- Method chaining
- String concatenation
Reference: The Swift Programming Language: Language Guide: Strings and Characters: String Interpolation
var vals = [10, 2]
vals.sort { (s1, s2) -> Bool in
s1 > s2
}
-
[10, 2]
-
[2, 10]
-
nil
- This code contains an error
Reference: Apple Developer: Documentations: Swift: Array: sort()
typealias Thing = [String: Any]
var stuff: Thing
print(type(of: stuff))
-
Dictionary<String, Any>
-
Dictionary
-
Error
-
Thing
Reference: The Swift Programming Language: Language Reference: Types: Type Identifier
let x = ["1", "2"].dropFirst()
let y = x[0]
- This code contains an error
-
1
-
2
-
nil
explanation
dropFirst()
from Swift.Collection.Array
returns a type of ArraySlice<Element>
as in the documentation pages:
@inlinable public func dropFirst(_ k: Int = 1) -> ArraySlice<Element>
The ArraySlice type makes it fast and efficient for you to perform operations on sections of a larger array. Instead of copying over the elements of a slice to new storage, an ArraySlice instance presents a view onto the storage of a larger array. And because ArraySlice presents the same interface as Array, you can generally perform the same operations on a slice as you could on the original array.
Slices Maintain Indices
Unlike Array and ContiguousArray, the starting index for an ArraySlice instance isn’t always zero. Slices maintain the same indices of the larger array for the same elements, so the starting index of a slice depends on how it was created, letting you perform index-based operations on either a full array or a slice.
The above code returns a slice of value ["2"]
but the index did not change. let y = x[1]
would give the expected result.
To safely reference the starting and ending indices of a slice, always use the startIndex and endIndex properties instead of specific values.
Reference
var test = 1 == 1
-
true
-
YES
-
1
- This code contains an error
Reference: The Swift Programming Language: Language Guide: Basic Operators: Comparison Operators
var x: Int?
let y = x ?? 5
-
5
-
0
-
nil
- This code contains an error
Reference: The Swift Programming Language: Language Guide: Basic Operators: Nil-Coalescing Operators
func add(a: Int, b: Int) -> Int { return a+b }
-
Int
-
(Int, Int) -> Int
-
Int<Optional>
- Functions don't have types.
Reference: The Swift Programming Language: Language Guide: Functions: Function Types
func myFunc(_ a: Int, b: Int) -> Int {
return a + b
}
-
myFunc(5, b: 6)
-
myFunc(5, 6)
-
myFunc(a: 5, b: 6)
-
myFunc(a, b)
- A combination of
Encodable
andDecodable
- Not a true protocol
- Required of all classes
- Automatically included in all classes
References:
- Apple Developer: Documentation: Swift: Swift Standard Library: Encoding, Decoding, and Serialization: Codable
- The Swift Programming Language: Language Guide: Protocols: Protocol Composition
let value1 = "\("test".count)"
-
String
-
Int
-
null
-
test.count
Reference: The Swift Programming Language: Language Guide: Strings and Characters: String Interpolation
- When it's executed after the function returns
- When it's scope is undefined
- When it's lazy loaded
- All of these answers
Reference: The Swift Programming Language: Language Guide: Closures: Escaping Closures
class Person {
var name: String
var address: String
}
- Person has no initializers.
- Person has no base class.
-
var name
is not formatted correctly. -
address
is a keyword.
Reference: The Swift Programming Language: Language Guide: Initialization: Class Inheritance and Initialization
let names = ["Bear", "Joe", "Clark"]
names.map { (s) -> String in
return s.uppercased()
}
-
["BEAR", "JOE", "CLARK"]
-
["B", "J", "C"]
-
["Bear", "Joe", "Clark"]
- This code contains an error.
let val = 5
- A constant named val of type
Int
- A variable named val of type
item
- A constant named val of type
Number
- A variable named val of type
Int
Reference: The Swift Programming Language: Language Guide: The Basics: Type Safety and Type Inference
extension String {
var firstLetter: Character = "c" {
didSet {
print("new value")
}
}
}
- Extensions can't add properties.
- Nothing is wrong with it.
-
didSet
takes a parameter. -
c
is not a character.
Reference: The Swift Programming Language: Language Guide: Extensions: Computed Properties
- Property observers
- Key properties
- All of these answers
-
newOld
value calls
Reference: The Swift Programming Language: Language Guide: Properties
self.callback = {
self.attempts += 1
self.downloadFailed()
}
- Use of
self
inside the closure causes retain cycle. - You cannot assign a value to a closure in this manner.
- You need to define the type of closure explicitly.
- There is nothing wrong with this code.
var vals = Set<String> = ["4", "5", "6"]
vals.insert("5")
- Three
- Four
- Eight
- This code contains an error.
Reference: The Swift Programming Language: Language Guide: Collection Types: Sets
- Use a capture list to set class instances of
weak
orunowned
. - You can't, there will always be a danger of strong reference cycles inside a closure.
- Initialize the closure as read-only.
- Declare the closure variable as
lazy
.
Reference: The Swift Programming Language: Language Guide: Automatic Reference Counting
if let s = String.init("some string") {
print(s)
}
- This
String
initializer does not return an optional. - String does not have an initializer that can take a
String
. -
=
is not a comparison. - Nothing is wrong with this code.
Reference: The Swift Programming Language: Language Guide: The Basics: Optionals
-
typealias CustomClosure = () -> ()
-
typealias CustomClosure { () -> () }
-
typealias CustomClosure -> () -> ()
-
typealias CustomClosure -> () {}
Reference: The Swift Programming Language: Language Reference: Declarations: Type Alias Declaration
-
self
-
instance
-
class
-
this
Reference: The Swift Programming Language: Language Guide: Methods: Instance Methods
- Structs
- Classes
- Optionals
- Generics
Reference: The Swift Programming Language: Language Guide: Structures and Classes
var strings = [1, 2, 3]
- All of these answers
-
strings.append(4)
-
strings.insert(5, at: 1)
-
strings += [5]
Reference: The Swift Programming Language: Language Guide: Collection Types: Arrays
for i in 0...100 {
print(i)
}
- 0
- 101
- 99
- 100
References:
- The Swift Programming Language: Language Guide: Control Flow: For-in Loops
- The Swift Programming Language: Language Guide: Basic Operators: Range Operators
- An instance of any class
- An instance of function type
- All of these answers
- An instance of an optional type
Reference: The Swift Programming Language: Language Guide: Type Casting: Type Casting for Any and AnyObject
let names = ["Larry", "Sven", "Bear"]
let t = names.enumerated().first().offset
- This code does not compile. / This code is invalid.
- 0
- 1
- Larry
References:
- Apple Developer: Documentation: Swift: Array: enumerated()
- Apple Developer: Documentation: Swift: Array
let vt = (name: "ABC", val: 5)
let test = vt.0
-
ABC
-
0
-
5
-
name
References:
- The Swift Programming Language: Language Guide: The Basics: Tuples
- The Swift Programming Language: Language Reference: Expressions: Primary Expressions: Tuple Expression
class LSN: MMM {
}
- MMM
- LSN
- There is no base class.
- This code is invalid.
Reference: The Swift Programming Language: Language Guide: Inheritance: Subclassing
var userLocation: String = "Home" {
willSet(newValue) {
print("About to set userLocation to \(newValue)...")
}
didSet {
if userLocation != oldValue {
print("userLocation updated with new value!")
} else {
print("userLocation already set to that value...")
}
}
}
userLocation = "Work"
-
About to set userLocation to Work... userLocation updated with new value!
-
About to set userLocation to Work... userLocation already set to that value...
-
About to set userLocation to Home... userLocation updated to new value!
-
Error
Reference: The Swift Programming Language: Language Guide: Properties: Property Observers
- A base class convenience initializer
- Either a designated or another convenience initializer
- A designated initializer
- None of these answers
Reference: The Swift Programming Language: Language Guide: Initialization: Class Inheritance and Initialization
- DispatchQueue.visible
- DispatchQueue.global
- errorExample need to be labeled as
throws
. - DispatchQueue.background
Reference: Apple Developer: Documentation: Dispatch: DispatchQueue
let x = ["a", "b", "c"]
-
String[]
-
Array<String>
-
Set<String>
-
Array<Character>
Reference: The Swift Programming Language: Language Guide: Collection Types: Arrays
let nThings: [Any] = [1, "2", "three"]
let oThings = nThings.reduce("") { "\($0)\($1)" }
- 11212three
- 115
- 12three
- Nothing, this code is invalid.
Reference: Apple Developer: Documentation: Swift: Array: reduce(_:_:)
-
!try
-
try?
-
try!
-
?try
Reference: The Swift Programming Language: Language Guide: Error Handling: Handling Errors
protocol TUI {
func add(x1: Int, x2: Int) -> Int {
return x1 + x2
}
}
- Protocol functions cannot have return types.
- Protocol functions cannot have implementations.
- Nothing is wrong with it.
-
add
is a reserved keyword.
Reference:
- The Swift Programming Language: Language Guide: Protocols: Method Requirements
- The Swift Programming Language: Language Guide: Protocols: Protocol Extensions
class Car {
var wheels: Int = 4
let doors = 4
}
- Class members
- This code is invalid
- Class fields
- Class properties
Reference:
- The Swift Programming Language: Language Guide: Structures and Classes
- The Swift Programming Language: Language Guide
- You cannot
-
deinit
-
init?
-
init
Reference:
- The Swift Programming Language: Language Guide: Initialization
- The Swift Programming Language: Language Guide: Deinitialization
let dbl = Double.init("5a")
print(dbl ?? ".asString()")
-
five
-
5a
-
.asString()
-
5
Reference:
- The Swift Programming Language: Language Guide: Basic Operators: Nil-Coalescing Operator
- The Swift Programming Language: Language Guide: Initialization: Failable Initializers
func add(this x: Int, toThat y: Int) { }
- None of these answers
- Local terms
- Argument labels
- Parameters names
Reference: The Swift Programming Language: Language Guide: Functions
for (key, value) in [1: "one", 2: "two"] {
print(key, value)
}
- The interaction source is invalid
- The interaction variable is invalid
- There is nothing wrong with this code
- The comma in the print is misplaced
Reference: The Swift Programming Language: Language Guide: Control Flow: For-In Loops
-
XCTest
- All of these answers
-
@testable
-
XCTAssert
Reference:
- Apple Developer: Documentation: XCTest: XCTest
- Apple Developer: Documentation: XCTest: Boolean Assertions: XCTAssert(_:_:file:line:)
- The Swift Programming Language: Language Guide: Access Control: Access Levels
class Square {
var height: Int = 0
var width: Int {
return height
}
}
- This code contains error
- A closure
- A computed property
- Lazy loading
Reference:
- The Swift Programming Language: Language Guide: Properties: Stored Properties
- The Swift Programming Language: Language Guide: Properties: Computed Properties
- The Swift Programming Language: Language Guide: Closures: Trailing Closures
let vals = ("val", 1)
- A dictionary
- A tuple
- An optional
- This code contains error
Reference:
- The Swift Programming Language: Language Guide: The Basics
- The Swift Programming Language: Language Reference: Types
var x = 5
x = 10.0
- You cannot assign a Double to a variable of type Int
-
x
is undefined -
x
is a constant -
x
has no type
Reference: The Swift Programming Language: Language Guide: The Basics
var items = ["a": 1, "b": 2, "c": "test"] as [String: Any]
items["c"] = nil
print(items["c"] as Any)
- Any
- test
- 1,2,3
- nil
References:
- The Swift Programming Language: Language Guide: Type Casting: Type Casting for Any and AnyObject
- The Swift Programming Language: Language Guide: Collection Types: Dictionaries
let val = 5.0 + 10
- There is nothing wrong with this code
-
val
is a constant and cannot be changed -
5.0
and10
are different types - There is no semicolon
Reference: The Swift Programming Language: Language Guide: The Basics: Type Safety and Type Inference
struct Test {
var score: Int
var date: Date
}
- Zero
- This code contains an error
- Two
- Structs do not have initializers
Reference: The Swift Programming Language: Language Guide: Initialization
let x = try? String.init("test")
print(x)
- nil
- Nothing - this code contains an error
- Optional("test")
- test
References:
- The Swift Programming Language: Language Guide: Error Handling: Handling Errors
- The Swift Programming Language: Language Guide: The Basics: Optionals
var vals = [1, 2, 3]
-
vals.sort { $0 < $1 }
-
vals.sort { (s1, s2) in s1 < s2 }
-
vals.sort(by: <)
- All of these answers
Reference: Apple Developer: Documentation: Swift: Array: sort()
- Not executed
- Executed in the main queue
- None of these answers
- Executed on the background thread
Reference: Apple Developer: Documentation: Dispatch: DispatchQueue: async(group:qos:flags:execute:)
- When a class instance needs memory
- All of these answers
- When the executable code is finished
- When a class instance is being removed from memory
Reference: The Swift Programming Language: Language Guide: Deinitialization
-
String?
-
Optional[String]
-
[String]?
-
?String
Reference: The Swift Programming Language: Language Guide: The Basics: Optionals
for i in ["0", "1"] {
print(i)
}
- One
- Two
- Three
- This code does not compile
Reference: The Swift Programming Language: Language Guide: Control Flow: For-In Loops
let names = ["Bear", "Tony", "Svante"]
print(names[1] + "Bear")
- 1Bear
- BearBear
- TonyBear
- Nothing, this code is invalid
References:
- The Swift Programming Language: Language Guide: Collection Types: Arrays
- The Swift Programming Language: Language Guide: Strings and Characters: Concatenating Strings and Characters
let name: String?
-
name
can hold only a string value. -
name
can hold either a string or nil value. - Optional values cannot be
let
constants. - Only non-empty string variables can be stored in
name
.
Reference: The Swift Programming Language: Language Guide: The Basics: Optionals
let i = 5
let val = i * 6.0
- This code is invalid.
- 6
- 30
- 0
Reference: The Swift Programming Language: Language Guide: The Basics: Type Safety and Type Inference
enum Positions: Int {
case first, second, third, other
}
print (Positions.other.rawValue)
- 3
- 0
- other
- nil
Reference: The Swift Programming Language: Language Guide: The Basics: Raw Values
"t".forEach { (char) in
print(char)
}
- nil
- Nothing, since the code contains an error
- t
- zero
References:
- The Swift Programming Language: Language Guide: Strings and Characters: Working with Characters
- Apple Developer: Documentation: Swift: String: forEach(_:)
let s1 = ["1", "2", "3"]
.filter { $0 > "0" }
.sorted { $0 > $1 }
print(s1)
- []
- ["3", "2", "1"]
- [321]
- ["1", "2", "3"]
References:
- Apple Developer: Documentation: Swift: Swift Standard Library: Collections: Sequence and Collection Protocols: Sequence: filter()
- Apple Developer: Documentation: Swift: Array: sorted()
- Associated values
- Integral values
- Raw values
- Custom values
Reference: The Swift Programming Language: Language Guide: Enumerations: Associated Values
class AmP: MMM, AOM { }
- Class
- Protocol
- Enumeration
- Struct
References:
- The Swift Programming Language: Language Guide: Inheritance: Subclassing
- The Swift Programming Language: Language Guide: Protocols: Protocol Syntax
let numbers = [1, 2, 3, 4, 5, 6].filter { $0 % 2 == 0 }
- [1, 3, 5]
- []
- [2, 4, 6]
- nil
let vals = ["a", 1, "Hi"]
- Array(char)
- [Any]
- Array
- [Generic]
Reference: The Swift Programming Language: Language Guide: Type Casting
let vt = (name: "ABC", val: 5)
- let x = vt.1
- All of these answers
- let x = vt.val
- let (
_
, x) = vt
Reference: The Swift Programming Language: Language Guide: The Basics: Tuples
let x = try? String.init(from: decoder)
- String
- String?
- String!
- try?
Reference: The Swift Programming Language: Language Guide: Error Handling: Handling Errors
let loopx = 5
repeat {
print (loopx)
} while loopx < 6
- Six
- Zero
- Five
- Infinite
Reference: The Swift Programming Language: Language Guide: Control Flow: While Loops
var vals: Set<String> = ["4", "5", "6"]
vals.insert("5")
- This code contains an error.
- Eight
- Three
- Four
Reference: The Swift Programming Language: Language Guide: Collection Types: Sets
class LSN: MMM{ }
- MMM
- LSN
- There is no base class.
- This code is invalid.