Set 5 Flashcards

(42 cards)

1
Q

What is the advantage of favouring composition over inheritance?

A

It makes it easier to test each class using unit testing

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

What is the similarity between composition and aggregation?

A

Both “has a” relationships (ownership)

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

What is the difference between composition and aggregation?

A
  • In composition, if the containing object is destroyed so are the objects it contains
  • This is not the case with aggregation

(composition = strong ownership, aggregation = weak ownership)

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

Give the advantages of using subroutines:
Give 3 points.

A

Any 3 points:
- Code re-use
- Allows for modularisation of the program
- It makes it easier to identify bugs
- It makes it easier to test individual tasks NE easier to test
- It makes it easier for other programmers to interpret and understand your code

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

What is an object reference variable?

A
  • A fixed length memory address that points to a memory location (in the heap)
  • It is used to refer to an object
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the purpose of a class?

A

To define the method and property fields that capture the common behaviours and characteristics of objects

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

How is composition represented in UML?

A

Black diamond

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

How is aggregation represented in UML?

A

White diamond

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

What is encapsulation?

A
  • The process of combining properties and methods together into an object
  • and being able to restrict access to an object’s state / behaviour
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is a constructor?

A

A method that is called when an object is first created

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

What is the purpose of inheritance?

A

To create a hierarchy of specialisation for classes

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

What is (subtype) polymorphism? What is its purpose?

A
  • When a method in a parent class also exists in a derived class
  • The method has the same name and parameter list, but a different implementation
  • It allows objects up and down the inheritance hierarchy chain to respond differently to the use of a common interface
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Give two advantages of “programming to interfaces, not implementation”:

A
  • Allows you to test each part of your program separately
  • Allows you to modify your code easily in the future
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is a constant?

A

A data item that, once declared, retains the same value for the entire duration of the program run

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

Give two advantages of using constants over local variables:

A
  • Makes your code more readable, by clearly indicating that a value should not be changed throughout the program
  • Enhances code safety by preventing accidental modifications to values that shouldn’t change
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What does the access modifier protected mean? Give its UML symbol.

A
  • It can be accessed by any subclasses
  • and from within the class itself
  • #
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What does the access modifier public mean? Give its UML symbol.

A
  • It can be accessed by any class
  • +
18
Q

What does the access modifier private mean? Give its UML symbol.

A
  • It can only be accessed from within the class
  • -
19
Q

What is the purpose of a hierarchy chart?

A
  • To represent the structure of the program
  • By showing which subroutine is called from which subroutine
20
Q

Constructing a hierarchy chart aids with which type of abstraction?

A

Decomposition
(possibly others)

21
Q

What is meant by a structured programming approach?

A
  • When decomposition of a problem occurs by using block structures (subroutines)
  • Structured programming makes use of control structures - sequence / selection / iteration
22
Q

What are user-defined data types?

A

Data types that are derived from existing built-in types in order to create a customised data structure

23
Q

Give advantages of OOP:

A
  • Classes are highly modular, meaning they are easy to maintain
  • Promotes code reuse
  • Encapsulation provides a high level of protection for data
24
Q

Give drawbacks of OOP:

A
  • Can lead to very large and complex systems
  • Objects can take up a relatively large amount of memory, which is an issue if there are hardware restrictions
25
What is meant by favouring composition over inheritance?
Using objects composed of other objects rather than a tightly coupled inheritance system as it allows for more flexibility
26
Describe the “encapsulate what varies” design principle
When designing a program in the OOP, any requirements ​which are likely to change in the future ​should be ​encapsulated in a class ​so that any changes can be​ easily made when required​.
27
Name the OOP design principles:
* Encapsulate what varies * Favour composition over inheritance * Program to interfaces, not implementation
28
Steps for binary search on a value **X** (in English/Pseudocode)
1. Look at item in centre of sorted list (or array) *(sort first if needed)* 2. If it isn’t **X**… 2.1. If it is larger than **X**, you can ignore all of the items above 2.2. If it is smaller than **X**, you can ignore all of the items below 3. Check middle of halved list and repeat, until you find **X** or the sublist cannot be halved any more.
29
What is the `while` condition when coding binary search?
`lower<= upper && !found`
30
What does it mean if you reach a leaf node before you find **X** in a binary tree search?
**X** is not in the tree
31
What is an abstract data type?
A theoretical description of a way of organising a collection of data, with particular features and access restrictions, that is independent of any particular data structure.
32
What is a data structure?
The concrete realisation of an abstract data type in code.
33
Is a stack FIFO or LIFO?
LIFO (Last in first out)
34
Give the five core operations of a stack:
- **Push** - adding to top of stack - **Pop** - removing from top of stack and returning - **Peek** - returns top item *without* removing it - **IsEmpty** - checking if stack is empty - **IsFull** - checking if stack is full (only relevant when stored in a static structure)
35
Describe the implementation of a stack
- Using an array or list to store the items - Initialise a pointer variable that points to the current top item - The pointer is initialised as -1 - The pointer is incremented if an item is pushed, and vice versa - The pointer will be -1 if stack is empty.
36
What is a recursive subroutine?
One that calls itself (or is defined in terms of itself)
37
Give three advantages of recursion:
1. Can lead to very elegant, short code 2. People think recursively (sometimes) 3. It is sometimes the only way to solve a problem
38
Give two disadvantages of recursion:
1. Can be slower than iterative solutions 2. Can use more memory than iterative solutions
39
What is meant by a ‘base case’?
The **terminating situation** in a recursive procedure that does not use recursion to produce a result
40
What are the steps for linear search on a list?
1. Start at beginning of list 2. Compare each item to one you want until - you find it or - you reach the end of the list *(use an indefinite `while` loop!)*
41
What are the steps for binary search on a value **X** (in English)?
1. Look at item in centre of sorted list (or array) *(sort first if needed)* 2. If it isn’t **X**… 2.1. If it is larger than **X**, you can ignore all of the items above 2.2. If it is smaller than **X**, you can ignore all of the items below 3. Check middle of halved list and repeat, until you find **X** or the sublist cannot be halved any more.
42
What is the `while` condition when coding binary search?
`lower<= upper && !found`