QUIZ ON : PROGRAMMING AND PROBLEM SOLVING THROUGH PYTHON - FUNCTIONS IN PYTHON

FUNCTIONS IN PYTHON

00:00:00

What will be the output of the following Python function?

import math

abs(math.sqrt(25))

Option A. Error
Option B. -5
Option C. 5
Option D. 5.0
True Answer D

Explanation :
The abs() function prints the absolute value of the argument passed. For example: abs(-5)=5. Hence, in this case we get abs(5.0)=5.0.

What will be the output of the following Python function?

import math

abs(math.sqrt(25))

Question No# : 01 out of 20



The function divmod(a,b), where both ‘a’ and ‘b’ are integers is evaluated as:

Option A. (a%b, a//b)
Option B. (a//b, a%b)
Option C. (a//b, a*b)
Option D. (a/b, a%b)
True Answer B

Explanation :
The function divmod(a,b) is evaluated as a//b, a%b, if both ‘a’ and ‘b’ are integers.

The function divmod(a,b), where both ‘a’ and ‘b’ are integers is evaluated as:

Question No# : 02 out of 20



Which of the following is a unit of code that is often defined within a greater code structure?

Option A. Subroutines
Option B. Function
Option C. Files
Option D. Modules
True Answer B

Explanation :

Which of the following is a unit of code that is often defined within a greater code structure?

Question No# : 03 out of 20

What is the type of sys.argv?

Option A. set
Option B. list
Option C. tuple
Option D. string
True Answer B

Explanation :
It is a list of elements.

What is the type of sys.argv?

Question No# : 04 out of 20



The variables in a function definition are called as

Option A. Subroutines
Option B. Function
Option C. Definition
Option D. Parameters
True Answer D

Explanation :

The variables in a function definition are called as

Question No# : 05 out of 20

Given a function that does not return any value, What value is thrown by default when executed in shell.

Option A. int
Option B. bool
Option C. void
Option D. None
True Answer D

Explanation :
Python shell throws a NoneType object back.

Given a function that does not return any value, What value is thrown by default when executed in shell.

Question No# : 06 out of 20



What will be the output of the following Python code?

def f1(a,b=[]):      b.append(a)      return b  print(f1(2,[3,4]))

Option A. [3,2,4]
Option B. [2,3,4]
Option C. Error
Option D. [3,4,2]
True Answer D

Explanation :
In the code shown above, the integer 2 is appended to the list [3,4]. Hence the output of the code is [3,4,2]. Both the variables a and b are local variables.

What will be the output of the following Python code?

def f1(a,b=[]):      b.append(a)      return b  print(f1(2,[3,4]))

Question No# : 07 out of 20



The function pow(x,y,z) is evaluated as:

Option A. (x**y)**z
Option B. (x**y) / z
Option C. (x**y) % z
Option D. (x**y)*z
True Answer C

Explanation :
The built-in function pow() can accept two or three arguments. When it takes in two arguments, they are evaluated as x**y. When it takes in three arguments, they are evaluated as (x**y)%z.

The function pow(x,y,z) is evaluated as:

Question No# : 08 out of 20



What happens if the base condition isn’t defined in recursive programs?

Option A. Program gets into an infinite loop
Option B. Program runs once
Option C. Program runs n number of times where n is the argument given to the function
Option D. An exception is thrown
True Answer A

Explanation :
The program will run until the system gets out of memory.

What happens if the base condition isn’t defined in recursive programs?

Question No# : 09 out of 20



What will be the output of the following Python function?

list(enumerate([2, 3]))

Option A. Error
Option B. [(1, 2), (2, 3)]
Option C. [(0, 2), (1, 3)]
Option D. [(2, 3)]
True Answer C

Explanation :
The built-in function enumerate() accepts an iterable as an argument. The function shown in the above case returns containing pairs of the numbers given, starting from 0. Hence the output will be: [(0, 2), (1,3)].

What will be the output of the following Python function?

list(enumerate([2, 3]))

Question No# : 10 out of 20



What will be the output of the following Python code?

def f(): x=4  x=1  f()  x

Option A. Error
Option B. 4
Option C. Junk value
Option D. 1
True Answer D

Explanation :
In the code shown above, when we call the function f, a new namespace is created. The assignment x=4 is performed in the local namespace and does not affect the global namespace. Hence the output is 1.

What will be the output of the following Python code?

def f(): x=4  x=1  f()  x

Question No# : 11 out of 20



What will be the output of the following Python code?

def foo(i, x=[]):      x.append(x.append(i))      return x  for i in range(3):      y = foo(i)  print(y)

Option A. [[[0]], [[[0]], [1]], [[[0]], [[[0]], [1]], [2]]]
Option B. [[0], [[0], 1], [[0], [[0], 1], 2]]
Option C. [0, None, 1, None, 2, None]
Option D. [[[0]], [[[0]], [1]], [[[0]], [[[0]], [1]], [2]]]
True Answer C

Explanation :
append() returns None.

What will be the output of the following Python code?

def foo(i, x=[]):      x.append(x.append(i))      return x  for i in range(3):      y = foo(i)  print(y)

Question No# : 12 out of 20



What will be the output of the following Python function?

all([2,4,0,6])

Option A. Error
Option B. True
Option C. False
Option D. 0
True Answer C

Explanation :
The function all returns false if any one of the elements of the iterable is zero and true if all the elements of the iterable are non zero. Hence the output of this function will be false.

What will be the output of the following Python function?

all([2,4,0,6])

Question No# : 13 out of 20



How are required arguments specified in the function heading?

Option A. identifier followed by an equal to sign and the default value
Option B. identifier followed by the default value within backticks (“)
Option C. identifier followed by the default value within square brackets ([])
Option D. identifier
True Answer D

Explanation :
Refer documentation.

How are required arguments specified in the function heading?

Question No# : 14 out of 20



What will be the output of the following Python code?

def foo(i, x=[]):      x.append(i)      return x  for i in range(3):      print(foo(i))

Option A. [0] [1] [2]
Option B. [0] [0, 1] [0, 1, 2]
Option C. [1] [2] [3]
Option D. [1] [1, 2] [1, 2, 3]
True Answer B

Explanation :
When a list is a default value, the same list will be reused.

What will be the output of the following Python code?

def foo(i, x=[]):      x.append(i)      return x  for i in range(3):      print(foo(i))

Question No# : 15 out of 20



What will be the output of the following Python code?

  x=1  def cg():  	global x  	x=x+1	  cg()  x

Option A. 2
Option B. 1
Option C. 0
Option D. Error
True Answer A

Explanation :
Since ‘x’ has been declared a global variable, it can be modified very easily within the function. Hence the output is 2.

What will be the output of the following Python code?

  x=1  def cg():  	global x  	x=x+1	  cg()  x

Question No# : 16 out of 20



Read the following Python code carefully and point out the global variables?

  y, z = 1, 2  def f():      global x      x = y+z

Option A. x
Option B. y and z
Option C. x, y and z
Option D. Neither x, nor y, nor z
True Answer C

Explanation :
In the code shown above, x, y and z are global variables inside the function f. y and z are global because they are not assigned in the function. x is a global variable because it is explicitly specified so in the code. Hence, x, y and z are global variables.

Read the following Python code carefully and point out the global variables?

  y, z = 1, 2  def f():      global x      x = y+z

Question No# : 17 out of 20



What will be the output of the following Python code?

def foo(k):      k[0] = 1  q = [0]  foo(q)  print(q)

Option A. [0]
Option B. [1]
Option C. [1, 0]
Option D. [0, 1]
True Answer B

Explanation :
Lists are passed by reference.

What will be the output of the following Python code?

def foo(k):      k[0] = 1  q = [0]  foo(q)  print(q)

Question No# : 18 out of 20



Which of the following is a distinct syntactic block?

Option A. Subroutines
Option B. Function
Option C. Definition
Option D. Modules
True Answer C

Explanation :

Which of the following is a distinct syntactic block?

Question No# : 19 out of 20

What is the type of each element in sys.argv?

Option A. set
Option B. list
Option C. tuple
Option D. string
True Answer D

Explanation :
It is a list of strings.

What is the type of each element in sys.argv?

Question No# : 20 out of 20