# Python Concepts/Quizzes/Functions & classes

< Python Concepts | Quizzes(Redirected from Python/Quizzes/Functions & classes)

1

With the follow code

```
def parallel(x, y=1):
return float(x * y) / (x + y)
parallel(2)
```

the follow happens:

A syntax error as you cannot have assignments in the function argument | |

An exception is raised as the second argument is missing when making a call to the function | |

The result is approximately 0.6666 as the second argument is by default 1 |

2

With the two different definitions:

```
def first(n, m):
"Bernoulli numbers computed as 1**m + 2**m + ... + n**m"
return sum(map(lambda n: n**m, range(1,n+1)))
def second(n):
# Bernoulli numbers computed as 1**m + 2**m + ... + n**m
return sum(map(lambda n: n**m, range(1,n+1)))
```

True | False | |
---|---|---|

The first line in the body of the "second" function is a comment | ||

The first line in the body in the 'first' function will make a syntax error as you cannot have a string without an assignment inside a function. | ||

"first.__doc__" contains the string "Bernoulli numbers computed as 1**m + 2**m + ... + n**m" | ||

"second.__doc__" contains the string "Bernoulli numbers computed as 1**m + 2**m + ... + n**m" | ||

The first definition has a more standard Python documentation than the second definition |

3

With the two above code

True | False | |
---|---|---|

`help(first)` will display the line "Bernoulli numbers computed as 1**m + 2**m + ... + n**m" |
||

`dir(first)` does not work as "first" is not a module |

4

With the follow code

```
class Mammal():
def number_of_legs(self):
return 4
class Dog(Mammal):
pass
class Human(Mammal):
def number_of_legs(self):
return 2
fido = Dog()
anna = Human()
```

we have:

True | False | |
---|---|---|

A class called "Mammal" is defined | ||

Mammal is a class derived from Dog | ||

Dog is a class derived from Mammal |

5

With the above code we have:

True | False | |
---|---|---|

"anna" is an instance of "Human". | ||

`fido.number_of_legs()` makes an error. |

6

We have that

__str__ | __init__ | __add__ | |
---|---|---|---|

Is the name of the constructor method for a class | |||

Is the method associated with extending a list with another list |

7

With the follow code

```
class Myint():
def __init__(self, integer):
self.integer = long(integer)
def __add__(self, integer):
return long(str(self.integer) + str(integer))
a = Myint(1)
b = a + 3
c = b + 4
```

we have

True | False | |
---|---|---|

b = 4 | ||

b = 4L | ||

b = 13L | ||

c = 8 | ||

c = 8L | ||

c = 17L | ||

c = 134L |

8

We have

True | False | |
---|---|---|

`True __add__ False` will result in "True" |
||

In `True or False` the "or" is not the correct syntax |

9

With the follow code

```
for n in range(10):
if n == 2 or n == 6:
continue
elif n == 8:
break
print(n)
else:
print("Hello")
```

True | False | |
---|---|---|

The code will print 0 and 1 but finish after 2. | ||

You cannot have "else" there: The indentation in the block is wrong | ||

"Hello" will be printed several times |

10

With the follow code

```
def bum():
n = 1
while True:
if not n % 5 or 5 in map(int, list(str(n))):
yield 'bum'
else:
yield n
n += 1
bums = bum()
for b in bums:
print(b)
```

True | False | |
---|---|---|

Clearly there is missing a 'return' statement somewhere | ||

Only integers from 1 to 5 are printed | ||

As the "while" loop is infinite then the "for" will at one point experience a memory problem when the number of elements gets too big. | ||

The if sentence examines whether 'n' is divisible with 5 or contains the decimal 5. |