Set 5 Flashcards

You may prefer our related Brainscape-certified flashcards:
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 five advantages of using subroutines:

A
  • 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

Give two advantages of local variables over global variables:

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
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
16
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
17
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
18
Q

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

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

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

A
  • It can only be accessed from within the class
  • -
20
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
21
Q

Constructing a hierarchy chart aids with which type of abstraction?

A

Decomposition
(possibly others)

22
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
23
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

24
Q

Give three 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
25
Q

Give two 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
26
Q

What is meant by favouring composition over inheritance?

A

Using objects composed of other objects rather than a tightly coupled inheritance system as it allows for more flexibility

27
Q

Describe the “encapsulate what varies” design principle

A

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​.

28
Q

Name three OOP design principles:

A
  • Encapsulate what varies
  • Favour composition over inheritance
  • Program to interfaces, not implementation
29
Q

Steps for binary search on a value X (in English/Pseudocode)

A
  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.
30
Q

What is the while condition when coding binary search?

A

lower<= upper && !found

31
Q

What does it mean if you reach a leaf node before you find X in a binary tree search?

A

X is not in the tree

32
Q

Bubble sort (ascending order) pseudocode on array A

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

33
Q

What is an abstract data type?

A

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.

34
Q

What is a data structure?

A

The concrete realisation of an abstract data type in code.

35
Q

Is a stack FIFO or LIFO?

A

LIFO (Last in first out)

36
Q

Give the five core operations of a stack:

A
  • 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)
37
Q

Describe the implementation of a stack

A
  • 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.
38
Q

What is a recursive subroutine?

A

One that calls itself (or is defined in terms of itself)

39
Q

Give three advantages of recursion:

A
  1. Can lead to very elegant, short code
  2. People think recursively (sometimes)
  3. It is sometimes the only way to solve a problem
40
Q

Give two disadvantages of recursion:

A
  1. Can be slower than iterative solutions
  2. Can use more memory than iterative solutions
41
Q

What is meant by a ‘base case’?

A

The terminating situation in a recursive procedure that does not use recursion to produce a result

42
Q

What are the steps for linear search on a list?

A
  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!)

43
Q

What are the steps for binary search on a value X (in English)?

A
  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.
44
Q

What is the while condition when coding binary search?

A

lower<= upper && !found