Programming for Business: Exam Revision Questions
Exam Revision Questions
The following questions cover similar to the likely content and format of the Exam questions with correct
answers specified as well. You may wish to use this for helping with your preparation for the Exam.
Question One
Design using Pseudocode and implement with Python 3, a set of two classes containing all the features
as exemplified in the lectures (i.e. constructor, accessors, mutators and _str_) function that contain the
following information:
- A class named ShoppingBasket which describes a basket of shopping; it will have a list of
product descriptions (e.g. “Milk”) and an item capacity (e.g. 240) as its attributes (class fields);
- A class named ShoppingTrolley which will contain all of the elements of the ShoppingBasket
but also contain the number of wheels which the trolley has (e.g. 4).
Then, in Python, create an instance of both classes to demonstrate their usage.
Answer One
CLASS ShoppingBasket:
NOTES: Describes a Shopping Basket.
ATTRIBUTES:
product_descriptions (List of Strings)
item_capacity (Integer / Whole Number)
CONSTRUCTOR: __init__
IMPORTS: self (current object), product_descriptions (List of Strings),
item_capacity (Integer / Whole Number)
EXPORTS: Nothing
NOTES: Constructs an instance of the ShoppingBasket
ALGORITHM:
self.product_descriptions <- product_descriptions
self.item_capacity <- item_capacity
ACCESSOR: get_product_descriptions
IMPORTS: self (current object)
EXPORTS: product_descriptions (List of Strings)
NOTES: Provides access to the product_descriptions attribute
ALGORITHM:
RETURN self.product_descriptions
ACCESSOR: get_item_capacity
IMPORTS: self(current object)
EXPORTS: item_capacity (Integer / Whole Number)
NOTES: Provides access to the item_capacity attribute
ALGORITHM:
RETURN self.item_capacity
MUTATOR: set_product_descriptions
IMPORTS: self (current object), product_descriptions (List of Strings)
EXPORTS: Nothing
NOTES: Changes the product_descriptions attribute
ALGORITHM:
self.product_descriptions <- product_descriptions
MUTATOR: set_item_capacity
Page 1 of 6
Programming for Business: Exam Revision Questions
IMPORTS: self (current object), item_capacity (Integer / Whole Number)
EXPORTS: Nothing
NOTES: Changes the item_capacity attribute
ALGORITHM:
self.item_capacity <- item_capacity
FUNCTION: __str__
IMPORTS: self (current object)
EXPORTS: string_rep (String – representation)
NOTES: Returns string representation of object.
ALGORITHM:
string_rep <- self.product_descriptions AS String + “ “ +
self.item_capacity AS String
RETURN string_rep
CLASS ShoppingTrolley INHERITS FROM ShoppingBasket:
NOTES: Describes a Shopping Trolley.
ATTRIBUTES:
number_wheels (Integer / Whole Number)
CONSTRUCTOR: __init__
IMPORTS: self (current object), product_descriptions (List of Strings),
item_capacity (Integer / Whole Number), number_wheels
(Integer / Whole Number)
EXPORTS: Nothing
NOTES: Constructs an instance of the ShoppingTrolley
ALGORITHM:
CALL __init__ FROM super WITH product_descriptions, item_capacity
self.number_wheels <- number_wheels
ACCESSOR: get_number_wheels
IMPORTS: self (current object)
EXPORTS: number_wheels (Integer / Whole Number)
NOTES: Provides access to the number_wheels attribute
ALGORITHM:
RETURN self.number_wheels
MUTATOR: set_number_wheels
IMPORTS: self (current object), number_wheels (Integer / Whole Number)
EXPORTS: Nothing
NOTES: Changes the number_wheels attribute
ALGORITHM:
self.number_wheels <- number_wheels
FUNCTION: __str__
IMPORTS: self (current object)
EXPORTS: string_rep (String – representation)
NOTES: Returns string representation of object.
ALGORITHM:
string_rep <- self.product_descriptions AS String + “ “ +
self.item_capacity AS String + “ “ +
self.number_wheels AS String
RETURN string_rep
class ShoppingBasket():
“”” Describes a Shopping Basket. “””
def __init__(self, product_descriptions, item_capacity):
self.product_descriptions = product_descriptions
self.item_capacity = item_capacity
Page 2 of 6
Programming for Business: Exam Revision Questions
def get_product_descriptions(self):
return(self.product_descriptions)
def get_item_capacity(self):
return(self.item_capacity)
def set_product_descriptions(self, product_descriptions):
self.product_descriptions = product_descriptions
def set_item_capacity(self, item_capacity):
self.item_capacity = item_capacity
def __str__(self):
string_rep = str(self.product_descriptions) + “ “ +\
str(self.item_capacity)
return(string_rep)
class ShoppingTrolley(ShoppingBasket):
“”” Describes a Shopping Trolley. ”””
def __init__(self, product_descriptions, item_capacity, number_wheels):
super().__init__(product_descriptions, item_capacity)
self.number_wheels = number_wheels
def get_number_wheels(self):
return(self.number_wheels)
def set_number_wheels(self, number_wheels):
self.number_wheels = number_wheels
def __str__(self):
string_rep = str(self.product_descriptions) + “ “ +\
str(self.item_capacity) + “ “ + str(self.number_wheels)
return(string_rep)
shopping_trolley = ShoppingTrolley([“Milk”, “Eggs”], 20, 4)
shopping_basket = ShoppingBasket([“Milk”, “Eggs”], 20)
Page 3 of 6
Programming for Business: Exam Revision Questions
Question Two
Write four Python functions as follows, ensuring you follow the conventions of the unit:
- A function that uses a list of numbers and another ‘single’ number which will insert the ‘single’
number into the list of numbers at the particular index element of the list of numbers that is the
same as said ‘single’ number (where it can be presumed that this will not cause an error). The
new list is then returned to the user.
- A function which prints out the contents (i.e. all the files and folders that are inside) of the current
working directory;
- A function which selects an item at ‘double random’ by first shuffling the order of elements in
the input list of numbers and then selects an element at random from the list and returns it;
- A function that returns the result of calculating the equation sin(x) 2 + cos(x)2 with the relevant
data provided as input parameters to the function;
Answer Two
def number_finder(list_numbers, single_number):
“”” Finds a number in a list. “””
list_numbers.insert(single_number, single_number)
return(list_numbers)
import os
def print_cwd():
“”” Prints the current working directory. “””
print(os.listdir(os.getcwd()))
import random
def select_double_random(input_list):
“”” Selects a list at ‘double random’. “””
random.shuffle(input_list)
selected_item = input_list[random.randint(0, len(input_list) - 1)]
# Alternatively, the .choice function could be used.
return(selected_item)
import math
def maths_calculator(x):
“”” Calculates a trigonometric value. “””
result = math.pow(math.sin(x), 2) + math.pow(math.cos(x), 2)
return(result)
Page 4 of 6
Programming for Business: Exam Revision Questions
Question Three
Utisiling the pandas and matplotlib libraries as taught in the unit, achieve the following tasks with the
creation of Python (3) code in the order stated below:
- Read in the sheet named “Example Sheet” from an Excel file named example_file.xlsx into a
DataFrame which should be named example_df;
- Convert a column named price_of_product to what would be the most appropriate data type for
the column (as implied by the name of the column);
- Create and show a scatter plot comparing two columns: cost_of_product and price_of_product
from the DataFrame (utilising its relevant method) where each scatter dot is coloured green;
- Group the data by the unique values in a column named type_of_product and summarise the
remaining columns by generating their average value (assume that this is possible for all
remaining columns based upon their data types);
- Sort the grouped price_of_product values from the largest value first to the smallest value last;
- Write a copy of this grouped DataFrame to a file named output.csv.
Answer Three
import pandas as pd
import matplotlib.pyplot as plt
example_df = pd.read_excel(“example_file.xlsx”,
sheet_name = “Example Sheet”)
example_df[“price_of_product”] = example_df[“price_of_product”]\
.astype(float)
the_ax = example_df.plot(kind = “scatter”, x = “cost_of_product”,
y = “price_of_product, color = “green”)
plt.show()
the_df = the_df.groupby(“type_of_product”).mean()
the_df = the_df.sort_values(“price_of_product”, ascending = False)
the_df.to_csv(“output.csv”)
Page 5 of 6
Programming for Business: Exam Revision Questions
Question Four
Assume you have a list of dictionaries in Python with the following contents and assignment:
the_dict = [{“name”: “Programming Service”, “cost”: 300.00}, {“name”: “Exam Study”, “cost”: 200.00},
{“name”: “Business Consulting”, “cost”: 400.00}]
Write the Python code required to filter the list of dictionaries such that only items (dictionaries) with a
cost value of over 200 remain in the list and then write the codes to write the dictionaries (using the
keys as headers) to a file named output.csv (hence, in the CSV format). Your code should be efficient
in the lines of code required and utilise the methods taught in the unit and be general enough that a
different list of dictionaries of the same format could use it. You should use the csv and json libraries,
alongside Python’s built-in functions to do so (i.e. do not use pandas or similar).
Answer Four
the_dict = list(filter(lambda x: x[“cost”] > 200.00, the_dict))
file_ptr = open(“output.csv”, “w”)
csv_writer = csv.writer(file_ptr)
csv_writer.writerow(list(the_dict[0].keys()))
for each_dict in the_dict:
csv_writer.writerow(list(each_dict.values()))
file_ptr.close()
Page 6 of 6