1 - Fundamentals of Programming Flashcards
The Call Stack forms a portion of the memory allocated to each process. What is meant by a process in this context?
A process is an instance of a program being executed.
Describe how the Call Stack grows and shrinks in size during the execution of the code.
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 is the difference between an object an an object reference?
An object is stored in the heap and contains the actual data, whereas an object reference contains a fixed-size memory address pointing to an object in the heap. An object reference is often assigned to a variable with an identifier in the code, whereas objects cannot have identifiers in the code.
What are the similarities and differences between a primitive variable and an object reference 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 primitive 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.
What is an object?
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.
What happens to the call stack and the heap when:
- A ‘list’ object is initialised in the program.
- A ‘list’ object is instantiated in the program.
- The object is declared in the code and an object reference for the variable is created and placed in the stack. It is set to null first.
- The object is created in the heap and it’s object reference is set to ‘point’ to its location.
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.