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
What is a TRY CATCH statement
A exception handling statement which allows the program to respond to an exceptional input and instead conduct another method.
What is an identifier?
The unique name given to a variable/ function/ procedure etc.
What is a variable
A memory location that contains one or more data values which could change during runtime
What is a local Variable
A variable that can only be accessed in the subroutine it was declared in and only exists while that subroutine is executing
What is a Global Variable
A variable that can be accessed and used anywhere in the program
What is an Exception
An error that causes the program being executed to stop (crash)
What is a Constant
A memory location that contains a value which does not change during runtime
What is a Subroutine
A named group of statements/instructions that can be called “out of line”
What is a Function
A subroutine that returns exactly one value
What is a Procedure
A subroutine that returns none/ one or more values
What is an Interface
A programming structure that defines a method that must be implemented/ its parameters and its return values
What is A class
A template used to create objects
What is An Object
an instance of a class
What is a Inheritance
The relationship between two object types in which one is a kind of the other and shares some of its properties or methods
What is overriding
When a subclass uses its won implementation of a method rather then one defined by a parent class
What is a Constructor
A method for instantiating an object in a class.
Abstract class
a class that only exists in a model so subclasses can inherit from it
What is a static class
a static class cannot be instantiated.
What is Private
Only code within the class can access this variable or method
What is Public
Code that can be accessed by any class
What is Protected
Only code within the class and derived class can access it
Virtual Methods
a virtual method allows for overrides in child classes
Static Method
A method without having to be instantiated
Abstract Method
A method that has no implementation. Only provides the requirement to be written into a child class
What is composition
The relationship between classes linked with instances of another class inside them. This causes these instances to cease to exist once the superclass is deleted. It is a part of relationship.
What is aggregation
The relationship between classes where if one class where to be deleted/ the other class would not cease to exist. It is a has a relationship.
What is a procedure
a named block of code that performs a specific task/ but does not return
What is a function
a named block of code that has the purpose of returning a value (called the ‘return value’).
Parameter
It is a placeholder for any value that a subroutine uses when it is called.
Interfaces
A subroutine interface is the method by which data is passed into and out of a subroutine through parameters and return values.
Reference
When an argument is passed to a subroutine by reference/ this creates a pointer which contains the memory address of the original variable. Therefore/ any change to the parameter within the subroutine also affects the value of the original variable.
By value
When an argument is passed to a subroutine by value/ the parameter created for the subroutine is a copy of the original argument.
Global Variable
A global variable can be accessed anywhere in a program.
Advantages subroutines
Easy to test/ easier to read/ can be reused easily.
4 reasosns why it is bad practice to use global variables
Debugging is difficult/ accidental changes are common/ they need to be preserved in memory until the program has stopped running.
What is exception handling
the way that a program handles exceptional circumstances. Without it the program would crash
Types of test data
normal boundary erroneous
What is normal test data
Values that you would except to be entered
What is erroneous data?
Values that should be rejected when they are entered
what is boundary test data
values that are just inside the boundary that should be accepted/ and values just outside the boundary that should not be
What is a text file
All data is stored as strings / ASCII values
What is a binary file
All data is stored using different data types.
What is a recursively defined procedure
A procedure that is defined in terms of itself
What is a base case
The base case is a stopping condition
What is a general case
General case is the statement in which the subroutine calls itself.