0
0
iOS Swiftmobile~20 mins

Structs vs classes in iOS Swift - Practice Questions

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Structs vs Classes Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Difference in memory behavior between struct and class
Consider this Swift code where a struct and a class are assigned to new variables. What will be the output after modifying the new variables?
iOS Swift
struct Point {
  var x: Int
  var y: Int
}

class Location {
  var x: Int
  var y: Int
  init(x: Int, y: Int) {
    self.x = x
    self.y = y
  }
}

var p1 = Point(x: 1, y: 1)
var p2 = p1
p2.x = 5

var l1 = Location(x: 1, y: 1)
var l2 = l1
l2.x = 5

print(p1.x, l1.x)
A5 5
B1 1
C1 5
D5 1
Attempts:
2 left
💡 Hint
Think about how structs and classes are copied or referenced in Swift.
ui_behavior
intermediate
2:00remaining
Effect of mutating method in struct vs class
Given these Swift definitions, what will be printed after calling change() on each instance?
iOS Swift
struct CounterStruct {
  var count = 0
  mutating func change() {
    count += 1
  }
}

class CounterClass {
  var count = 0
  func change() {
    count += 1
  }
}

var cs = CounterStruct()
var cc = CounterClass()
cs.change()
cc.change()
print(cs.count, cc.count)
A0 1
B1 1
C1 0
D0 0
Attempts:
2 left
💡 Hint
Remember that structs need mutating keyword to change properties inside methods.
lifecycle
advanced
1:30remaining
Deinitialization behavior difference
Which statement about deinitializers in Swift structs and classes is true?
ANeither structs nor classes have deinitializers.
BBoth structs and classes can have deinitializers.
CStructs have deinitializers but classes do not.
DOnly classes can have deinitializers; structs cannot.
Attempts:
2 left
💡 Hint
Think about which types manage memory with reference counting.
navigation
advanced
2:00remaining
Passing struct vs class between view controllers
You pass a struct instance and a class instance from one view controller to another. What happens if you modify the struct in the second view controller?
AThe original struct in the first view controller remains unchanged.
BThe original struct in the first view controller is changed.
CBoth struct and class instances are changed in the first view controller.
DNeither struct nor class instances can be modified in the second view controller.
Attempts:
2 left
💡 Hint
Consider how value and reference types behave when passed between screens.
📝 Syntax
expert
2:30remaining
Which code snippet correctly defines a struct with a computed property?
Select the Swift code that correctly defines a struct with a computed property named 'area' that returns width * height.
A
struct Rectangle {
  var width: Int
  var height: Int
  var area: Int {
    return width * height
  }
}
B
struct Rectangle {
  var width: Int
  var height: Int
  var area {
    return width * height
  }
}
C
struct Rectangle {
  var width: Int
  var height: Int
  var area: Int() {
    return width * height
  }
}
D
struct Rectangle {
  var width: Int
  var height: Int
  func area() -> Int {
    width * height
  }
}
Attempts:
2 left
💡 Hint
Computed properties use var with a type and a getter block.