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 for −

raining'. find('z') ?

A - Type error

B - ' '

C - -1

D - Not found

Answer : C

Explanation

If the string is not found by method find() , it returns the integer -1.

Q 2 - Which of the following is false statement in python

A - int(144)==144

B - int('144')==144

C - int(144.0)==144

D - None of the above

Answer : D

Explanation

The built-in int() type constructor converts string and floating value to integer.

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 of following code −

s = ''mnopqr ''
i = ''m ''
while i in s:
   print('i', end= '' '')

A - i i i i i i i i……..

B - m m m m m …..

C - m n o p q r

D - no output

Answer : A

Q 5 - What is the output of the code?

def f():
   global z
   print('z is: ', z)
   z=50
   print('new value of global z is: ', z)

f()
   print('Value of z is: ', z)

A - z is 100

new value of global z is: 100

value of z is 100

B - z is 100

new value of global z is: 100

value of z is 50

C - z is 100

new value of global z is: 50

value of z is 100

D - z is 100

new value of global z is: 50

value of z is 50

Answer : D

Explanation

Here in the above code ‘global’ keyword is used to state that ‘z’ is global variable. So when we assign a value to z in the function then the new assigned value is reflected whenever we use the value of ‘z’ in the main block or outside the function.

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

class P: 
   def __init__(self): 
      self.__x=100 
      self.y=200 
   def print(self): 
      print(self.__x, self.y)  
class C(P): 
   def __init__(self): 
      super().__init__() 
      self.__x=300 
      self.y=400  
d = C() 
d.print()

A - 300 400

B - 100 400

C - 100 200

D - 300 200

Answer : B

Explanation

In the above code x is a private variable declared in the class P. Thus value of x cannot be changed in the class C which inherits class P. But y is not a private variable thus its value can be changed.

Q 7 - Discuss the outcome of the code?

def func1(n): 
   if(n==0): 
      return 0 
   else: 
      return(n+func1(n-1)) 
def func2(n, result): 
   if(n==0): 
      return(result) 
   else: 
      return(func2(n-1, n+result))  
print(func1(4)) 
print(func2(4,0)) 

A - Func1 is tail recursion.

B - Func1 and Func2 are tail recursions.

C - Func2 is only tail recursion.

D - Neither Func2 nor Func1 is tail recursion.

Answer : B

Explanation

A function call is said to be tail recursive if there is nothing to do after the function returns except return its value.

Q 8 - Which method is used to convert raw byte data to a string?

A - Encode()

B - Decode()

C - Convert()

D - tostring()

Answer : B

Explanation

Decode is the method used to convert the raw byte data to a string.

Q 9 - Select the correct option to draw a rectangle centred at 50,50 with width and height as 50, 70 respectively.

A - Canvas.create_rect(50,50,50,70)

B - Canvas.create_rect(50,70,50,50)

C - Canvas.create_rectangle(50,50,50,70)

D - Tkinter.create_rect(50,50,50,70)

Answer : C

python_questions_answers.htm
Advertisements