Unit 1: Introduction to iPhone and iOS Platform with Swift - Practice Quiz

INT372 — Iphone Application Programming 60 Questions
0 Correct 0 Wrong 60 Left
0/60

1 Which type of device primarily runs iOS?

Difference between iOS and MAC OS Easy
A. Linux server computers
B. Apple desktop computers
C. Windows desktop computers
D. Apple mobile devices

2 Which operating system is designed primarily for Apple's Mac computers?

Difference between iOS and MAC OS Easy
A. iOS
B. An iPhone-specific version of iOS used only for desktop application development
C. watchOS
D. macOS

3 In object-oriented programming, what is an object?

Object-oriented programming Easy
A. An instance of a class
B. A list of import statements
C. A loop inside a method
D. A file containing only global constants

4 Which object-oriented programming concept allows a class to acquire features from another class?

Object-oriented programming Easy
A. A process that converts every stored property into a global variable
B. Inheritance
C. Compilation
D. Iteration

5 Which Swift keyword is used to declare a class?

Declaring and defining classes Easy
A. struct
B. protocol
C. enum
D. class

6 Which declaration correctly defines an empty Swift class named Car?

Declaring and defining classes Easy
A. class Car { }
B. new Car { }
C. class Car inherits every standard Swift type automatically { }
D. define Car { }

7 Which Swift keyword declares a variable whose value can change?

Variables Easy
A. func
B. var
C. import
D. let

8 Which Swift keyword declares a constant?

Variables Easy
A. mutable, which permits the assigned value to change at any time
B. class
C. var
D. let

9 What does a Swift array store?

Arrays Easy
A. A single key-value pair
B. An unordered collection in which every value must have a unique key
C. Only class definitions
D. An ordered collection of values

10 What is the index of the first element in a Swift array?

Arrays Easy
A. -1
B. 1
C. 0
D. The total number of elements currently stored in the array

11 What is a method in Swift?

Methods and messages Easy
A. A function associated with a type
B. A collection of key-value pairs
C. A variable that can store only numeric values
D. A fixed list of enum cases

12 Which Swift keyword is used to declare a method?

Methods and messages Easy
A. method, followed by a return type and a mandatory message identifier
B. case
C. func
D. var

13 What is a closure in Swift?

Closure Easy
A. A class that automatically closes the application after completing a task
B. A special type of stored property
C. A collection of unique keys
D. A self-contained block of functionality

14 Which symbols commonly surround the body of a Swift closure?

Closure Easy
A. Angle brackets < >
B. Curly braces { }
C. Parentheses ( ) followed by a required class declaration
D. Square brackets [ ]

15 How does a Swift dictionary organize its data?

Dictionary Easy
A. As an ordered sequence in which duplicate index positions are permitted
B. As indexed values only
C. As class-method pairs
D. As key-value pairs

16 In the dictionary ["name": "Ravi"], what is the key?

Dictionary Easy
A. "Ravi"
B. ["name"]
C. ["name": "Ravi"]
D. "name"

17 Which Swift keyword is used to declare a structure?

Struct Easy
A. struct
B. class
C. case
D. structure, followed by an inherited reference-type declaration

18 What is the main purpose of an enumeration in Swift?

Enum Easy
A. To create only mutable variables
B. To declare a class that must inherit from every case it contains
C. To store values by numeric index
D. To define a group of related cases

19 What is the main purpose of a guard statement in Swift?

Guard Easy
A. To prevent every property of a class from being accessed outside its source file
B. To repeat code while a condition is true
C. To exit early when a condition fails
D. To declare a stored property

20 Which block is required in a Swift guard statement?

Guard Easy
A. A repeat block that executes the condition at least one time
B. An else block
C. A catch block
D. A finally block

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?

Difference between iOS and MAC OS Medium
A. Foundation for iOS and Core Data for macOS
B. Core Graphics for iOS and Foundation for macOS
C. UIKit for iOS and AppKit for macOS
D. AppKit for iOS and UIKit for macOS

22 When adapting an iPhone application for macOS, which interface change is most appropriate?

Difference between iOS and MAC OS Medium
A. Replace all windows with a single full-screen view
B. Restrict every operation to touch gestures
C. Support resizable windows and pointer-based interaction
D. Remove keyboard shortcuts and contextual menus

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())

