4.1 Fundamentals of programming Flashcards

1
Q

What is a recursive subroutine?

A

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

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

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

2 disadvantages of recursion

A
  1. Can be slower than iterative solutions
  2. Can use more memory than iterative solutions
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is a global variable?

A

A variable that can be accessed from any part of a program

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

What error will occur if a recursive subroutine never meets its base case?

A

Stack overflow

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

3 items stored in a stack frame

A
  • Return address
  • Parameters
  • Local variables
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
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

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

What is meant by iteration?

A

Repeating an instruction

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

What is a subroutine? (2*)

A
  • A named block of code containing a set of instructions designed to performed a frequently used operation
  • It can be called from other parts of the program using its identifier
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What type of iteration is it when the number of repetitions is not required to be known before the loop starts?

A

Indefinite iteration

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

2 advantages of using constants over hard-coded values

A
  1. Constants can be given identifier names, making code easier for a human to understand
  2. When updating a value, constants only need updating at one position in code
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
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
13
Q

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
14
Q

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
15
Q

5 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
16
Q

What is a syntax error? (2*)

A
  • Occurs before program is run
  • Code can’t be compiled
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What is a runtime error? (2*)

A
  • Occurs while the program is running
  • Causes the program to crash
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What is a logic error? (3*)

A
  • Occurs while the program is running
  • Made by the programmer
  • Doesn’t cause the program to crash
19
Q

Suggest a way to fix a logic error?

A

By tracking variable values or dry running using a trace table

20
Q

What is an object reference variable? (2*)

A
  • A fixed length memory address that points to a memory location (in the heap)
  • It is used to refer to an object
21
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

22
Q

How is composition represented in UML?

A

Black diamond

23
Q

How is aggregation represented in UML?

A

White diamond

24
Q

What is encapsulation? (2*)

A
  • The process of combining properties and methods together into an object
  • and being able to restrict access to an object’s state / behaviour
25
What is a constructor?
A method that is called when an object is first created
26
What is the purpose of inheritance?
To create a **hierarchy of specialisation** for classes
27
What is (subtype) polymorphism? What is its purpose? (3*)
- 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
28
2 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
29
2 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
30
What is a constant?
A data item that, once declared, retains the same value for the entire duration of the program run
31
2 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
32
What does the access modifier `protected` mean? Give its UML symbol
* It can be accessed by any subclasses * **and** from within the class itself * `#`
33
What does the access modifier `public` mean? Give its UML symbol
* It can be accessed by any class * `+`
34
What does the access modifier `private` mean? Give its UML symbol
* It can only be accessed from within the class * `-`
35
What is the purpose of a hierarchy chart?
* To represent the structure of the program * By showing which subroutine is called from which subroutine
36
Constructing a hierarchy chart aids with which type of abstraction?
Decomposition (possibly others)
37
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**
38
What are user-defined data types?
Data types that are derived from existing built-in types in order to create a customised data structure
39
3 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
40
2 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
41
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
42
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​.
43
Name 3 OOP design principles
* Encapsulate what varies * Favour composition over inheritance * Program to interfaces, not implementation