MCQ

Python MCQ and Answers – Part 3

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

1. Which of the following uses the id() function in Python?

A id() returns the object identifier

B An object does not have a unique identifier

C All the answers are true

D None of the above

A
Each object in Python has a unique identifier. The id() function returns the identifier of the object.

str = "StackHowTo"
print(id(str))    #Print 1350928023
 

2. What is the output of the following code?
def add(init = 5, *nbr, **key):
   c = init
   for n in nbr:
      c+=n
   for k in key:
      c+=key[k]
   return c
print(add(100,2,2, x=20, y=10))

A 136

B 134

C 100

D 122

B
The function adds 2, 2, 20 and 10 to the initial value 100, so the result is 134
 

3. What is the output of the following code?
import re
sentence = 'I am fine'
regex = re.compile('(?P<subject>\w+) (?P<verb>\w+) (?P<adjective>\w+)')
matched = re.search(regex, sentence)
print(matched.groupdict())

A {‘subject’: ‘I’, ‘verb’: ‘am’, ‘adjective’: ‘fine’}

B (‘I’, ‘am’, ‘fine’)

C ‘I am fine’

D ‘am’

A
This function returns a dictionary containing all the matches.
 

4. What is the output of the following code?
try: 
   list = 2*[0]+2*[5] 
   x = list[2] 
   print('OK!') 
except IndexError: 
   print('Block Except!') 
else: 
   print('Block Else!') 
finally: 
   print('Block Finally!')

A OK!

B Block Else!

C Block Finally!

D All the answers are true

D
Output:

OK!
Block Else!
Block Finally!
 

5. Suppose list1 is [2, 3, 4, 5, 1, 20, 6], what is the value of list1 after this list1.pop(1) ?

A [2, 3, 4, 5, 20, 6]

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

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

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

D
pop(i) deletes the nth element that is found at the index ‘i’ of the list.
 

6. Suppose we have two sets (s1 and s2) then what is the output of S1 + S2

A Adds the elements of the two sets.

B Removes the duplicate elements and adds the two sets.

C Impossible to perform this type of operation.

D The output will be stored in S1.

C
The + operator cannot be applied between two sets.
 

7. Are String objects mutable?

A Yes.

B No.

B
The String class is immutable. That is, we cannot modify a String object.
 

8. Python is a compiled language?

A True

B False

B
Python is an interpreted language
 

9. Can the use of parentheses change the order of evaluation?

A True

B False

A
 

10. What is the correct syntax for reading from a text file stored in “c:\file.txt”?

A f = open('c:\file.txt', 'r')

B f = open(file='c:\\\file.txt', 'r')

C f = open.file('c:\\file.txt', 'r')

D f = open('c:\\file.txt', 'r')

D
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 *