# Python quiz #1

(1) Predict the output of the following:

>>> lst1 = ['a', 'two', 2]
>>> lst1.extend('hello')
>>> lst1
???


(2) Predict the output of the following:

>>> a = [1,2,3, [4,5,6]]
>>> b = a[:]
>>> a = 99
>>> a = 99
>>> b
???


(3) Predict the output of the following statements:

>>> [] == []
???
>>> [] is []
???
>>> id([]) == id([])
???
>>> id([]) is id([])
???


(4) Is the following expression fast or slow to evaluate in Python 3? What about in Python 2?

>>> 985345432 in range(1,int(1.e100))


(5) Is the following a Syntax Error? If not, why not?

>>> a = 3
>>> a++++++4
???
>>> a
???


(6) Given the following simple class Vector, representing a 2-dimensional vector:

import math

class Vector:
def __init__(self, x,y):
self.x, self.y = x,y

def __len__(self):
return math.hypot(self.x, self.y)


What is the difference, if any, between the following usages with the Vector object, v defined as v = Vector(3,4):

>>> v.__len__()
???


and

>>> len(v)
???


(7) Predict the output of the following small program:

class A:
food = 'eggs'

class B(A):
pass

class C(A):
pass

B.food = 'spam'
print(A.food, B.food, C.food)

A.food = 'toast'
print(A.food, B.food, C.food)


(8) What happens when the following statements are executed? Why?

>>> a = (,)
>>> a += [2, 3, 4]
>>> a
???


(9) Predict the output of the following:

>>> def func(a=[]):
...    a.append(10)
...    return a
...
>>> func()

>>> func()
???


(10) What is the difference, if any, in the output of the following two small programs?

def make_power_funcs1():
return (lambda x: x**i for i in range(1,5))

for power_func in make_power_funcs1():
print(power_func(3))


and

def make_power_funcs2():
return [lambda x: x**i for i in range(1,5)]

for power_func in make_power_funcs2():
print(power_func(3))

Current rating: 5