MCQ

Python MCQ and Answers – Part 15

This collection of Python Multiple Choice Questions and Answers (MCQs): Quizzes & Practice Tests with Answer.
 

1. Only problems defined recursively can be solved using recursion

A True

B False

B
Many other problems can also be solved by using recursion.
 

2. Which of the following statements is wrong about recursion?

A Recursive function can be replaced by a non-recursive function

B Recursive functions are faster than non-recursive functions

C Recursive functions generally take more memory than non-recursive functions

D Recursion makes programs easier to understand

B
The speed of a program using recursion is slower than the speed of its non-recursive equivalent.
 

3. What is the output of the following code?
list1 = [1, 2, 3, 4] 
list1.extend([5, 6])
print(list1)

A [1, 2, 3, 4]

B [1, 2, 3, 4, 5, 6]

C [1, 2, 3, 4, 5]

D [5, 6, 1, 2, 3, 4]  
 

B
extend(), is used to merge two lists.
 

4. What is the output of the following code?
list1 = [1, 2, 3, 4] 
list1.pop(1)
print(list1)

A [2, 3, 4]

B [1, 2, 3]

C [4, 3, 2]

D [1, 3, 4]  
 

D
The pop() method takes a single argument (index) and removes the element present at this index.
 

5. What is the output of the following code?
list1 = [1, 2, 3, 4] 
list1.pop()
print(list1)

A [2, 3, 4]

B [1, 2, 3]

C [4, 3, 2]

D [1, 3, 4]  
 

B
The parameter passed to the pop() method is optional. If no parameter is passed, the default index -1 is given as an argument that returns the last element.
 

6. What is the output of the following code?
>>>"Welcome to StackHowTo".split()

A “Welcome”, “to”, “StackHowTo”

B {“Welcome”, “to”, “StackHowTo”}

C (“Welcome”, “to”, “StackHowTo”)

D [“Welcome”, “to”, “StackHowTo”]  
 

D
The split() function returns the items as a list.
 

7. What is the output of the following code?
>>>list("w#x#y#z".split('#'))

A [‘wxyz’]

B [‘w x y z’]

C [‘w’, ‘x’, ‘y’, ‘z’]

D [‘w#x#y#z’]  
 

C
 

8. Complete the following code to calculate the factorial of a number.
def fact(n):
    if n == 0: 
        return 1
    else:
        return ........

A fact(n)*fact(n-1)

B n*fact(n-1)

C n*(n-1)

D (n-1)*(n-2)

B
 

9. What is the purpose of setattr()?

A To set an attribute

B To access the object attribute

C To check if an attribute exists or not

D Delete an attribute

A
setattr(obj, name, value) is used to set an attribute. If the attribute does not exist, it will be created. Let’s take an example :

class Person:
  name = "Alex"
  age = 25
  country = "Schrway"

setattr(Person, 'age', 30)
 

10. Which methods start and end with two underscores “__”?

A Special methods

B Integrated methods

C Additional methods

D User defined methods

A
Example: __init__
 

11. Which of these fields is a private data field?
def MyClass:
def __init__(self):
    __x = 1
    self.__y = 1
    self.__z__ = 1
    __t__= 1

A __x

B __y

C __z__

D __t__

B
Variables such as self.__y are private members of the class.
mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *