Thanks to visit codestin.com
Credit goes to www.tutorialspoint.com

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 is invalid in python for z = 5 ?

A - z = z++

B - z = ++z

C - z += 1

D - z -= 1

Answer : A

Explanation

z = z++ is not valid in python, it is not a legal expression. It results in syntax error.

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

import math
 
   def main():
      math.cos(math.pi)
main()
   print(main())

A - -1

B - None

C - Error

D - Math.pi not defined

Answer : B

Explanation

None is printed because function does not return the value.

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 - Which of the function among will return 4 on the set s = {3, 4, 1, 2}?

A - Sum(s)

B - Len(s)

C - Max(s)

D - Four(s)

Answer : B & C.

Explanation

len(s) returns the length of the set and max(s) returns the maximum value in the set.

Q 8 - Which function can be used on the file to display a dialog for saving a file?

A - Filename = savefilename()

B - Filename = asksavefilename()

C - Fielname = asksaveasfilename()

D - No such option in python.

Answer : C

Explanation

This is the default method to display a dialog for saving a file in Tkinter module of Python.

Answer : B

Explanation

Text is created in the canvas by using create_text method of canvas. Color of the text can be set according to the user which is specified under filled'.

python_questions_answers.htm
Advertisements