MCQ

Python MCQ and Answers – Part 10

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

1. Which of the following statements will create a list?

A l = list()

B l = []

C l = list([1, 2, 3])

D All the answers are true

D
 

2. What is the output of the following code?
class MyClass: 
    def __init__(self, id): 
        self.id = id
        id = 20 
  
o = MyClass(10) 
print o.id

A 20

B 10

C None

D Error

B
Instantiating the “MyClass” class automatically calls the __init__ method and passes the object as the self parameter. 10 is assigned to the data attribute of the object called id.
 

3. What is the output, if set1 = {1, 2, 3}?
set1.issubset(set1)

A True

B False

C Error

D None of the above

A
Each set is a subset of itself.
 

4. What is the output of the following code?
True = False
while True:
    print(True)
    break

A True

B False

C Error

D None

C
True is a keyword and its value cannot be changed.
 

5. Suppose t = (1, 2, 4, 4), which of the following statements is incorrect?

A print(max(t))

B print(t[2])

C print(len(t))

D t[2] = 3

D
Values cannot be changed in case of a tuple, i.e. tuple is immutable.
 

6. What is the output of the following code?
d = {"alex":30, "bob":35}
d["alex"]

A 30

B 35

C Error

D None

A
 

7. What is the output of the following code?
i = 1
while False:
    if i%2 == 0:
        break
    print(i)
    i += 2

A 1

B 1 3 5 7 …

C 2 4 6 8 …

D None of the above

D
The control does not go into the loop because of “False”.
 

8. What is the output when we run list(“hello”)?

A [‘olleh’]

B [‘llo’]

C [‘hello’]

D [‘h’, ‘e’, ‘l’, ‘l’, ‘o’]  
 

D
 

9. What is the output of the following code?
class Point:
  
    def __init__(self, x = 0, y = 0):
      self.x = x
      self.y = y
  
    def __sub__(self, other):
        x = self.x + other.x
        y = self.y + other.y
        return Point(x,y)
        
point1 = Point(30, 40)
point2 = Point(10, 20)
point3 = point1 - point2
print(point3.x, point3.y)

A 20 30

B 40 60

C 10 20

D 20 40

B
 

10. What is the output of the following code?
>>>a=("Hello")*3
>>>a

A (‘Hello’,’Hello’,’Hello’)

B The operator * not valid for tuples

C (‘HelloHelloHello’)

D Syntax error

C
Here (“Hello”) is a string and not a tuple because there is no comma after the element.
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 *