Unit 1: Introduction to iPhone and iOS Platform with Swift - Practice Quiz
1 Which type of device primarily runs iOS?
2 Which operating system is designed primarily for Apple's Mac computers?
3 In object-oriented programming, what is an object?
4 Which object-oriented programming concept allows a class to acquire features from another class?
5 Which Swift keyword is used to declare a class?
struct
class
protocol
enum
6
Which declaration correctly defines an empty Swift class named Car?
class Car inherits every standard Swift type automatically { }
define Car { }
new Car { }
class Car { }
7 Which Swift keyword declares a variable whose value can change?
import
let
var
func
8 Which Swift keyword declares a constant?
mutable, which permits the assigned value to change at any time
class
let
var
9 What does a Swift array store?
10 What is the index of the first element in a Swift array?
-1
1
0
11 What is a method in Swift?
12 Which Swift keyword is used to declare a method?
func
var
method, followed by a return type and a mandatory message identifier
case
13 What is a closure in Swift?
14 Which symbols commonly surround the body of a Swift closure?
< >
( ) followed by a required class declaration
{ }
[ ]
15 How does a Swift dictionary organize its data?
16
In the dictionary ["name": "Ravi"], what is the key?
["name"]
"name"
["name": "Ravi"]
"Ravi"
17 Which Swift keyword is used to declare a structure?
class
struct
structure, followed by an inherited reference-type declaration
case
18 What is the main purpose of an enumeration in Swift?
19
What is the main purpose of a guard statement in Swift?
20
Which block is required in a Swift guard statement?
else block
catch block
finally block
repeat block that executes the condition at least one time
21 An application shares its data-processing code between iPhone and Mac targets but uses platform-specific user-interface controls. Which framework pairing is appropriate?
22 When adapting an iPhone application for macOS, which interface change is most appropriate?
23
What is printed by the following Swift code?
class Animal {
func sound() -> String { return "Unknown" }
}
class Dog: Animal {
override func sound() -> String { return "Bark" }
}
let pet: Animal = Dog()
print(pet.sound())
Bark
Dog
Animal
Unknown
24
A BankAccount class should allow its methods to change balance while preventing external code from changing it directly. Which declaration best supports encapsulation?
public var balance: Double
lazy var balance: Double
private var balance: Double
static var balance: Double
25
Which class definition correctly initializes the constant property name?
class User { let name: String; init(name: String) { self.name == name } }
class User { let name: String; func init(name: String) { self.name = name } }
class User { let name: String; init() { name: String = "Sam" } }
class User { let name: String; init(name: String) { self.name = name } }
26
What is printed by this code?
class Point {
var x = 0
}
let first = Point()
let second = first
second.x = 9
print(first.x)
1
9
0
27
The statement items.append("C") fails in the following code. Which change fixes it while preserving the intended behavior?
let items = ["A", "B"]
items.append("C")
String type annotation
append to insertLast
let items to var items
28
What value is assigned to displayName?
var nickname: String? = nil
let displayName = nickname ?? "Guest"
nil
Optional("Guest")
Guest
29
What is printed by the following code?
var numbers = [10, 20, 30, 40]
numbers.remove(at: 1)
print(numbers[1])
20
30
40
10
30
What is the value of result?
let values = [1, 2, 3, 4]
let result = values.filter { $0 % 2 == 0 }.map { $0 * 3 }
[2, 4]
[6, 12]
[3, 6, 9, 12]
[3, 9]
31
Given the method declaration below, which call is valid?
func move(from start: Int, to end: Int) { }
move(start: 2, end: 5)
move(2, 5)
move(from: 2, to: 5)
move(from start: 2, to end: 5)
32
Which keyword should replace KEYWORD so that celsius(from:) can be called on Converter without creating an instance?
struct Converter {
KEYWORD func celsius(from fahrenheit: Double) -> Double {
return (fahrenheit - 32) * 5 / 9
}
}
final
mutating
static
override
33
Which expression sorts words from shortest to longest using a closure?
words.filter { $0.count < $1.count }
words.sorted { $0.count < $1.count }
words.sorted { $0.count > $1.count }
words.map { $0.count < $1.count }
34
What is printed by this code?
var count = 1
let calculate = { [count] in count + 1 }
count = 5
print(calculate())
6
2
5
1
35
What is the value associated with "apple" after this code executes?
var counts = ["apple": 2]
counts["apple", default: 0] += 1
2
0
1
3
36
What does scores.count return after the following code executes?
var scores = ["Ana": 90, "Ben": 80]
scores["Ana"] = nil
1
3
0
2
37
Which keyword is required for increase() to modify value in the following structure?
struct Counter {
var value = 0
KEYWORD func increase() {
value += 1
}
}
override
static
mutating
private
38
What is printed by the following code?
struct Size {
var width: Int
}
var original = Size(width: 10)
var copy = original
copy.width = 25
print(original.width)
25
10
15
39
Given the enum and value below, which pattern correctly extracts the status code?
enum NetworkResult {
case success(Int)
case failure(String)
}
let result = NetworkResult.success(200)
case success = code:
case .success where code:
case .success(Int code):
case .success(let code):
40
What are the results of calling username(nil), username(""), and username("Mia"), in that order?
func username(_ input: String?) -> String {
guard let name = input, !name.isEmpty else {
return "Unknown"
}
return name
}
nil, Unknown, Mia
Unknown, empty string, Mia
Unknown, Unknown, Mia
Mia, Unknown, Unknown
41 A Swift source file must compile in both a native iOS target and a native macOS target. Which conditional compilation approach correctly selects the platform-specific user-interface framework?
#if arch(arm64)\nimport UIKit\n#else\nimport AppKit\n#endif
#if canImport(UIKit)\nimport UIKit\n#elseif canImport(AppKit)\nimport AppKit\n#endif
#if os(macOS)\nimport UIKit\n#elseif os(iOS)\nimport AppKit\n#endif
#if swift(>=5.0)\nimport UIKit\n#else\nimport AppKit\n#endif
42 Which statement most accurately describes a default architectural difference between ordinary iOS applications and ordinary macOS applications?
43
Given final class Box { var value: Int; init(_ value: Int) { self.value = value } }, what does the following code print?\n\nlet a = Box(1)\nlet b = a\nb.value = 9\nprint(a === b, a.value)
true 9
false 1
false 9
true 1
44
What is printed by this protocol-extension dispatch example?\n\nprotocol P { }\nextension P { func label() -> String { "P" } }\nfinal class C: P { func label() -> String { "C" } }\nlet concrete = C()\nlet existential: any P = concrete\nprint(concrete.label(), existential.label())
P C
C P
C C
P P
45
Consider these class declarations:\n\nclass Base {\n init(x: Int) { }\n convenience init() { self.init(x: 0) }\n}\nclass Child: Base {\n let y: Int\n init(x: Int, y: Int) {\n self.y = y\n super.init(x: x)\n }\n}\n\nWhich initializer call compiles?
Child(y: 2)
Child(x: 1, y: 2)
Child(x: 1)
Child()
46
Given class Base { required init(id: Int) { } }, which subclass declaration compiles without adding another initializer?
class Child: Base { override init(id: Int) { super.init(id: id) } }
class Child: Base { init() { super.init(id: 0) } }
class Child: Base { convenience init() { self.init(id: 0) } }
class Child: Base { }
47
Given final class Counter { var value = 0 }, which statement about the following declarations is correct?\n\nlet numbers = [1, 2]\nlet counter = Counter()
counter.value += 1 compiles, but numbers.append(3) does not compile.
numbers.append(3) compiles, but counter.value += 1 does not compile.
let.
48
What happens when this code executes?\n\nlet values = [10, 20, 30, 40]\nlet slice = values[1...2]\nprint(slice[0])
10 because the slice retains the original storage.
20 because every slice starts at index 0.
slice.startIndex is 1.
ArraySlice has no integer subscript.
49
What does the following code print?\n\nfinal class Box { var value = 0 }\nlet boxes = Array(repeating: Box(), count: 3)\nboxes[0].value = 7\nprint(boxes.map(\.value))
boxes is immutable.
[0, 0, 0]
[7, 7, 7]
[7, 0, 0]
50
Given the method func bump(_ a: inout Int, _ b: inout Int) { a += 1; b += 1 }, what is the result of calling bump(&number, &number) for one variable number?
number once because writes are automatically merged.
number twice because parameters alias safely.
inout accesses overlap.
51
Why can these methods coexist as pure Swift overloads but conflict when both are exposed to Objective-C with the default selector?\n\nfunc update(_ value: Int)\nfunc update(_ value: String)
update: despite different Swift parameter types.
52
What does this code print?\n\nvar score = 10\nlet snapshot = { [score] in score }\nlet live = { score }\nscore = 25\nprint(snapshot(), live())
10 25
25 25
25 10
10 10
53
What does this code print?\n\nfinal class Owner { var value = 7 }\nvar owner: Owner? = Owner()\nlet read = { [weak owner] in owner?.value ?? -1 }\nowner = nil\nprint(read())
-1
0
7
54
For var values: [String: Int?] = [:], which operation stores the key "x" with an explicit nil value instead of removing the key?
values["x"] = nil
values["x"] = Optional.some(nil)
values.updateValue(nil, forKey: "x")?.none
values.removeValue(forKey: "x")
55
What values are printed?\n\nvar counts = ["a": 1]\nlet missing = counts["b", default: 0]\nlet countAfterRead = counts.count\ncounts["b", default: 0] += 2\nprint(missing, countAfterRead, counts["b"]!)
0 1 0
0 1 2
2 1 2
0 2 2
56
Consider this code in the same module:\n\nstruct Pair {\n let x: Int\n let y: Int\n}\nextension Pair {\n init(repeating value: Int) {\n self.init(x: value, y: value)\n }\n}\n\nWhich statement is correct?
Pair(x: 1, y: 2) compiles because extensions cannot define initializers.
Pair(repeating: 3) compiles because it replaces the memberwise initializer.
let.
Pair(x: 1, y: 2) and Pair(repeating: 3) compile.
57
What does this code print?\n\nfinal class Storage { var number = 1 }\nstruct Wrapper { var storage: Storage }\nvar first = Wrapper(storage: Storage())\nvar second = first\nsecond.storage.number = 8\nprint(first.storage.number, second.storage.number)
8 8
1 8
1 1
8 1
58 Which Swift enum declaration is valid?
enum Token: String { case word; case number }
enum Token { case word = "word"; case number }
enum Token: String { case word(String); case number }
enum Token: Int { case word = 1; case number(Int) }
59
The declaration enum Node { case value(Int); case next(Node) } is rejected because it is recursive. Which statement gives a sufficient correction?
mutating to the recursive case so its storage can be allocated lazily.
next case as indirect.
Int associated value as weak before declaring the case.
Node from an enum to a raw-value enum conforming to Int.
60
What does firstPositive([nil, -1, 3]) return?\n\nfunc firstPositive(_ values: [Int?]) -> Int? {\n for item in values {\n guard let value = item else { continue }\n guard value > 0 else { break }\n return value\n }\n return nil\n}
3
nil
-1
Did this save you a night before the exam?
LPU Notes is free, and it stays free. Ads cover part of the server bill. The rest comes out of a student's own pocket: the domain, the storage, and keeping the site up through the weeks everyone needs it at once.
The payment button didn't load. An ad blocker or a filtered network is the usual reason. to try again.
Nothing here is ever locked, and nothing unlocks. Chip in only if it was worth it. What it pays for →