1 - Fundamentals of Programming 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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
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. Allows changes to be made easily when required.
  • 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 and allows unrelated classes to make use of similar methods.
27
Q

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

A

The circumstances where a recursive subroutine does not call itself

28
Q

Describe one method of avoiding hard-coding values that make code easier to maintain.

A

Using a constant which would be declared once at the top of the program and could be changed in that single place.

29
Q

Explain why hard-coded values makes the code more prone to errors and harder to understand.

A

It is possible to miss out values, update the wrong values, or mistype values. Constants have an identifier making a value easier to understand.

30
Q

Describe why it is important to always use exception handling when dealing with files.

A

File handling can always generate exceptions because files could be locked, removed, unavailable or inaccessible.

31
Q

Give an example of a situation (not file handling) where exception handling is useful and explain why.

A

Converting an inputted string to an Integer because if it fails you want to catch the error and ask the user to input again.

32
Q

Why do many programmers follow the design principle “favour composition over inheritance”?

A
  • With composition each class can be tested separately but it is not possible to test a subclass independently from the base class
  • There can be unintended side-effects for derived classes if a method in the base class is altered
  • Composition is more flexible as if new class is developed it can easily be used instead of the class that currently is used in the composition
33
Q

Describe the relationships aggregation and composition, making it clear what the difference between the two is.

A

Composition and aggregation are both “has a” relationships when an object contains another object. With composition if the containing object is destroyed so are the objects in contains, this is not the case with association.

34
Q

What is meant by inheritance?

A

Inheritance is a relationship between two object types in which one object type is a kind of the other. The subclass can access properties and methods of the parent class that it is derived from.

35
Q

Why is a stack necessary in order to execute a recursive subroutine?

A

The values of the return address, local variables and parameters need to be stored for each subroutine call to enable a previous execution of the subroutine to be resumed.

36
Q

What is the purpose of a constructor?

A

It prepares the object for use, often accepting arguments that the constructor uses to set the values of attributes.

37
Q

Why should subroutines, ideally, not use global variables?

A

• Easier reuse of routines in other programs
• Routine can be included in a library
• Helps to make the program code more understandable
• Ensures that the routine is self-contained
• (Global variables use memory while a program is running) but local
variables use memory for only part of the time a program is running
• Reduces possibility of undesirable side effects
• Using global variables makes a program harder to debug

38
Q

What features of well-written program code make it understandable without the need to include lots of comments?

A
  • Meaningful identifiers
  • Indentation
  • Subroutines with interfaces using parameters to pass values
  • Subroutines should execute a single task
  • Appropriate use of structures statements
  • Avoid use of goto statements
  • Use of named constants
39
Q

Why is the approach of making attributes private or protected and making creating methods to return their value favoured in object oriented programming?

A

It allows data to be modified in a controlled way.

40
Q

Why may a binary file be chosen over a text file for storing data for a game?

A
  • The binary file cannot be easily read by a person so the game data is hidden more from the user
  • No need for data type conversion routines
  • File size likely to be smaller as not all the stored data is text
41
Q

What is meant by exception handling?

A

Exception handling is responding to the occurrence of runtime errors.