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

S = [['him', 'sell'], [90, 28, 43]]

S[0][1][1]

A - 'e'

B - 'i'

C - '90'

D - 'h'

Answer : A

Explanation

List can contain other list values.

So, in this question S[0] gives ['him', 'sell'], S[0][1] gives ‘sell' and S[0][1][1] gives ‘e'.

Remember, the index in python starts with ‘0'.

Q 2 - What is output for following code −

y = [4, 5,1j]
y.sort()

A - [1j,4,5]

B - [5,4,1j]

C - [4,5,1j]

D - Type Error

Answer : D

Explanation

we cannot compare complex numbers with numbers, so the output results in type error stating that complex numbers cannot be compared with <,>,=.

Q 3 - Name the error that doesn't cause program to stop/end, but the output is not the desired result or is incorrect.

A - Syntax error

B - Runtime error

C - Logical error

D - All of the above

Answer : C

Explanation

logical error doesn't cause the program to stop/end, but the output is not the desired result or is incorrect.

Q 4 - what is output of following code −

class Count:
   def __init__(self, count=0):
      self.__count=count
a=Count(2)
b=Count(2)
print(id(a)==id(b), end = '' '')

c= ''hello''
d= ''hello''
print(id(c)==id(d))

A - True False

B - False True

C - False False

D - True True

Answer : B

Explanation

The objects with same contents share the same object in the python library but this is not true for custom-defined immutable classes.

Q 5 - What command is used to insert 6 in a list ‘‘L'' at 3rd position ?

A - L.insert(2,6)

B - L.insert(3,6)

C - L.add(3,6)

D - L.append(2,6)

Answer : A

Explanation

listname.insert(x,y) method is used to insert a item at certain position in a list. x defines position at which the element will be added and y defines the element to be added in the list.

Q 6 - Guess the output −

def main(): 
   try: 
      func() 
      print(''print this after function call'') 
   except ZeroDivisionError: 
      print('Divided By Zero! Not Possible! ') 
   except: 
      print('Its an Exception!') 
def func(): 
   print(1/0) 
main()

A - ‘Its an Exception!'

B - ‘Divided By Zero! Not possible!'

C - ‘print this after function call' followed by ‘Divided By Zero! Not Possible!'

D - ‘print this after function call' followed by ‘Its an Exception!'

Answer : B

Explanation

The function ‘func' will not run because it contains an exception. So in try and expect block. The function called under try will not run and will move to except block which defines the type of exception present in the function ‘func'. Thus block of statements present in except ZeroDivisionError is printed.

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.

Answer : C

Explanation

For the type of message user want to display on the window of python there is a type of code according to it. Example for warning message user choose showwarning method.

Answer : C

Explanation

Button keyword is used to make a button in Python. To set the process button in command we assign the value processButton in the command. Any text value can be given to the button which is assigned under text keyword.

Q 10 - Which module is used in python to create Graphics?

A - Turtle

B - Canvas

C - Tkinter

D - Graphics

Answer : A

python_questions_answers.htm
Advertisements