1 - Fundamentals of Programming Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

The Call Stack forms a portion of the memory allocated to each process. What is meant by a process in this context?

A

A process is an instance of a program being executed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Describe how the Call Stack grows and shrinks in size during the execution of the code.

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

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.

A
  • 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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Why does the Call Stack follow a stack data structure (only access the item on top)?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is a subroutine?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the definition of an array?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is meant by the term Global Variable?

A

Global variables are declared outside of any subroutine and are accessible to all subroutines.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the purpose of the heap?

A

The heap is a pool of unallocated memory used for dynamic memory allocation and storing objects and data with size only known at runtime.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Describe how the heap grows and shrinks in size during execution of a program.

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the difference between an object an an object reference?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What are the similarities and differences between a primitive variable and an object reference variable?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is a primitive variable?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is an object?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

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.
A
  • 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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

In relation to OOP, what does Protected mean?

A

It is an access modifier. The modified code element can only be accessed within the defining class or a class derived from it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is an access modifier?

A

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.

17
Q

What is a constructor?

A

A constructor is a special procedure which is run when an object is first created.

18
Q

The main concept behind OOP is encapsulation. What does this mean?

A

Encapsulation is the idea of combining properties and methods together and being able to restrict access to an object’s data/functions.

19
Q

What is a programming paradigm?

Give some examples of programming paradigms.

A

A general style of writing code.

  • Imperative
  • Declarative
  • Functional
  • Event Driven
20
Q

What is the difference between a class and an object?

A

A class specifies the data fields and methods that each object of that type must have. Whereas an object is a specific instance of a class.

21
Q

What is meant by instantiation?

A

Instantiation is where you create an object from a class.

22
Q

Explain what is meant by overriding when writing programs that involve inheritance.

A

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.

23
Q

What is subtype polymorphism?

A

When there is a method in a derived class which has the same name, but different behaviour, as a method in a base class. The derived class’ method overrides the method in the base class. The correct method is chosen dynamically because method calls are determined by the actual object and not the object reference type.

24
Q

What is an abstract method?

A

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.

25
Q

What is an abstract class?

A

A class which cannot be instantiated. It usually contains at least one abstract method.

26
Q

What are the object oriented design principles?

A
  • 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;
27
Q

What is meant by a base case for a recursive subroutine?

A

The circumstances where a recursive subroutine does not call itself