Classes, Encapsulation and Representation Invariants · Multiple choice · 1 mark
class Point:
"""A point."""
def __init__(self, x: int, y: int) -> None:
self.x = x
self.y = y
pts = [Point(1, 2), Point(3, 4)]Point does not define __eq__. Which expression evaluates to True?
Point(1, 2) in pts
pts.count(Point(3, 4)) == 1
pts.index(pts[1]) == 1
pts == [Point(1, 2), Point(3, 4)]
Answer: (C)
(C). Without __eq__, == (and therefore in, count and list equality) falls back to identity. Only pts[1] itself is the same object as an element of pts.
Inheritance, Abstract Classes and Polymorphism · Predict the output · 2 marks
Write the output of the code below. If the code raises an error, write ERROR and stop.
class Shape:
def __init__(self, name: str) -> None:
print('Shape init', name)
self.name = name
class Polygon(Shape):
def __init__(self, name: str, sides: int) -> None:
print('Polygon init')
super().__init__(name)
self.sides = sides
print('Polygon done')
class Square(Polygon):
def __init__(self, side: int) -> None:
super().__init__('square', 4)
print('Square init')
self.side = side
s = Square(3)
print(s.name, s.sides, s.side)Polygon init
Shape init square
Polygon done
Square init
square 4 3Square.__init__ starts by calling Polygon.__init__, which prints, calls Shape.__init__, and only then finishes. Each print after a super().__init__ call runs after the parent’s body is finished.
Stacks, Queues and Exceptions · Multiple choice · 2 marks
A programmer writes pop for a stack that may hold any values, including None. When the stack is empty, her pop returns None instead of raising EmptyStackError. What is the main problem?
Returning None is slower than raising an exception
A function that returns None cannot be called from a try statement
The caller cannot tell an empty stack from a stack whose top item is None
Python does not allow a method to return None and a non-None value
Answer: (C)
(C). If None can be a legitimate item, the return value None is ambiguous. Raising an exception signals the error through a separate channel that cannot be confused with an item, and cannot be silently ignored.
Linked Lists · Write code · 3 marks
Implement the method below by exchanging the items stored in the nodes (do not relink nodes). In the examples, a LinkedList prints as [1 -> 2 -> 3] (and as [] when empty).
class LinkedList:
def swap_pairs(self) -> None:
"""Swap the items in positions 0 and 1, 2 and 3, and so on. If
there is an odd number of items, the last item stays where it
is.
>>> lst = LinkedList([1, 2, 3, 4, 5])
>>> lst.swap_pairs()
>>> print(lst)
[2 -> 1 -> 4 -> 3 -> 5]
""" curr = self._first
while curr is not None and curr.next is not None:
curr.item, curr.next.item = curr.next.item, curr.item
curr = curr.next.nextRunning Time and Big-Oh · Multiple choice · 2 marks
What is the running time of the following function in terms of ?
def f(n: int) -> int:
count = 0
for i in range(n):
j = n
while j > 1:
j = j // 2
count = count + 1
return countAnswer: (C)
(C). The inner loop halves j each time, so it runs about times for every one of the iterations of the outer loop: steps in all.
Object Oriented Design and Principles using Python · York University (York, YorkU, York U) · also written EECS1516
Make a free, printable EECS 1516 practice exam: choose the topics, the length and the difficulty above, and get an exam-style PDF with a full answer key. Each paper draws on 600 original questions and skips the ones you have already seen.
Start with the past exams and tests your instructor or the York library shares: they show what your course actually asks. Most courses only have a few, so once you have worked through them, make fresh EECS 1516 practice exams here on the same topics, with full answer keys, as many as you need. The questions here are original, not copies of past exams.
questionbank.dev is independent and not affiliated with York University. Course codes only say which topics a paper covers; papers are original practice material, not past or upcoming exams.