Quiz

What is the output of the given code?
do_something('a')
def do_something(name):
  print(name)

NameError a name Function is invoked before it is defined, hence the NameError with the message "name 'do_something' is not defined"

What is the output of the given code?
def do_something(num):
  if num == 0:
    return None
  return num
b = do_something(10)
print(b)

10 Error as cannot have 2 return statements None Function can have many return statements as long as they all belong to different control blocks.

What is the output of the given code?
def do_something(a, b):
  print(a+b)
do_something(10, 20, 30)

Error as one extra argument is passed than the function definition parameters 30 50 Function definition is not expecting variable argument list, hence the number of arguments should match the function definition number of parameters.

What is the output of the given code?
def do_something(*a):
  print(a[1]+a[2])
do_something(10, 20, 30)

Error due to extra arguments 30 50 Function is defined to receive variable arguments so any number of arguments can be passed with this definition and the arguments with be received as one tuple and you can get individual elements of the tuple with the index number.

What is the output of the given code?
def do_something(a):
  return a * 2
  print(a)
b = do_something(10)
print(b)

Prints out 10 and then 20 Prints out 20 and then 10 Prints out 20 only Error The print statement inside the function is unreachable as there is a return statement before that. Hence you will see only '20' printed on the console.

What is the output of the given code?
def do_something(a, b=10, c):
  return a*b*c
b = do_something(1, 2)
print(b)

20 2 Error 200 SyntaxError: non-default argument 'c', follows default argument b. So if the function declaration is changed to 'def do_something(a, c, b=10)', it will work as all the non default arguments, a and c are declared before the default argument b.

What is the output of the given code?
def do_something(a, b):
  if(a > b):
    print('hi')
  else:
    print('hello')
do_something(b=1, a=2)

hi hello Error as you cannot send keyword arguments for this function You can send keyword arguments and in this case the position of the argument is ignored and instead the given keyword is used to assign values to arguments

What is the output of the given code?
def do_something(a, b):
  if(a > b):
    print('hi')
  else:
    print('hello')
do_something(b=1, c=2)

hi hello Error as there is no parameter named c

What is the output of the given code?
def do_something(a, b=10):
  print(b, a)
do_something('a'=5)

Error 5 10 10 5 Keyword argument 'a' cannot be within any type of quotes in the function invocation.

Exercise

results matching ""

    No results matching ""