55284A: Introduction to Python Quiz Questions and Answers

What is the output of the following function call? def outerFun(a, b): def innerFun(c, d): return c + d return innerFun(a, b) return a result = outerFun(5, 10) print(result)

Answer :
  • Syntax Error

Explanation :

This is an example of inner function in which a function is defined inside the body of other function. Two return statements are used in the outer function, so the second return statement will get the value returned by previous return statement of the out

What is displayed when the following program is run? try: list = 10 * [0] x = list[9] print( "Done ") except IndexError: print( "Index out of bound ") else: print( "Nothing is wrong ") finally: print( "Finally we are here ")

Answer :
  • "Done " followed by "Nothing is wrong " followed by "Finally we are here "

Explanation :

"Done" will get print as there is no error in code. Then "Nothing is wrong" will print as this print statement is under the else block which will get executed when there is no error and then "Finally we are here" will get printed because "finally" block w

Which one of the following has the highest precedence in the expression?

Answer :
  • Parentheses

Explanation :

Just remember: PEMDAS, that is, Parenthesis, Exponentiation, Division, Multiplication, Addition, Subtraction. Note that the precedence order of Division and Multiplication is the same. Likewise, the order of Addition and Subtraction is also the same.

What will be the output of the following Python code? random.seed(3) random.randint(1,5) 2 random.seed(3) random.randint(1,5)

Answer :
  • 2

Explanation :

We use the seed function when we want to use the same random number once again in our program. Hence the output of the code shown above will be 2, since 2 was generated previously following which we used the seed function.

Suppose d = { "john ":40, "peter ":45}, what happens when retieving a value using d.get( "susan ")?

Answer :
  • It is executed fine and no exception is raised, and it returns None.

Explanation :

This get() function of the dictionary will return value for the key specified. If key does not exists then it will return None. It will return an optional value if specified while calling in case when key is not found.

What is the output of the following code? a = True b = False c = False if not a or b: print(1) elif not a or not b and c: print(2) elif not a or b or not b and a: print(3) else: print(4)

Answer :
  • 3

Explanation :

 In Python the precedence order is first NOT then AND and in last OR. So the if condition and second elif condition evaluates to False while third elif condition is evaluated to be True resulting in 3 as output.

What is the output of the add() function call? def add(a, b): return a+5, b+5 result = add(3, 2) print(result)

Answer :
  • (8, 7)

Explanation :

This function call will return a tuple and that whole tuple will be stored in a variable. If you would have used two variables to store the result then each variable would have get each value from the function.

What will be displayed by the following code? def f(values): values[0] = 44 v = [1, 2, 3] f(v) print(v)

Answer :
  • [44, 2, 3]

Explanation :

When function is being called with the list as parameter in it, the function 0th index is being updated. It is possible because list is a mutable object which will get update after the function will be called.