wayground logo

Free Printable Worksheets

NEW

Font size

S
M
L
XL
Worksheets

Swift Property Observers

Total questions: 24

Worksheet time: 12mins

Name
Class
Date
1.

This is an exit ticket on Swift Property Observers. I copied this material from the Apple site that gives the details of subjects in Swift coding. Here is a link to the document I created for presentation to my students and then to have them complete this exit ticket. I always allow my students to have access to this document as they complete this exit ticket. Here is the link: https://docs.google.com/document/d/1BJ6DaYocUzVsvmJBm7WDWZi5jwCRRPdb/edit?usp=sharing&ouid=102362467938526371992&rtpof=true&sd=true

a)

I get it.

b)

I don't get it.

2.

What is the purpose of property observers?

a)

a) To calculate the value of a property

b)

b) To monitor changes to a property’s value and respond with custom actions

c)

c) To initialize stored properties during instance creation

d)

d) To enable lazy initialization of properties

3.

What is the primary difference between stored properties and computed properties?

a)

a) Stored properties calculate their value, while computed properties store it.

b)

b) Stored properties store a value, while computed properties calculate it.

c)

c) Both stored and computed properties are only provided by classes.

d)

d) Stored properties and computed properties are always constant.

4.

Which of the following can define stored properties?

a)

a) Classes only

b)
  • b) Classes and structures only

c)

c) Classes, structures, and enumerations

d)

d) Enumerations only

5.

On which types of properties can property observers be added?

a)

A. Only stored properties defined by the user.

b)

B. Only computed properties.

c)

C. Both stored and computed properties.

d)

D. Only inherited properties in a subclass.

6.

When are property observers called?

a)

A. Only when a new value is different from the current value.

b)

B. Every time a property’s value is set, even if the new value is the same.

c)

C. Only when a property is initialized.

d)

D. Only for computed properties.

7.

Which types of properties can have property observers?

a)

A. Only computed properties.

b)

B. Only stored properties that you define.

c)

C. Stored properties you define, stored properties you inherit, and computed properties you inherit.

d)

D. Only inherited properties.

8.

How do you add a property observer to an inherited property?

a)

A. By defining a getter and setter for the property.

b)

B. By marking the property with @ObservedObject.

c)

C. By overriding the property in a subclass.

d)

D. By using a property wrapper.

9.

What does the willSet property observer do?

a)

A. It is called immediately after a value is stored.

b)

B. It prevents a property’s value from being changed.

c)

C. It is called just before a new value is stored.

d)

D. It validates the new value before storage.

10.

What is the default parameter name for the new value in a willSet observer?

a)

A. oldValue

b)

B. newValue

c)

C. defaultValue

d)

D. newParameter

11.

What happens if you assign a value to a property within its own didSet observer?

a)

A. The property’s value reverts to the old value.

b)

B. The new value replaces the one just set.

c)

C. The didSet observer is bypassed.

d)

D. A runtime error occurs.

12.

When are willSet and didSet observers of superclass properties called during subclass initialization?

a)

A. Before the superclass initializer is called.

b)

B. After the superclass initializer is called.

c)

C. Before any property is set.

d)

D. They are never called during initialization.

13.

In the provided StepCounter example, what happens when stepCounter.totalSteps is set to 200?

a)

Only the didSet observer is triggered.

b)

Only the willSet observer is triggered.

c)

Both willSet and didSet observers are triggered.

d)

Neither observer is triggered.

14.

Which statement about property observers and in-out parameters is correct?

a)

Observers are bypassed when a property is used as an in-out parameter.

b)

Observers are always called when a property is passed as an in-out parameter.

c)

Only didSet is called for in-out parameters.

d)

Property observers don’t work with in-out parameters.

15.

What does the didSet observer receive by default if no parameter name is provided?

a)

newValue

b)

currentValue

c)

oldValue

d)

previousValue

16.

What is the primary use case for willSet and didSet observers?

a)

To dynamically calculate a property’s value.

b)

To execute custom actions before and after a property value is changed.

c)

To enforce immutability of a property.

d)

To define computed properties.

17.

Why can’t a computed property have a willSet or didSet observer?

a)

Because computed properties do not store values.

b)

Because observers are not compatible with getters.

c)

Because computed properties can’t inherit.

d)

Because computed properties are immutable.

18.

What must you do to observe a value change in a computed property?

a)

Override the getter method.

b)

Use a didSet observer.

c)

Use the property’s setter.

d)

Use a stored property.

19.

Which of the following is true about willSet and didSet in relation to initializers?

a)
  • They are always called during property initialization.

b)
  • They are never called during property initialization.

c)

They are only called after the superclass initializer in a subclass.

d)

They are called before and after every property assignment in an initializer.

20.

In the StepCounter example, what role does oldValue play in the didSet observer?

a)

It is used to track the previous value of the property.

b)

It calculates the new value for the property.

c)

It triggers a custom action before the value is updated.

d)

It prevents the value from being reset.

21.

What is the purpose of the willSet observer in the StepCounter example?

a)

To ensure the new value is valid before storage.

b)

To print a message before the property is updated.

c)

To prevent the property from being updated.

d)

To log the old value of the property.

22.

What happens if a property with observers is passed as an in-out parameter?

a)

Observers are not triggered during the function call.

b)

Observers are triggered due to the copy-in copy-out memory model.

c)

Only didSet is called after the function ends.

d)

Observers are bypassed entirely.

23.

Can property observers be applied to type properties?

a)

Yes, but only willSet can be used.

b)
  • Yes, both willSet and didSet can be used.

c)

No, property observers are only for instance properties.

d)

No, type properties use static methods instead.

24.

What is the default name of the parameter passed to the willSet observer if no name is specified?

a)

oldValue

b)

newValue

c)

updatedValue

d)

futureValue