Fall 2026
  • Discord
  • Gradescope
  • Syllabus

On this page

  • Question 1
  • Question 2
  • Question 3
  • Question 4
  • Question 5
  • Question 6
  • Question 7
  • Question 8
  • Question 9
  • Question 10
  • Question 11
  • Question 12
  • Question 13
  • Question 14
  • Question 15
  • Question 16
  • Question 17
  • Question 18
  • Question 19
  • Question 20
  • Question 21
  • Question 22
  • Question 23
  • Question 24
  • Question 25
  • Question 26
  • Question 27
  • Question 28
  • Question 29
  • Question 30
  • Question 31
  • Question 32
  • Question 33
  • Question 34
  • Question 35
  • Question 36
  • Question 37
  • Question 38
  • Question 39
  • Question 40
  • Question 41
  • Question 42
  • Question 43
  • Question 44
  • Question 45
  • Question 46
  • Question 47
  • Question 48
  • Question 49
  • Question 50
  • Question 51
  • Question 52
  • Question 53
  • Question 54

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

import copy

xs = [1, 2, 3]
ys = xs
ys.append('A')
print('xs=', xs)
Answer
xs= [1, 2, 3, 'A']

Question 2

import copy

xs = [1, 2, 3]
ys = copy.copy(xs)
ys.append('A')
print('xs=', xs)
Answer
xs= [1, 2, 3]

Question 3

import copy

xs = [1, 2, 3]
ys = xs[1:]
ys.append('A')
print('xs=', xs)
Answer
xs= [1, 2, 3]

Question 4

import copy

xs = [1, 2, 3]
ys = xs[:]
ys.append('A')
print('xs=', xs)
Answer
xs= [1, 2, 3]

Question 5

import copy

xs = [1, 2, 3]
ys = copy.deepcopy(xs)
ys.append('A')
print('xs=', xs)
Answer
xs= [1, 2, 3]

Question 6

import copy

xs = [[1, 2, 3], [4, 5, 6]]
ys = xs
ys.append('A')
print('xs=', xs)
Answer
xs= [[1, 2, 3], [4, 5, 6], 'A']

Question 7

import copy

xs = [[1, 2, 3], [4, 5, 6]]
ys = xs[:]
ys.append('A')
print('xs=', xs)
Answer
xs= [[1, 2, 3], [4, 5, 6]]

Question 8

import copy

xs = [[1, 2, 3], [4, 5, 6]]
ys = xs
ys[0].append('A')
print('xs=', xs)
Answer
xs= [[1, 2, 3, 'A'], [4, 5, 6]]

Question 9

import copy

xs = [[1, 2, 3], [4, 5, 6]]
ys = copy.copy(xs)
ys[0].append('A')
print('xs=', xs)
Answer
xs= [[1, 2, 3, 'A'], [4, 5, 6]]

Question 10

import copy

xs = [[1, 2, 3], [4, 5, 6]]
ys = copy.deepcopy(xs)
ys[0].append('A')
print('xs=', xs)
Answer
xs= [[1, 2, 3], [4, 5, 6]]

Question 11

import copy

xs = [[1, 2, 3], [4, 5, 6]]
ys = xs[0][:]
ys.append('A')
print('xs=', xs)
Answer
xs= [[1, 2, 3], [4, 5, 6]]

Question 12

def foo(x=4):
    x += 1
    return x


y = foo()
y = foo()
y = foo()
y = foo()
print('y=', y)
Answer
y= 5

Question 13

xs = [1, 2, 3]


def foo(xs=[]):
    xs.append(len(xs) + 1)
    return len(xs)


y = foo()
y = foo()
y = foo()
y = foo()
print('y=', y)
Answer
y= 4

Question 14

xs = [1, 2, 3]


def foo(xs=[]):
    xs.append(len(xs) + 1)
    return len(xs)


y = foo([1])
y = foo([1, 2, 3])
y = foo()
y = foo([1, 2])
print('y=', y)
Answer
y= 3

Question 15

xs = [1, 2, 3]


def foo(xs=[]):
    xs.append(len(xs) + 1)
    return len(xs)


y = foo([1])
y = foo([1, 2, 3])
y = foo([1, 2])
y = foo()
print('y=', y)
Answer
y= 1

Question 16

xs = [1, 2, 3]


def foo(xs=[]):
    xs.append(len(xs) + 1)
    return len(xs)


y = foo()
y = foo([1, 2, 3])
y = foo()
y = foo()
print('y=', y)
Answer
y= 3

Question 17

xs = [1, 2, 3]


