Value vs. Reference Types Flashcards

1
Q

let p3 = Point(x: 4, y: 2) → p3 is created with a struct of variables x and y. Can we change the x or y for p3?

A

We cannot change the x and y of p3 despite them being variables because p3 is a constant

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

var p4 = AnotherPoint(x: 3, y: 2) is created with a struct of constants x and y. Can we change the x or y for p4?

A

We can’t change the x or y for p4 either because x and y are constants.
BUT you can assign p4 to a different AnotherPoint by doing p4 = AnotherPoint(x: 43, y: 6)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Why does our function need mutating? ‘

struct Point {
var x: Double
var y: Double

mutating func moveLeft(steps: Double) {
    x -= steps
} }
A

without mutating, this will not work because we have to remember that value types are immutable.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

When dealing with classes, what happens to the anotherRobot.model for the example below?

var someRobot = Robot(model: "T1999")
var anotherRobot = someRobot

someRobot.model = “T2000”

A

Both of these become T2000 because these are both reference types. they are pulling this information from memory

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q
class Robot {
    var model: String
init(model:String) {
    self.model = model
} }

If we create an instance of Robot that is a constant, can we change the model of it? See below

let thirdRobot = Robot(model: "T3000")
thirdRobot.model = "T4000"
A

This does change the model to T4000 because the variable doesn’t actually contain the actual object but a reference of the object in memory.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How does the mutating keyword change the behavior of a struct’s instance method?

A

It indicates that the method will modify the struct’s values allowing the compiler to create a copy of the struct

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

TF: Assigning a value type to a variable means you can change the value assigned to the variable, but not the value itself.

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

TF: When we assign an instance of a reference type to a constant, the thing that’s constant isn’t the object, it’s the reference to the object.

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

TF: We can modify properties in a reference type even when it is assigned to a constant

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

In reference types, final class implements the same behavior as which keyword?

A

Static

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How do we prevent a reference type from being subclassed?

A

Use final keyword

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

TF: Modifying a reference type contained within a value type creates a new copy of the value type

A

False - Since the reference type is directly modified in memory, the value type is not mutated or copied

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How does the class keyword differ from static when creating type methods?

A

Indicated that the method can be overridden in subclasses.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

If we have a class, final class Calculator {}, can I subclass it?

A

No, final classes cannot be changed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

If we have a method in a class, and we want to alter it in a subclass, what do we have to type before changing the method?

A

Override, because you are changing it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

We have a Struct called Robot and a Class called Robot. We assigned a constant let thirdRobot = Robot(model: “T3000”). We then change the mode, thirdRobot.model = “T4000”. Can we do this for structs and classes?

A

Structs → We CANNOT because it is a value type.

Classes → You CAN because reference types allow it.

17
Q

How different is it to write a type method for structs vs classes?

A

For structs, you just type static in front of the function

For classes, you can type static, final OR class in front of the function. If you put class, you can override that function in a subclass. With a static function, you cannot.

18
Q

What is a method inside of another method called?

A

Helper method