Object-Oriented Programming in Swift Flashcards

1
Q

What is another word for an object?

A

Class instances

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

What are objects?

A

Self-contained modules of functionality that can be easily used and re-used as the building blocks of a software application.

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

Instances consist of 1. _____ _____ called _____ and 2. _____ called ______. They can be accessed and called on the instance to perform tasks AND are collectively referred to as 3. _______ _______.

A
  1. Data variables, properties
  2. Functions, methods
  3. Class members
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What does a class do? Specify

A

Defines what an instance will look like when it’s created. What methods will do and what properties will be.

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

What does the “properties” section of a class do?

A

Defines the variables and constants to be contained in the class.

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

What is the idea behind “data encapsulation”?

A

Data should be stored within classes and accessed only through methods defined in that class.

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

Data encapsulated in a class is referred to as _______ and _____

A

Properties, Instance variables

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

What’s the difference between a type method and an instance method?

A
  1. A type method operates at the level of a class, such as creating a new instance of a class.
  2. An instance method operates only on an instance of a class such as performing an arithmetic operation on two properties and returning the result.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Where and how is an instance method declared?

A

Within the opening and closing braces of the class to which it belongs using the standard Swift function declaration syntax.

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

How is a type method declared?

A

By preceding the declaration with the “class” keyword.

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

syntax to create an instance of a class

A

var instanceName: ClassName = ClassName( )

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

What does dot notation do?

A

Calls instance methods and accesses properties built into a class.

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

Syntax to access an instance variable or call an instance method?

A

classInstanceName.propertyName
Or
classInstanceName.instanceMethod( )

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

What are the two class property categories?

A

“Stored properties” and “computed properties”

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

What is a “computed property”?

A

A property that’s derived based on some form of calculation or logic at the point at which the property is set or retrieved.

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

How are “computed properties” implemented?

A

By creating “getter” and optional “setter” methods containing the code to perform the computation.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q
  1. What’s unique about a property being declared as “lazy”?

2. How is this advantageous?

A
  1. It will only be initialized when it’s first accessed.

2. It allows resource intensive activities to be deferred until the property is needed.

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

Lazy properties must be declared as ______.

A

variables (var)

19
Q

Why prefix references to properties and methods with the self property?

A

To indicate that the method or property belongs to the current class instance.

20
Q

Why is it no longer necessary to use self in most situations?

A

It’s now assumed to be the default for references to properties and methods.

21
Q

What are two situations where it is still necessary to use the “self” property?

A
  1. When referencing a property or method from within a closure expression.
  2. When a function parameter has the same name as a class property.
22
Q

Define protocol

A

A keyword that specifies the methods and properties a class or struct must contain in order to be in conformance.

23
Q

What do “Opaque return types” do?

A

Allow a function to return any type as long as it conforms to a specified protocol.

24
Q

How are “opaque” return types declared?

A

By preceding the protocol name with the “some” keyword.

25
Q

What is a concrete type?

A

A specific return type.

26
Q

How do you create class hierarchy?

A

By deriving classes

27
Q

What is “single inheritance”?

A

The concept that a subclass can only be derived from a single direct parent class.

28
Q

What are the two rules that must be obeyed when overriding a method of a class?

A
  1. The overriding method in the subclass must take exactly the same number and type of parameters as the overridden method in the parent class
  2. The new method must have the same return type as the parent method.
29
Q

To avoid potential initialization problems, the init method of the _______ must always be called after the initialization tasks for the _______ have been completed.

A

superclass, subclass

30
Q
  1. What is an “extension” used for?

2. What sort of features can an extension be used to add?

A
  1. To add new functionality to a class without needing to create a subclass.
  2. To add features such as methods, initializers, computed properties, and subscripts to an existing class without the need to create and reference a subclass.
31
Q

What are 3 things that structures have in common with classes?

A
  1. They can be extended
  2. They’re able to adopt protocols
  3. They contain initializers
32
Q

What happens when a structure instance is copied?

A

The copy has its own version of the data which is unconnected with the original structure instance.

33
Q

What happens when a class instance is copied?

A

The only thing duplicated or passed is a reference to the location in memory where that class instance resides.

34
Q

Unlike classes structures can not contain a _____ method

A

de-initializer (deinit)

35
Q

Why are structures generally recommended whenever possible?

A

They are more efficient and safer to use in multi-threaded code then classes.

36
Q

In what three situations should classes be used over structures?

A
  1. Inheritance is needed
  2. Only one instance of the encapsulated data is required
  3. Extra steps need to be taken to free up resources when an instance is de-initialized.
37
Q
  1. What are enumerations?

2. When are they of particular use?

A
  1. A data type of pre-defined sets of state values.

2. When identifying state within a switch statement.

38
Q

What do “property wrappers” do?

A

They allow the capabilities of computed properties to be separated from individual classes/structures and reused throughout the app code base.

39
Q
  1. How are property wrappers declared?
  2. Where are they implemented?
  3. What is the preferred choice for implementation?
A
  1. Using the @propertyWrapper directive
  2. In a class or struct
  3. Structures
40
Q

What are arrays and dictionaries in Swift?

A

Objects that contain collections of other objects

41
Q

What’s an array?

A

A data type that holds multiple values in a single ordered collection.

42
Q

Syntax to create and initialize a populated collection of array literal values

A

var variableName: [type] = [value 1, value2, value3, …….. ]

43
Q

The following syntax could be used to create an empty array

A

var arrName = [type] ( )