4.1 Fundamentals of programming Flashcards
What is a recursive subroutine?
One that calls itself (or is defined in terms of itself)
3 advantages of recursion
- Can lead to very elegant, short code
- People think recursively (sometimes)
- It is sometimes the only way to solve a problem
2 disadvantages of recursion
- Can be slower than iterative solutions
- Can use more memory than iterative solutions
What is a global variable?
A variable that can be accessed from any part of a program
What error will occur if a recursive subroutine never meets its base case?
Stack overflow
3 items stored in a stack frame
- Return address
- Parameters
- Local variables
What is meant by a ‘base case’?
The terminating situation in a recursive procedure that does not use recursion to produce a result
What is meant by iteration?
Repeating an instruction
What is a subroutine? (2*)
- 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
What type of iteration is it when the number of repetitions is not required to be known before the loop starts?
Indefinite iteration
2 advantages of using constants over hard-coded values
- Constants can be given identifier names, making code easier for a human to understand
- When updating a value, constants only need updating at one position in code
What is the advantage of favouring composition over inheritance?
It makes it easier to test each class using unit testing
Similarity between composition and aggregation
Both “has a” relationships (ownership)
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)
5 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 a syntax error? (2*)
- Occurs before program is run
- Code can’t be compiled
What is a runtime error? (2*)
- Occurs while the program is running
- Causes the program to crash
What is a logic error? (3*)
- Occurs while the program is running
- Made by the programmer
- Doesn’t cause the program to crash
Suggest a way to fix a logic error?
By tracking variable values or dry running using a trace table
What is an object reference variable? (2*)
- 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? (2*)
- 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? (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
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
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
What is a constant?
A data item that, once declared, retains the same value for the entire duration of the program run
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
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
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
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
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 3 OOP design principles
- Encapsulate what varies
- Favour composition over inheritance
- Program to interfaces, not implementation