Python Online Quiz



Following quiz provides Multiple Choice Questions (MCQs) related to Python. You will have to read all the given answers and click over the correct answer. If you are not sure about the answer then you can check the answer using Show Answer button. You can use Next Quiz button to check new set of questions in the quiz.

Questions and Answers

Q 1 - What is output of −

33 == 33.0

A - False

B - True

C - 33

D - None of the above

Answer : B

Explanation

comparison operator evaluates true and false. And in python we need not specify whether the number is int or float.

Q 2 - How can we swap two numbers a = 10, b = 20 in python without using third variable?

A - a = b

b = a

B - a,b = b,a

C - both a & b

D - b = a

a = b

Answer : C

Explanation

To swap two numbers we can use both a & b option. Both a & b are similar statemnts written in different ways.

Q 3 - What is output of following code −

print('hijk'.partition('ab'))

A - ('hijk', 'cd', ' ')

B - ('hijk')

C - ('hijk', ' ', ' ')

D - Name error

Answer : C

Explanation

Since there are no separators in the given string so the output is the same string.

Q 4 - What is output for −

2 * 2 **3

A - 12

B - 64

C - 16

D - 36

Answer : C

Explanation

2**3 gives 2*2*2 i.e. 8, then 2*8 gives 16, since order of precedence is ** then*. ** implies raise to power''.

Q 5 - What is the output of the following code?

def nprint(message, n):
while(n > 0):
   print(message)
n-=1
nprint('z', 5)

A - zzzz

B - zzzzz

C - Syntax Error

D - Infinite Loop

Answer : D

Explanation

Because decrementing condition of n' is not present in the while loop.

Q 6 - What is the output of the following code?

def f(x = 100, y = 100): 
   return(x+y, x-y) 
x, y = f(y = 200, x = 100) 
print(x, y) 

A - 300 -100

B - 200 0

C - 200 100

D - 0 300

Answer : A

Explanation

In variable y functions runs and gives the output of x + y and x - y i.e 300 and -100 respectively. Then assignment operator is used to assign the value of x and y.

Q 7 - Analyze the code −

print(''Recursive Function'') 
def factorial(n): 
   return(n*factorial(n-1))  
factorial(4)

A - Recursive Function 24.

B - Recursive Function.

C - Function runs infinitely and causes a StackOverflowError.

D - Syntax Error.

Answer : C

Explanation

there is no condition in the code to stop the function.

Answer : D

Explanation

You need to define the format in which you want to open the file. Proper path has to be declared by the user for the interpreter to reach the destination of the file.

Answer : C

python_questions_answers.htm
Advertisements