def foo(xs=[]):
    xs += [4]
    return len(xs)


y = foo()
y = foo()
y = foo()
y = foo()
print('y=', y)
Answer
y= 4

Question 18

def foo(xs=[1]):
    xs = xs + xs
    return len(xs)


y = foo()
y = foo()
y = foo()
y = foo()
print('y=', y)
Answer
y= 2

Question 19

def reverse_list(xs):
    ys = []
    for i in range(len(xs)):
        ys.append(xs.pop())
    return ys


xs = [1, 2, 3]
ys = reverse_list(xs)
print('xs=', xs)
print('ys=', ys)
Answer
xs= []
ys= [3, 2, 1]

Question 20

import copy


def reverse_list(xs):
    ys = []
    xs = copy.copy(xs)
    for i in range(len(xs)):
        ys.append(xs.pop())
    return ys


xs = [1, 2, 3]
ys = reverse_list(xs)
print('xs=', xs)
print('ys=', ys)
Answer
xs= [1, 2, 3]
ys= [3, 2, 1]

Question 21

def reverse_list(xs):
    ys = []
    xs = xs[:]
    for i in range(len(xs)):
        ys.append(xs.pop())
    return ys


xs = [1, 2, 3]
ys = reverse_list(xs)
print('xs=', xs)
print('ys=', ys)
Answer
xs= [1, 2, 3]
ys= [3, 2, 1]

Question 22

def reverse_list(xs):
    ys = xs
    for i in range(len(ys)):
        ys[i] = xs[-i - 1]
    return ys


xs = [1, 2, 3]
ys = reverse_list(xs)
print('xs=', xs)
print('ys=', ys)
Answer
xs= [3, 2, 3]
ys= [3, 2, 3]

Question 23

import copy


def reverse_list(xs):
    ys = copy.copy(xs)
    for i in range(len(ys)):
        xs[i] = ys[-i - 1]
    return ys


xs = [1, 2, 3]
ys = reverse_list(xs)
print('xs=', xs)
print('ys=', ys)
Answer
xs= [3, 2, 1]
ys= [1, 2, 3]

Question 24

import copy


def reverse_list(xs):
    ys = copy.copy(xs)
    for i in range(len(ys)):
        ys[i] = xs[-i - 1]
    return ys


xs = [1, 2, 3]
ys = reverse_list(xs)
print('xs=', xs)
print('ys=', ys)
Answer
xs= [1, 2, 3]
ys= [3, 2, 1]

Question 25

def reverse_list():
    ys = xs
    for i in range(len(ys)):
        ys[i] = xs[-i - 1]
    return ys


xs = [1, 2, 3]
ys = reverse_list()
print('xs=', xs)
print('ys=', ys)
Answer
xs= [3, 2, 3]
ys= [3, 2, 3]

Question 26

xs = [1, 2, 3]
ys = xs.reverse()
print('xs=', xs)
print('ys=', ys)
Answer
xs= [3, 2, 1]
ys= None

Question 27

xs = [1, 2, 3]
ys = list(reversed(xs))
print('xs=', xs)
print('ys=', ys)
Answer
xs= [1, 2, 3]
ys= [3, 2, 1]

Question 28

xs = [1, 2, 3]
ys = xs.sort()
print('xs=', xs)
print('ys=', ys)
Answer
xs= [1, 2, 3]
ys= None

Question 29

xs = [1, 2, 3]
ys = list(sorted(xs))
print('xs=', xs)
print('ys=', ys)
Answer
xs= [1, 2, 3]
ys= [1, 2, 3]

Question 30

xs = [1, 2, 3]
xs.append(xs)
y = xs[3][3][0]
print('y=', y)
Answer
y= 1

Question 31

xs = [1, 2, 3]
xs.append(xs)
y = xs[0][3][3]
print('y=', y)
Answer
TypeError

Question 32

xs = []
xs.append(xs)
xs.append(xs)
xs.append(xs)
xs.append(xs)
y = len(xs[0][1][2])
print('y=', y)
Answer
y= 4

Question 33

xs = []
xs.append(xs)
xs.append(xs)
xs.append(xs)
xs.append(xs)
y = len(xs[:0][1][2])
print('y=', y)
Answer
IndexError

Question 34

class Foo:
    pass


a = Foo()
a.message = 'hello world'
b = Foo()
b.message = 'hola mundo'
print('a.message=', a.message)
Answer
a.message= hello world

Question 35

class Foo:
    pass


a = Foo()
b = Foo()
b.message = 'hello world'
print('a.message=', a.message)
Answer
AttributeError

Question 36

class Foo:
    message = 'salve munde'


