Unit 3 Test Flashcards

(3 cards)

1
Q

What will be the result of executing the following code?

def f(x):
try:
X = X / X
except:
print(“a”,end=’’)
else:
print(“b”,end=’’)
finally:
print(“c”,end=’’)

f (1)
f (0)

A

it will print bcac

for f(1) there is no exception error so it goes to else and finally
for f(2) there is a division zero error so it goes to except then finally

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What will be the output of the following code?

class A:
a = 1
print (hasattr(A, ‘a’))

A

True

the hasattr() func checks for a specific object in a class, hasattr(class, “object”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What will be the result of executing the following code?
class I:

def __init__(self):
self.s = ‘abc’
self.i i = 0

def __iter__ (self):
return self

def __next__ (self):
if self.i == len(self.s):
raise StopIteration
v = self.s[self.i]
self.i += 1
return v

for x in I():
print(x, end=’’)

A

it will print abc

How well did you know this?
1
Not at all
2
3
4
5
Perfectly