Set 5 Flashcards
What is the advantage of favouring composition over inheritance?
It makes it easier to test each class using unit testing
What is the similarity between composition and aggregation?
Both “has a” relationships (ownership)
What is the difference between composition and aggregation?
- 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)
Give five advantages of using subroutines:
- 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
What is an object reference variable?
- A fixed length memory address that points to a memory location (in the heap)
- It is used to refer to an object
What is the purpose of a class?
To define the method and property fields that capture the common behaviours and characteristics of objects
How is composition represented in UML?
Black diamond
How is aggregation represented in UML?
White diamond
What is encapsulation?
- The process of combining properties and methods together into an object
- and being able to restrict access to an object’s state / behaviour
What is a constructor?
A method that is called when an object is first created
What is the purpose of inheritance?
To create a hierarchy of specialisation for classes
What is (subtype) polymorphism? What is its purpose?
- 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
Give two advantages of “programming to interfaces, not implementation”:
- Allows you to test each part of your program separately
- Allows you to modify your code easily in the future
Give two advantages of local variables over global variables:
- You can give local variables the same name in different subroutines because they are only recognised by the subroutine they are declared in
- Local variables are deleted as soon as the subroutine is over and release the memory space which it occupies
What is a constant?
A data item that, once declared, retains the same value for the entire duration of the program run
Give two advantages of using constants over local variables:
- 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
What does the access modifier protected
mean? Give its UML symbol.
- It can be accessed by any subclasses
- and from within the class itself
#
What does the access modifier public
mean? Give its UML symbol.
- It can be accessed by any class
+
What does the access modifier private
mean? Give its UML symbol.
- It can only be accessed from within the class
-
What is the purpose of a hierarchy chart?
- To represent the structure of the program
- By showing which subroutine is called from which subroutine
Constructing a hierarchy chart aids with which type of abstraction?
Decomposition
(possibly others)
What is meant by a structured programming approach?
- When decomposition of a problem occurs by using block structures (subroutines)
- Structured programming makes use of control structures - sequence / selection / iteration
What are user-defined data types?
Data types that are derived from existing built-in types in order to create a customised data structure
Give three advantages of OOP:
- Classes are highly modular, meaning they are easy to maintain
- Promotes code reuse
- Encapsulation provides a high level of protection for data
Give two drawbacks of OOP:
- 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
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
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.
Name three OOP design principles:
- Encapsulate what varies
- Favour composition over inheritance
- Program to interfaces, not implementation
Steps for binary search on a value X (in English/Pseudocode)
- Look at item in centre of sorted list (or array) (sort first if needed)
- 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 - Check middle of halved list and repeat, until you find X or the sublist cannot be halved any more.
What is the while
condition when coding binary search?
lower<= upper && !found
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
Bubble sort (ascending order) pseudocode on array A
N ← length(A)
swapped ← True
while swapped and N > 1:
__swapped ← False
__for position = 0, 1, ... , N-2:
____if A[position] > A[position+1]:
______swap( A[position] , A[position+1])
______swapped ← True
__N ← N-1
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.
What is a data structure?
The concrete realisation of an abstract data type in code.
Is a stack FIFO or LIFO?
LIFO (Last in first out)
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)
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.
What is a recursive subroutine?
One that calls itself (or is defined in terms of itself)
Give three advantages of recursion:
- Can lead to very elegant, short code
- People think recursively (sometimes)
- It is sometimes the only way to solve a problem
Give two disadvantages of recursion:
- Can be slower than iterative solutions
- Can use more memory than iterative solutions
What is meant by a ‘base case’?
The terminating situation in a recursive procedure that does not use recursion to produce a result
What are the steps for linear search on a list?
- Start at beginning of list
- Compare each item to one you want until
- you find it or
- you reach the end of the list
(use an indefinite while
loop!)
What are the steps for binary search on a value X (in English)?
- Look at item in centre of sorted list (or array) (sort first if needed)
- 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 - Check middle of halved list and repeat, until you find X or the sublist cannot be halved any more.
What is the while
condition when coding binary search?
lower<= upper && !found