[go: up one dir, main page]

0% found this document useful (0 votes)
31 views6 pages

2018 Nov Sample Solutions

Electrical Principles sample questions

Uploaded by

milnermarunda718
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views6 pages

2018 Nov Sample Solutions

Electrical Principles sample questions

Uploaded by

milnermarunda718
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

EE103

2018 NOV/DEC PAPER

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

if (a < b) and (a < c):


print("\nSmallest number is", a)
elif (b < a) and (b < c):
print("\nSmallest number is", b)
else:
print("\nSmallest number is", c)
break

e) def divisibility(intList, x):


newList = []

for num in intList:


if (num % x == 0): newList.append(num)

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)

# loop that produces the triangular pattern required


for i in range(6):

line = " " * (5 - i)

for j in range(i + 1):


line += str(binomial(i, j)) + " "

print(line)

c) (i) myList = [x**2 for x in range(200) if (x % 3 == 0) and (x % 4 == 0)]


print(myList)

(ii) myList = [x for x in range(100) if (x % 2 == 0) and (x % 6 != 0)]


print(myList)

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

b) # function that uses the high school method to convert to binary


def binary(dec):
rem = dec % 2
dec = dec // 2

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

print(dec, "->", binary(dec)[::-1])

choice = input("\nGo Again? (Y/N): ")


if choice == 'y' or choice == 'Y':
continue
else:
break

c) class Time():

def __init__(self, hour, minute, second):


self.hour = int(hour)
self.minute = int(minute)
self.second = float(second)

def isNow(self, time):

if (self.hour == time.hour) and (self.minute ==


time.minute) and (self.second == time.second):
return True
else:
return False

def isAfter(self, time):


if (time.hour > self.hour):
return True

elif (time.hour == self.hour) and (time.minute >


self.minute):
return True

elif (time.hour == self.hour) and (time.minute ==


self.minute) and (time.second > self.second):
return True
else:
return False

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.

NameError → raised when a variable or function has an invalid name.

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.

KeyError → when the key specified in a dictionary is not found.

IndexError → when an index specified in a list is not found.

ZeroDivisionError → Produced when you divide by zero (or use % with 0)

b) Check Question 1 (c)

c) def squareroot(n):
lower = 0.0
upper = n

while (upper - lower) > 1e-15:


middle = (lower + upper) / 2

if (middle**2) < n:
lower = middle
else:
upper = middle

middle = (upper + lower) / 2


return 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

b) for i in range(1, 36):


if (i % 10 == 0):
print(i)

c) def avgOfSquares(n):
sumOfSquares = 0

for i in range(1, n + 1):


sumOfSquares += i**2

return float(sumOfSquares / n)

d) def sumOfNumbers(n):

if n == 0:
return 0
else:
return (n + sumOfNumbers(n - 1))

0719380106 | dstanley3247@gmail.com

You might also like