2018 Nov Sample Solutions
2018 Nov Sample Solutions
SAMPLE SOLUTIONS
QUESTION 1
a) Inheritance is when a class, called the Child or Inheriting Class, takes the attributes and methods of
another class, called the Parent class, when it is created. The Child class can then either add other
attributes and methods, or modify the ones it has received, through overloading.
b) (i) 10101
& 10001
10001
(ii) 10101
| 00010
10111
(iii) 10101
^ 01010
11111
0719380106 | dstanley3247@gmail.com
c) Output:
1
0
2
1
d) while True:
try:
a = float(input("Enter first number: "))
b = float(input("Enter second number: "))
c = float(input("Enter third number: "))
except:
print("Entries must be numeric! Please try again.\n")
continue
newList.sort()
return newList
QUESTION 2
a) Encapsulation means wrapping up directly related variables and functions that define a single
entity into one unit. This makes code easier to reuse, maintain, and change or improve. An
example of encapsulation in OOP is the use of classes. They wrap up attributes and methods that
define characteristics and behaviours of objects into one entity.
0719380106 | dstanley3247@gmail.com
b) # function that generates a Pascal number
# r is the row of the number, c its column
def binomial(r, c):
if (c == 0) or (c == r):
return 1
else:
return binomial(r - 1, c - 1) + binomial(r - 1, c)
print(line)
QUESTION 3
a) The main difference between a list and a tuple is that lists are mutable while tuples are immutable.
This means you can modify and remove individual items in a list, but cannot do the same on
individual items in a tuple. To change a single value in a tuple, you have to create a new tuple
entirely. In addition, lists offer methods for editing their values, e.g. sort(), remove(), append(),
etc, which are not present in tuples.
QUESTION 4
if (dec == 0):
return str(rem)
else:
return str(rem) + binary(dec)
0719380106 | dstanley3247@gmail.com
# continuously asking for the user’s input to convert to binary
while True:
try:
dec = int(input("Enter Integer: "))
except ValueError:
print("Invalid Input. Please try again.")
continue
c) class Time():
0719380106 | dstanley3247@gmail.com
QUESTION 5:
a) TypeError → raised when an operation is performed on data types that do not support it, e.g.
adding a string and an integer using the + operator.
ValueError → raised when an object is of the correct type but has an invalid value for the task
being performed, e.g. converting a string “hello” to an int.
IOError → raised when Python encounters an unexpected problem while performing a File
Operation, e.g. being denied access to the file, or failing to find a file path specified.
SyntaxError → raised when the interpreter fails to understand the format of the code while
compiling it.
c) def squareroot(n):
lower = 0.0
upper = n
if (middle**2) < n:
lower = middle
else:
upper = middle
0719380106 | dstanley3247@gmail.com
QUESTION 6:
a) while True:
try:
a = float(input("Enter side A: "))
b = float(input("Enter side B: "))
except:
print("Entries must be numeric. Please try again.\n")
c = (a**2 + b**2)**0.5
print("Hypotenuse:", c)
break
QUESTION 7
c) def avgOfSquares(n):
sumOfSquares = 0
return float(sumOfSquares / n)
d) def sumOfNumbers(n):
if n == 0:
return 0
else:
return (n + sumOfNumbers(n - 1))
0719380106 | dstanley3247@gmail.com