4.1 Fundamentals of Programming Flashcards
What is an array
an ordered, finite set of elements of a single type.
What is selection
Where the program executes different actions depending on the result of the comparison.
Difference between definite and indefinite iteration
Definite iteration - The number of iterations is known before execution
Indefinite iteration - Number of iterations depends on a logical condition
What is nested selection
Nested selection is when there is more than one expression to be tested
What is nested iteration
iterating over elements of one or more iterable objects within another iteration loop.
What is the symbol for integer division and for real divison
real division - /
integer division - //
What is modulus and what is the symbol for it?
Modulus calculates the remainder from an integer division
Symbol for modulus is %
What is truncation and how is it achieved in python
Truncation is the process of limiting the number of digits after the decimal point.
In order to perform it in python you need to import math library and use the math.trunc(number) function
What is rounding and how is it achieved in python
Rounding replaces a number with an approximate value using fewer digits.
python has an inbuilt round() function
round(number, nr of dp)
How do we get a substring of a larger string
Characters and phrases can be extracted from a string based on their position using [] in Python
[:3] Extracts the 0th ,1st and 2nd character from the string.
What is concatenation and how is it achieved
Concatenation is the joining together of strings.
string_3 = string_1 + string_2
How do we go from:
character → character code
* character code → character
chr() converts ASCII code to character
ord() does the opposite
How do we generate a random number in python
import random
number =
random.randint(range excluded)
What is exception handling
A technique used to catch and manage runtime errors in a program.
What is subroutine and what are its uses
a set of instructions designed to perform a frequently used operation within a program.
What are the advantages of using subroutines in programs (2)
The same code only has to be written once to be re-used. making it more efficient.
Its also easier to maintain and fix the code
What is an interface
An interface defines a contract for classes that implement it. It specifies a set of methods (functions) that the implementing classes must provide.
What are local variables
Variables that are declared and used in a subroutine, only in existence while the subroutine is being executed.
How is a stack frame used in the context of subroutines
An area of the stack is allocated data to store during a subroutine call, ( return addresses, parameters, local variables)
What is a function
A subroutine consisting of a series of instructions to perform a task
What is recursion
A function that calls itself until a base case is met.
What are attributes and what are methods
Attributes - Characteristics of an object
Methods - Functions that belong to an object
What is a class
Template used to create objects
Describes the shared attributes and methods of the objects to be created.
what is a object
Once instance of a class, representative of a real world object
What is instantiation
Creating an instance of a class
What is encapsulation
Protection of attributes and methods of an object so they can’t be accessed or altered by other objects
What is inheritance and what is the class diagram symbol for it
A new class is created that retains the attributes and methods from the parent class.
Symbol is a arrow with a clear tip
“is a” relationship
What is aggregation and what is the class diagram symbol for it
Is where an object is created that can contain other objects.
If original object is deleted composed objects continue to exist
Class diagram symbol - empty diamond arrow
What is composition and the class diagram symbol for it
is where an object that is created can be composed of other objects.
If the original objects is deleted so are the composed objects.
Class diagram symbol - Filled in diamond arrow
What is polymorphism
Where different objects in a class can be processed differently
while making use of the same method.
What is overriding
way of achieving polymorphism:
A subclass can have a different execution for a method compared with the same method of the base class.
What are the three oop design principles
encapsulate what varies
* favour composition over inheritance
* program to interfaces, not implementation.
Elaborate on the phrase “encapsulate what varies”
when designing classes and objects, we should encapsulate the parts of the code that are likely to change.
By doing so, we can localize the effects of change, making the code easier to maintain, modify, and extend.
What are the different types of association in OOP?
Inheritance
Aggregation
Composition
Association
Elaborate on the point “favor composition over inheritance”
Favoring composition mitigates the risks of the fragile base class problem,
leading to more modular, maintainable, and adaptable object-oriented designs.
Explain the point “ Program to interfaces not implementation
Instead of relying on specific implementations of objects,
focus on defining interfaces that describe the behaviour or capabilities that objects should have.
What are abstract methods
A method that is declared but not implemented in a class.
It serves as a placeholder for functionality that must be provided by subclasses.
What are static methods
Static methods in Python are methods that are bound to the class rather than to an instance of the class.
What are virtual methods
Methods that can be overridden ina subclass
What is meant by public specifier
Public members are accessible from any part of the program
What is meant by protected specifiers
Protected members are accessible within the class itself and by its subclasses
What is meant by private specifiers
Private members are accessible only within the class in which they are declared.
What is a paradigm
a fundamental model that guides how we approach and solve problems
Why is the OOP paradigm used (4)
Programs are written as a series of modules making them easy to modify and maintain should a fault occur.
Data can be hidden within the class that accesses it providing greater system security
Through inheritance code can easily be reused saving memory.
Classes are only concerned with the data defined within it, so it is unlikely to access other data accidentally.
What is instantiation
Creating an instance of a class
Dog1 = Dog()
How do we alter the name of An instance of a class item named “Item 1”
Item1.name = “new name”
How do we change a string to make it all upper case.
String1 = “aaa”
String1 = String1.upper()
What is a procedure
A procedure is a block of code that performs a specific task and may or may not return a value
What are identifiers
Symbolic names used for any variable, function or definition in a computer program
What does the DIV operator do
The div operator performs integer division if both operands are integers.
It gives the result of the calculation and the remainder
5 DIV 2 = 2R1 ( 2 remainder 1)
How do we perform exponentiation
X**n
X is multiplied repeatedly n times
Difference between functions and procedures
Function always returns a value
Procedure may not return a value
What is a constructor method and how do we define it in python
def __init__():
initialize newly created objects of a class.
The __init__ method is called automatically when an object is instantiated
What is the role of the self parameter
self parameter in Python refers to the instance of the class itself.
Define a class for a dog with a bark method
class dog:
def bark(self):
print(“bark”)
Within the class dog i have a bark function
How do i make use of the bark function for my object
dog1 = dog()
dog1.bark
class customer:
def__init__(self, name, email)
What is missing from the code and why must it be there
self.name = name
self.email = email
Assigns a name and email for each instance of a class, the self represents whichever instance of the class we are dealing with.
what function allows us to add a item to the end of a list
ListName.append(“item”)
If we want the dog class to inherit the methods and attributes of the pet class what words must be added to the code below
class dog():
pet must be put as a parameter
class dog(pet):
what does super() do in python
Reference the super class (class we inherit from)
way of specifically inheriting only certain methods from a parent class
how do we convert integer to binary in python
using the inbuilt bin() function
for example bin(9) returns:
1001
How do we convert a number into hexadecimal string
Using the inbuilt hex() function
hex(15)
output f
How do we return the absolute value of a number
abs()
abs(-19.345)
output 19.345
which function can we use to simulate mod division
divmod()
Returns:
(quotient, remainder)
divmod(30,4)
(7,2)
What function can be used to find the unique identifier of variables
id()
how do we find the maximum and minimum values of an iterable variable
max()
min()
How do we print the reverse of a string
named string
reverse_string = string[::-1]
a[start:stop
round num to 2 dp
num = 9.76515
RoundedNumber = round(num,2)
uses inbuilt function round()
Advantage of static structure and one advantage of dynamic data structure
Static - Memory addresses will be allocated at compile time
data is fixed and contiguous allowing faster access,
Dynamic - memory requirements can change as a program is running
, memory is only used when required
what are the 3 types of queues
Linear queue
Circular queue
Priority queue
what is the linear queue
Linear queue - FIFO data structure organised as a line of data. Has a start and end pointer
what is a circular queue
FIFO data structure where the first and end pointers wrap around the array.
what is a priority queue
same as linear except each element in queue has a priority
How do we handle exceptions in Python?
Using try and except:
What is the difference between a parameter and an argument?
A parameter is a variable in the function definition.
An argument is the actual value passed to the function.
What are the main programming paradigms?
Procedural Programming (structured approach)
Object-Oriented Programming (using classes and objects)
What is procedural programming
A programming paradigm based on the concept of procedures (functions) that manipulate data.
what is meant by contiguous
when data is stored together one element after another
what is the difference between lists and arrays
lists - can hold multiple data types, not stored contiguously
array - can hold one type of data, stored contiguously, use less memory
what is meant by data type
data we use in programs can come in various shapes and sizes these different forms of data are refered to as data types.
what is association
“has a “ relationship in oop
what is meant by modular code
A technique of code design where a solution is broken down into a number of small self-contained and manageable chunks’
what are the advantages of writing code in a “modular way” (3)
enables different programmers to work on different parts of the code
code is easier to read and debug
reduces need for duplicated code
what are global variables
a variable that is declared outside of any function or subroutine and is accessible throughout the entire program.
In object orientated programming what is the purpose of ‘GET’ and ‘SET’ methods?
Allows you to access and change private attributes of an object
Why can an interface not declare a constructor method
An interface cannot declare a constructor because all of the code found within it is abstract. You need a concrete implementation of the interface by another class.
What does this line of object orientated pseudo-code do? Class teacher(Person)
The new subclass teacher ‘inherits’ or is derived from the parent/super class person which is placed in brackets
why can an interface not have attributes for object state.
An interface cannot have attributes for object state because it simply states what methods need to be implemented, not how they are implemented or what data they may use.
What data type should be used to store the value of a 4 digit pin number? and why
Unless numbers are going to be used for arithmetic, they should always be stored as strings. In the case of a 4 digit pin number, if it started with a zero and was stored as an integer, the leading zeros would be lost.
what is the base class
the highest class that doesn’t inherit from any other classes
procedural language’?
any high level language in which program statements can be grouped in self-contained blocks called procedures and functions.
What is meant by parameter passing
passing values into a sub-routine when it is called.
What does a subroutine interface refer to?
It defines the rules for calling the subroutine, including the number and types of parameters, the return value, and any constraints.
What is the fragile base class problem
is a software design issue that occurs when a base class is modified in a way that unintentionally breaks derived (subclass) behavior
Disadvantages of static data structures
memory is reserved even when it is not used
Disadvnatage of dynamic data structures
memory locations used may be fragmented so they may be slower to access
what is meant by object state
the state of an object refers to its data at a given time
The values stored in its attributes.