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

Answer : D

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 - How can we swap two numbers a = 10, b = 20 in python without using third variable?

A - a = b

b = a

B - a,b = b,a

C - both a & b

D - b = a

a = b

Answer : C

Explanation

To swap two numbers we can use both a & b option. Both a & b are similar statemnts written in different ways.

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 −

def func(n):
   if(n==1):
      return 1;
   else:
      return(n+func(n-1))
print(func(4))

A - 12

B - 10

C - 9

D - 11

Answer : B

Q 5 - What is output of following −

print('any'.encode())

A - ‘any’

B - ‘yan’

C - b’any’

D - x’any’

Answer : C

Explanation

encode() returns bytes object.

Q 6 - Analyze the given below code?

class Demo: 
   def __init__(self,d): 
      self.d=d 
   def print(self): 
      print(d)  
a = Demo(''Hello'') 
a.print()

A - You cannot use print(self) as a function name.

B - Program has an error because class A does not have a constructor.

C - Program will print ‘Hello’ if we change print(d) to print(self.d).

D - Syntax Error.

Answer : C

Explanation

D stores the argument value here. Thus when we call the class ‘‘Hello’’ is passed as an argument which is stored in the variable d.

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 - 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.

python_questions_answers.htm
Advertisements