a = Foo()
b = Foo()
b.message = 'hola mundo'
print('a.message=', a.message)
Answer
a.message= salve munde

Question 37

class Foo:
    pass


a = Foo()
b = Foo()
b.message = 'hola mundo'
Foo.message = 'salve munde'
print('a.message=', a.message)
Answer
a.message= salve munde

Question 38

class Foo:
    pass


a = Foo()
a.message = 'hello world'
b = Foo()
b.message = 'hola mundo'
Foo.message = 'salve munde'
print('a.message=', a.message)
Answer
a.message= hello world

Question 39

class Foo:
    def __init__(self, message):
        self.message = message


a = Foo('hello world')
b = Foo('hola mundo')
print('a.message=', a.message)
Answer
a.message= hello world

Question 40

class Foo:
    def __init__(self, message):
        message = message


a = Foo('hello world')
b = Foo('hola mundo')
print('a.message=', a.message)
Answer
AttributeError

Question 41

class Foo:
    message = 'salve munde'

    def __init__(self, message):
        message = message


a = Foo('hello world')
b = Foo('hola mundo')
print('a.message=', a.message)
Answer
a.message= salve munde

Question 42

class Foo:
    def __init__(self, message):
        Foo.message = message


a = Foo('hello world')
b = Foo('hola mundo')
print('a.message=', a.message)
Answer
a.message= hola mundo

Question 43

class Foo:
    def __init__(self, message):
        Foo.message = message


a = Foo('hello world')
b = Foo('hola mundo')
Foo.message = 'salve mundo'
print('a.message=', a.message)
Answer
a.message= salve mundo

Question 44

class Foo:
    def __init__(self, message):
        Foo.message = message


a = Foo('hello world')
b = Foo('hola mundo')
b.message = 'salve mundo'
print('a.message=', a.message)
Answer
a.message= hola mundo

Question 45

class Foo:
    def __init__(self, message=None):
        self.message = message


a = Foo()
b = Foo('hola mundo')
print('a.message=', a.message)
Answer
a.message= None

Question 46

class Foo:
    def __init__(self, message=None):
        if message:
            self.message = message


a = Foo()
b = Foo('hola mundo')
print('a.message=', a.message)
Answer
AttributeError

Question 47

class Foo:
    def __init__(self, xs=[]):
        self.xs = xs
        self.xs.append('init')


a = Foo(['hello'])
b = Foo()
c = Foo(['hola'])
d = Foo()
x = len(d.xs)
print('x=', x)
Answer
x= 2

Question 48

class Foo:
    def __init__(self, xs=None):
        if not xs:
            xs = []
        self.xs = xs
        self.xs.append('init')


a = Foo(['hello'])
b = Foo()
c = Foo(['hola'])
d = Foo()
x = len(d.xs)
print('x=', x)
Answer
x= 1

Question 49

class Foo:
    def __init__(self, xs=[]):
        self.xs = xs
        xs.append('init')


a = Foo(['hello'])
b = Foo()
c = Foo(['hola'])
d = Foo()
x = len(d.xs)
print('x=', x)
Answer
x= 2

Question 50

class Foo:
    def __init__(self, xs=None):
        if not xs:
            xs = []
        self.xs = xs
        xs.append('init')


a = Foo(['hello'])
b = Foo()
c = Foo(b.xs)
d = Foo()
x = len(d.xs)
print('x=', x)
Answer
x= 1

Question 51

class Foo:
    def __init__(self):
        self.xs = []

    def bar(self, x):
        self.xs.append(x)


a = Foo()
b = Foo()
a.bar('hola')
b.bar('mundo')
x = len(a.xs)
print('x=', x)
Answer
x= 1

Question 52

class Foo:
    def __init__(self):
        self.xs = []

    def bar(self, x):
        self.xs.append(x)


a = Foo()
b = Foo()
b.xs = a.xs
a.bar('hola')
b.bar('mundo')
x = len(a.xs)
print('x=', x)
Answer
x= 2

Question 53

class Foo:
    def __init__(self):
        self.xs = []

    def bar(self, x):
        self.xs.append(x)


a = Foo()
b = Foo()
a.bar(b.xs)
b.bar('mundo')
x = len(a.xs)
print('x=', x)
Answer
x= 1

Question 54

class Foo:
    def __init__(self):
        self.xs = []

    def bar(self, x):
        self.xs.append(x)


a = Foo()
b = Foo()
a.xs = b.xs
a.bar(b.xs)
b.bar('mundo')
x = len(a.xs)
print('x=', x)
Answer
x= 2