Object-oriented programming Medium
A. Unknown
B. Bark
C. Animal
D. Dog

24 A BankAccount class should allow its methods to change balance while preventing external code from changing it directly. Which declaration best supports encapsulation?

Object-oriented programming Medium
A. public var balance: Double
B. lazy var balance: Double
C. static var balance: Double
D. private var balance: Double

25 Which class definition correctly initializes the constant property name?

Declaring and defining classes Medium
A. class User { let name: String; init(name: String) { self.name = name } }
B. class User { let name: String; init() { name: String = "Sam" } }
C. class User { let name: String; func init(name: String) { self.name = name } }
D. 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)

Declaring and defining classes Medium
A. 0
B. A compilation error
C. 1
D. 9

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")

Variables Medium
A. Change append to insertLast
B. Change let items to var items
C. Add an explicit String type annotation
D. Change the array elements to integers

28 What value is assigned to displayName?

var nickname: String? = nil
let displayName = nickname ?? "Guest"

Variables Medium
A. Optional("Guest")
B. An empty string
C. nil
D. Guest

29 What is printed by the following code?

var numbers = [10, 20, 30, 40]
numbers.remove(at: 1)
print(numbers[1])

Arrays Medium
A. 40
B. 30
C. 10
D. 20

30 What is the value of result?

let values = [1, 2, 3, 4]
let result = values.filter { $0 % 2 == 0 }.map { $0 * 3 }

Arrays Medium
A. [6, 12]
B. [3, 6, 9, 12]
C. [2, 4]
D. [3, 9]

31 Given the method declaration below, which call is valid?

func move(from start: Int, to end: Int) { }

Methods and messages Medium
A. move(2, 5)
B. move(start: 2, end: 5)
C. move(from start: 2, to end: 5)
D. move(from: 2, to: 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
}
}

Methods and messages Medium
A. mutating
B. final
C. override
D. static

33 Which expression sorts words from shortest to longest using a closure?

Closure Medium
A. words.sorted { $0.count > $1.count }
B. words.map { $0.count < $1.count }
C. words.filter { $0.count < $1.count }
D. words.sorted { $0.count < $1.count }

34 What is printed by this code?

var count = 1
let calculate = { [count] in count + 1 }
count = 5
print(calculate())

Closure Medium
A. 2
B. 5
C. 1
D. 6

35 What is the value associated with "apple" after this code executes?

var counts = ["apple": 2]
counts["apple", default: 0] += 1

Dictionary Medium
A. 2
B. 0
C. 3
D. 1

36 What does scores.count return after the following code executes?

var scores = ["Ana": 90, "Ben": 80]
scores["Ana"] = nil

Dictionary Medium
A. 2
B. 0
C. 3
D. 1

37 Which keyword is required for increase() to modify value in the following structure?

struct Counter {
var value = 0
KEYWORD func increase() {
value += 1
}
}

Struct Medium
A. mutating
B. override
C. static
D. 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)

Struct Medium
A. A compilation error
B. 10
C. 25
D. 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)

Enum Medium
A. case .success(Int code):
B. case success = code:
C. case .success where code:
D. 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
}

Guard Medium
A. Unknown, Unknown, Mia
B. Unknown, empty string, Mia
C. nil, Unknown, Mia
D. 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?

Difference between iOS and MAC OS Hard
A. #if swift(>=5.0)\nimport UIKit\n#else\nimport AppKit\n#endif
B. #if arch(arm64)\nimport UIKit\n#else\nimport AppKit\n#endif
C. #if os(macOS)\nimport UIKit\n#elseif os(iOS)\nimport AppKit\n#endif
D. #if canImport(UIKit)\nimport UIKit\n#elseif canImport(AppKit)\nimport AppKit\n#endif

42 Which statement most accurately describes a default architectural difference between ordinary iOS applications and ordinary macOS applications?

Difference between iOS and MAC OS Hard
A. iOS applications use AppKit for windows, while macOS applications use UIKit for windows.
B. macOS applications cannot create multiple windows, while iOS applications always create several windows.
C. iOS applications are normally sandboxed and lifecycle-managed more restrictively than macOS applications.
D. macOS applications cannot execute background work, while iOS applications can execute it indefinitely.

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)

