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 mobile devices
C. Windows desktop computers
D. Apple desktop computers

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. macOS
D. watchOS

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

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

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

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

5 Which Swift keyword is used to declare a class?

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

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

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

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

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

8 Which Swift keyword declares a constant?

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

9 What does a Swift array store?

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

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

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

11 What is a method in Swift?

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

12 Which Swift keyword is used to declare a method?

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

13 What is a closure in Swift?

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

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

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

15 How does a Swift dictionary organize its data?

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

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

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

17 Which Swift keyword is used to declare a structure?

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

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

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

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

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

20 Which block is required in a Swift guard statement?

Guard Easy
A. An else block
B. A catch block
C. A finally block
D. A 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?

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

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

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

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. Bark
B. Dog
C. Animal
D. 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?

Object-oriented programming Medium
A. public var balance: Double
B. lazy var balance: Double
C. private var balance: Double
D. static 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; func init(name: String) { self.name = name } }
C. class User { let name: String; init() { name: String = "Sam" } }
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. 1
B. 9
C. 0
D. A compilation error

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 the array elements to integers
B. Add an explicit String type annotation
C. Change append to insertLast
D. Change let items to var items

28 What value is assigned to displayName?

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

Variables Medium
A. nil
B. Optional("Guest")
C. An empty string
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. 20
B. 30
C. 40
D. 10

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. [2, 4]
B. [6, 12]
C. [3, 6, 9, 12]
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(start: 2, end: 5)
B. move(2, 5)
C. move(from: 2, to: 5)
D. 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
}
}

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

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

Closure Medium
A. words.filter { $0.count < $1.count }
B. words.sorted { $0.count < $1.count }
C. words.sorted { $0.count > $1.count }
D. 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())

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

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. 1
D. 3

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

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

Dictionary Medium
A. 1
B. 3
C. 0
D. 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
}
}

Struct Medium
A. override
B. static
C. mutating
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. 25
B. 10
C. 15
D. A compilation error

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 = code:
B. case .success where code:
C. case .success(Int 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. nil, Unknown, Mia
B. Unknown, empty string, Mia
C. Unknown, 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 arch(arm64)\nimport UIKit\n#else\nimport AppKit\n#endif
B. #if canImport(UIKit)\nimport UIKit\n#elseif canImport(AppKit)\nimport AppKit\n#endif
C. #if os(macOS)\nimport UIKit\n#elseif os(iOS)\nimport AppKit\n#endif
D. #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?

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

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. false 1
C. false 9
D. 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())

Object-oriented programming Hard
A. P C
B. C P
C. C C
D. 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?

Declaring and defining classes Hard
A. Child(y: 2)
B. Child(x: 1, y: 2)
C. Child(x: 1)
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 { override init(id: Int) { super.init(id: id) } }
B. class Child: Base { init() { super.init(id: 0) } }
C. class Child: Base { convenience init() { self.init(id: 0) } }
D. 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()

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

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 prints 10 because the slice retains the original storage.
B. It prints 20 because every slice starts at index 0.
C. It traps at runtime because slice.startIndex is 1.
D. It fails to compile because 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))

Arrays Hard
A. The code fails because boxes is immutable.
B. [0, 0, 0]
C. [7, 7, 7]
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 traps only when the second assignment executes at runtime.
B. It increments number once because writes are automatically merged.
C. It increments number twice because parameters alias safely.
D. It is rejected because the two 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)

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 25
B. 25 25
C. 25 10
D. 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())

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

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

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

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

58 Which Swift enum declaration is valid?

Enum Hard
A. enum Token: String { case word; case number }
B. enum Token { case word = "word"; case number }
C. enum Token: String { case word(String); case number }
D. 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?

Enum Hard
A. Add mutating to the recursive case so its storage can be allocated lazily.
B. Mark either the entire enum or the recursive next case as indirect.
C. Mark the stored Int associated value as weak before declaring the case.
D. Change 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}

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