1 - Fundamentals of Programming Flashcards
Describe how the Call Stack grows and shrinks in size during the execution of a program.
When a subroutine is called, the Stack grows as a new Stack Frame is added on top for the new activation. When a subroutine call ends, the Stack shrinks as the relevant Stack Frame is popped off the Stack.
State three pieces of information that are stored for each active subroutine in the call stack. For each one, explain the purpose of storing them in the call stack.
• Parameter values - these are local to the current subroutine call. When the subroutine ends, the values of the parameters are no longer needed.
• Local variable values - same reason as for parameter values.
• Return address - this could be different depending on which line of code the subroutine was called from. Therefore it needs to be stored each time the subroutine is called/activated.
Why does the Call Stack follow a stack data structure (only access the item on top)?
Nested subroutine/procedure calls work in such a way that the most recent one to be called will be the first one to finish/return. This makes it fit perfectly in a LIFO (Last in First out) data structure.
What is a subroutine?
A self-contained block of code which has an identifier. It can be called from other parts of the code. They can have parameters, local variables and can return a value. A call to a subroutine is a statement in it’s own right.
What is the definition of an array?
An array is a fixed-sized collection of data elements, all of the same type, under a single variable name. To access an element, both the array name and that element’s index is required. Indices start at 0.
What is meant by the term Global Variable?
Global variables are declared outside of any subroutine and are accessible to all subroutines.
What is the purpose of the heap?
The heap is a pool of unallocated memory used for dynamic memory allocation and storing objects and data with size only known at runtime.
Describe how the heap grows and shrinks in size during execution of a program.
When an object is created, memory is dynamically allocated for it - the heap grows in size. When all references to an object no longer exist, the memory is freed back to the pool - the heap shrinks.
What are the similarities and differences between a primitive variable and an object reference (pointer) variable?
Similarities:
• They can both be stored in the Call Stack.
• They both have a fixed size.
• They are both variables.
• They both have identifiers in the code.
Differences:
• Primitive variables store data, whereas object reference variables store memory references to actual data in the heap.
What is a variable?
Primitive variables have a fixed size and are stored in the stack (in the stack frame for a subroutine). Space in memory for them is allocated before the code is run.
How are objects stored?
Objects are stored in the heap, and have an object reference which is stored in the stack which “point” to the object’s location in the heap. Objects can vary in size as the program is running so they need memory allocated dynamically.
In relation to OOP, what does Protected mean?
It is an access modifier. The modified code element can only be accessed within the defining class or a class derived from it.
What is an access modifier?
Access modifiers are used to restrict code elements from use by other parts of the code. They can be applied to class, variable, and procedure/function definitions.
What is a constructor?
A constructor is a special procedure which is run when an object is first created.
The main concept behind OOP is encapsulation. What does this mean?
Encapsulation is the idea of combining properties and methods together and being able to restrict access to an object’s data/functions.
What is a programming paradigm?
Give some examples of programming paradigms.
A general style of writing code.
• Imperative
• Declarative
• Functional
• Event Driven
What is the difference between a class and an object?
A class is a template that specifies the data fields and methods. Whereas an object is a specific instance of a class.
What is meant by instantiation?
Instantiation is where you create an object from a class.
Explain what is meant by overriding when writing programs that involve inheritance.
An inherited method can be redefined so that it still has the same name but a different implementation. The redefined method will be used instead of the parent’s method.
What is polymorphism?
When there is a method in a derived class which has the same name, but a different behaviour/implementation.
What is an abstract method?
A method with a name, parameter list and return type but no implementation. It must be overridden in the first non-abstract derived class. Abstract methods can only exist in abstract classes.
What is an abstract class?
A class which cannot be instantiated. It usually contains at least one abstract method.
Why do many programmers follow the design principle “favour
composition over inheritance”
Easier to test each class using unit testing
There can be unintended side-effects for derived classes if a method in
the base class is altered;
Composition is more flexible as if a new class is developed it can easily
be used instead of the class that currently is used in the composition
What are the object oriented design principles?
• Encapsulate what varies - separate (i.e. put it its own class) the parts that may be subject to change from the parts that will stay the same
• Favour composition over inheritance - favour using aggregation or composition to combine existing objects to make new ones, rather than using inheritance
• Program to interfaces, not implementation - allows programming to an abstraction and not an implementation;
What is meant by a base case for a recursive subroutine?
The circumstances where a recursive subroutine does not call itself
What is a structure?
a data structure that consists of a fixed number of variables/ called fields. Every field has an identifier (field name) and a data type.
What is another name for a structure?
record
What is an exception
An unexpected event that occurs during runtime
What is exception handling
refers to how the program deals with and prevents crashes resulting from exceptional circumstances