Object-oriented programming Hard
A. true 9
B. true 1
C. false 1
D. false 9

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())

Object-oriented programming Hard
A. P P
B. C C
C. C P
D. P C

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?

Declaring and defining classes Hard
A. Child(x: 1, y: 2)
B. Child(x: 1)
C. Child(y: 2)
D. Child()

46 Given class Base { required init(id: Int) { } }, which subclass declaration compiles without adding another initializer?

Declaring and defining classes Hard
A. class Child: Base { }
B. class Child: Base { convenience init() { self.init(id: 0) } }
C. class Child: Base { init() { super.init(id: 0) } }
D. class Child: Base { override init(id: Int) { super.init(id: id) } }

47 Given final class Counter { var value = 0 }, which statement about the following declarations is correct?\n\nlet numbers = [1, 2]\nlet counter = Counter()

Variables Hard
A. Neither mutation compiles because both bindings were declared with let.
B. Both mutations compile because the stored values remain at fixed addresses.
C. numbers.append(3) compiles, but counter.value += 1 does not compile.
D. counter.value += 1 compiles, but numbers.append(3) does not compile.

48 What happens when this code executes?\n\nlet values = [10, 20, 30, 40]\nlet slice = values[1...2]\nprint(slice[0])

Arrays Hard
A. It fails to compile because ArraySlice has no integer subscript.
B. It traps at runtime because slice.startIndex is 1.
C. It prints 10 because the slice retains the original storage.
D. It prints 20 because every slice starts at index 0.

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))

Arrays Hard
A. [0, 0, 0]
B. [7, 7, 7]
C. The code fails because boxes is immutable.
D. [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?

Methods and messages Hard
A. It is rejected because the two inout accesses overlap.
B. It increments number twice because parameters alias safely.
C. It traps only when the second assignment executes at runtime.
D. It increments number once because writes are automatically merged.

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)

Methods and messages Hard
A. Objective-C selectors include Swift parameter types but exclude the method base name.
B. Both map to the Objective-C selector update: despite different Swift parameter types.
C. Swift overload resolution ignores parameter types after Objective-C interoperability is enabled.
D. Objective-C requires every exposed method to have a different number of parameters.

52 What does this code print?\n\nvar score = 10\nlet snapshot = { [score] in score }\nlet live = { score }\nscore = 25\nprint(snapshot(), live())

Closure Hard
A. 10 10
B. 25 10
C. 25 25
D. 10 25

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())

Closure Hard
A. 7
B. 0
C. It traps while unwrapping the weak reference.
D. -1

54 For var values: [String: Int?] = [:], which operation stores the key "x" with an explicit nil value instead of removing the key?

Dictionary Hard
A. values.updateValue(nil, forKey: "x")?.none
B. values["x"] = Optional.some(nil)
C. values["x"] = nil
D. 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"]!)

Dictionary Hard
A. 0 1 2
B. 2 1 2
C. 0 2 2
D. 0 1 0

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?

Struct Hard
A. Only Pair(x: 1, y: 2) compiles because extensions cannot define initializers.
B. Both Pair(x: 1, y: 2) and Pair(repeating: 3) compile.
C. Neither call compiles because all stored properties are declared with let.
D. Only Pair(repeating: 3) compiles because it replaces the memberwise initializer.

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)

Struct Hard
A. 8 1
B. 1 8
C. 1 1
D. 8 8

58 Which Swift enum declaration is valid?

Enum Hard
A. enum Token: Int { case word = 1; case number(Int) }
B. enum Token { case word = "word"; case number }
C. enum Token: String { case word; case number }
D. enum Token: String { case word(String); case number }

59 The declaration enum Node { case value(Int); case next(Node) } is rejected because it is recursive. Which statement gives a sufficient correction?

Enum Hard
A. Mark either the entire enum or the recursive next case as indirect.
B. Change Node from an enum to a raw-value enum conforming to Int.
C. Add mutating to the recursive case so its storage can be allocated lazily.
D. Mark the stored Int associated value as weak before declaring the case.

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}

Guard Hard
A. 3
B. -1
C. nil
D. The function does not compile.