4.1 Fundamentals of programming Flashcards

You may prefer our related Brainscape-certified 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
Q

What is a constructor?

A

A method that is called when an object is first created

26
Q

What is the purpose of inheritance?

A

To create a hierarchy of specialisation for classes

27
Q

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

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

2 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
29
Q

2 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
30
Q

What is a constant?

A

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

31
Q

2 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
32
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
  • #
33
Q

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

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

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

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

Constructing a hierarchy chart aids with which type of abstraction?

A

Decomposition
(possibly others)

37
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
38
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

39
Q

3 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
40
Q

2 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
41
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

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

43
Q

Name 3 OOP design principles

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