Python Online Quiz


Advertisements

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 −

a = ['he', 'she', 'we']

' '.join(a)

A - ['heshewe']

B - 'heshewe'

C - ['he she we']

D - 'he she we'

Answer : B

Explanation

The method join() takes list of string as input and returns string as output. It removes ', ' and add the given string with join to the list.

Q 2 - What is output for −

b = [11,13,15,17,19,21]

ptint(b[::2])

A - [19,21]

B - [11,15]

C - [11,15,19]

D - [13,17,21]

Answer : C

Explanation

b[::2] :- it iterates over the list with ‘2’ increments

Q 3 - Which operator is right-associative

A - *

B - =

C - +

D - %

Answer : B

Explanation

‘=‘ operator is right associative as assignment operators are right associative.

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 - 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 happens in the below code?

class A: 
   def __init__(self, i=100): 
      self.i=i 
class B(A): 
   def __init__(self,j=0): 
      self.j=j 
def main(): 
   b= B() 
   print(b.i) 
   print(b.j) 
main() 

A - Class B inherits all the data fields of class A.

B - Class B needs an Argument.

C - The data field ‘j’ cannot be accessed by object b.

D - Class B is inheriting class A but the data field ‘i’ in A cannot be inherited.

Answer : D

Explanation

Reason being that i is initiated with self thus making it a instantiate variable of that class which cannot be inherited by the above way.

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.

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 - Which way among them is used to create an event loop ?

A - Window.eventloop()

B - Window.mainloop()

C - Window.loop()

D - Eventloop.window()

Answer : B

python_questions_answers.htm
Advertisements