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 −

'search'. find('S') ?

A - s

B - -1

C -

D - None of the above

Answer : B

Explanation

The method find() is case sensitive.

Q 2 - What is the output of following code −

[ (a,b) for a in range(3) for b in range(a) ]

A - [ (1,0),(2,1),(3,2)]

B - [ (0,0),(1,1),(2,2)]

C - [(1,0),(2,1),(2,1)]

D - [ (1,0),(2,0),(2,1)]

Answer : D

Explanation

This is nested for loop. The output of first for loop will be the value for the next loop.

Q 3 - Select the option for following code −

s = 0
for d in range(0, 5, 0.1):
 s += d
 print(s)

A - Syntax Error

B - Type Error

C - Runtime Error

D - Both b & c

Answer : D

Explanation

we will get type error during runtime as float object cannot be interpreted as integer. Here 0.1 is the float value.

Q 4 - What is output of following code −

def func(x, ans):
   if(x==0):
      return 0
   else: 
      return func(x-1, x+ans) 
print(func(2,0))

A - 0

B - 1

C - 2

D - 3

Answer : A

Q 5 - Find the output of the code?

def f(a, b = 1, c = 2):
   print('a is: ',a, 'b is: ', b, 'c is: ', c)
f(2, c = 2)
f(c = 100, a = 110)

A - a is: 2 b is: 1 c is: 2

a is: 110 b is: 1 c is: 100

B - a is: 2 b is: 2 c is: 2

a is: 110 b is: 2 c is: 100

C - a is: 0 b is: 2 c is: 2

a is: 110 b is: 0 c is: 100

D - a is: 110 b is: 0 c is: 100

a is: 110 b is: 0 c is: 100

Answer : A

Explanation

Value of Arguments is passed to the parameters in order of their sequence.

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 - What is the out of the code?

def rev_func(x,length): 
   print(x[length-1],end='' '') 
   rev_func(x,length-1) 
x=[11, 12, 13, 14, 15] 
rev_func(x,5) 

A - The program runs fine without error.

B - Program displays 15 14 13 12 11.

C - Program displays 11 12 13 14 15.

D - Program displays 15 14 13 12 11 and then raises an index out of range exception.

Answer : D

Explanation

In the print statement of rev_func we use the index value of list x. We decrement the value of length of function because it becomes the index of x in the print statement.

Q 8 - Select the correct function among them which can be used to write the data to perform for a binary output?

A - Write

B - Output.binary

C - Dump

D - Binary.output

Answer : C

Q 10 - Best part is you can display images in various options in Python. Select the option where you can display an image −

A - Only A label

B - Only A button and A label

C - Only A checkbox

D - A label, a check box , a button and a radio button.

Answer : D

python_questions_answers.htm
Advertisements