Object Oriented Python: Practice Questions
For each snippet, write the output, the exception type, or “Runs Forever.” If it prints nothing, write “No Output”.
Question 1
items = ['tea', 'cake']
alias = items
alias.append('juice')
print(items)
print(alias is items)Answer
['tea', 'cake', 'juice']
True
Question 2
import copy
items = ['tea', 'cake']
separate = copy.copy(items)
separate.append('juice')
print(items)
print(separate)Answer
['tea', 'cake']
['tea', 'cake', 'juice']
Question 3
import copy
rows = [[1, 2], [3]]
shallow = copy.copy(rows)
shallow[0].append(4)
print(rows)
print(shallow is rows)Answer
[[1, 2, 4], [3]]
False
Question 4
import copy
rows = [[1, 2], [3]]
independent = copy.deepcopy(rows)
independent[0].append(4)
print(rows)
print(independent)Answer
[[1, 2], [3]]
[[1, 2, 4], [3]]
Question 5
rows = [[1], [2]]
selected = rows[:]
selected[0] = [7]
selected[1].append(8)
print(rows)
print(selected)Answer
[[1], [2, 8]]
[[7], [2, 8]]
Question 6
items = [3, 1, 2]
result = items.reverse()
print(items)
print(result)Answer
[2, 1, 3]
None
Question 7
items = [3, 1, 2]
backward = list(reversed(items))
backward.append(9)
print(items)
print(backward)Answer
[3, 1, 2]
[2, 1, 3, 9]
Question 8
scores = [8, 2, 5]
alias = scores
result = scores.sort()
print(alias)
print(result)Answer
[2, 5, 8]
None
Question 9
scores = [8, 2, 5]
ordered = sorted(scores)
scores[0] = 1
print(ordered)
print(scores)Answer
[2, 5, 8]
[1, 2, 5]
Question 10
def next_number(number=4):
number += 1
return number
print(next_number())
print(next_number())
print(next_number(9))Answer
5
5
10
Question 11
def record(value, entries=[]):
entries.append(value)
return entries
first = record('A')
second = record('B')
print(first)
print(first is second)Answer
['A', 'B']
True
Question 12
def record(value, entries=[]):
entries.append(value)
return len(entries)
outside = ['X']
print(record('A'))
print(record('B', outside))
print(record('C'))
print(outside)Answer
1
2
2
['X', 'B']
Question 13
def extend(values):
values += [len(values)]
items = [5, 6]
result = extend(items)
print(items)
print(result)Answer
[5, 6, 2]
None
Question 14
def extend(values):
values = values + [len(values)]
return values
items = [5, 6]
longer = extend(items)
print(items)
print(longer)Answer
[5, 6]
[5, 6, 2]
Question 15
def record(value, entries=None):
if entries is None:
entries = []
entries.append(value)
return entries
first = record('A')
second = record('B')
print(first, second)
print(first is second)Answer
['A'] ['B']
False
Question 16
def record(value, entries=None):
if not entries:
entries = []
entries.append(value)
return entries
empty = []
result = record('A', empty)
print(empty)
print(result)
print(empty is result)Answer
[]
['A']
False
Question 17
class Note:
pass
first = Note()
second = Note()
first.text = 'hello'
second.text = 'goodbye'
alias = first
alias.text = 'welcome'
print(first.text, second.text)Answer
welcome goodbye
Question 18
class Note:
pass
first = Note()
second = Note()
second.text = 'hello'
print(first.text)Answer
AttributeError
Question 19
class Counter:
def __init__(self, start):
self.value = start
first = Counter(3)
second = Counter(8)
first.value += 2
print(first.value, second.value)Answer
5 8
Question 20
class Counter:
def __init__(self, value):
value = value
counter = Counter(3)
print(counter.value)Answer
AttributeError
Question 21
class Sign:
color = 'green'
first = Sign()
second = Sign()
first.color = 'red'
print(first.color, second.color)Answer
red green
Question 22
class Sign:
color = 'green'
first = Sign()
second = Sign()
first.color = 'red'
Sign.color = 'blue'
print(first.color, second.color)Answer
red blue
Question 23
class Basket:
def __init__(self, items=[]):
self.items = items
self.items.append('ticket')
first = Basket()
separate = Basket(['map'])
second = Basket()
print(first.items)
print(separate.items)Answer
['ticket', 'ticket']
['map', 'ticket']
Question 24
class Basket:
def __init__(self, items=None):
if items is None:
items = []
self.items = items
first = Basket()
second = Basket()
first.items.append('map')
print(second.items)
shared = Basket(first.items)
shared.items.append('ticket')
print(first.items)Answer
[]
['map', 'ticket']
Question 25
class Counter:
def __init__(self):
self.value = 0
def add(self, amount):
self.value += amount
first = Counter()
second = Counter()
first.add(3)
first.add(2)
second.add(1)
print(first.value, second.value)Answer
5 1
Question 26
class Basket:
def __init__(self):
self.items = []
def add(self, item):
self.items.append(item)
first = Basket()
second = Basket()
second.items = first.items
first.add('map')
second.add('ticket')
print(first.items)
print(first is second)Answer
['map', 'ticket']
False
Question 27
class Basket:
items = []
def add(self, item):
self.items.append(item)
first = Basket()
second = Basket()
first.add('map')
second.items = []
second.add('ticket')
print(first.items)
print(second.items)Answer
['map']
['ticket']
Question 28
class Counter:
def __init__(self):
self.value = 0
def add(self):
self.value += 1
def total(self):
return self.value
counter = Counter()
print(counter.add())
print(counter.total())Answer
None
1
Question 29
class Box:
def __init__(self, label):
self.label = label
self.items = []
def __str__(self):
return self.label + ': ' + str(len(self.items))
box = Box('Tools')
box.items.append('hammer')
box.items.append('saw')
print(box)Answer
Tools: 2
Question 30
class Ticket:
def __init__(self, number):
self.number = number
def __eq__(self, other):
if not isinstance(other, Ticket):
return False
return self.number == other.number
first = Ticket(7)
second = Ticket(7)
print(first == second)
print(first is second)
print(first == 7)Answer
True
False
False
Question 31
class Book:
def __init__(self, title):
self.title = title
class Film:
def __init__(self, title):
self.title = title
def label(item):
return item.title.upper()
print(label(Book('Dune')))
print(label(Film('Arrival')))
print(label({'title': 'Alien'}))Answer
AttributeError
Question 32
class Note:
def __init__(self, text):
self.text = text
def rooms(self):
found = []
for word in self.text.split():
if word.startswith('room:'):
found.append(word[5:])
return found
note = Note('room:12 classroom:3 room:12 room:7!')
print(note.rooms())Answer
['12', '12', '7!']