MCQ

Python MCQ and Answers – Part 2

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

1. Which function inserts an object at a given index into a list?

A list.index(obj)

B list.insert(index, obj)

C list.pop(obj=list[-1])

D list.remove(obj)

B
The insert() method inserts an element at a given index into a list. It returns nothing.

vowel = ['a', 'e', 'i', 'u']   # vowel list

vowel.insert(3, 'o')  # inserting an element in the 4th position

print(vowel)   #Print ['a', 'e', 'i', 'o', 'u']
 

2. In python, what is the correct method to load a module?

A include math

B import math

C #include math.h

D using math

B
To use functions of a module, you must import the module with the “import” instruction.
 

3. What is the output for 'python' [-3]?

A ‘h’

B ‘t’

C ‘o’

D Error: negative index.

A
Negative indexes start from the end of a string and are inverted.
 

4. What is the output of the following code: print type(type(int))?

A type ‘int’

B type ‘type’

C Error

D 0

B
The function type() returns the class of the argument to which the object belongs. type(int) returns an object of type “type”.
 

5. What is the output of the following code?
[ (a,b) for a in range(3) for b in range(a) ]

A [(1,0),(2,0),(2,1)]

B [(1,0),(2,1),(2,1)]

C [(0,0),(1,1),(2,2)]

D [(1,0),(2,1),(3,2)]  
 

A
It’s a nested loop. The output of the first “for” loop will be the value of the next loop.
 

6. What is the output of the following code?
myList = ['a','b','c','d']
print "".join(myList)

A [‘a’,’b’,’c’,’d’]

B abcd

C Null

D Error

B
“” describes a null string and the join() function combines the elements of the list into a string.
 

7. What is the output of the following code?
print(max('welcom to stackhowto.com'))

A x

B w

C y

D .

B
Python considers ‘z’ as the maximum value in a string and ‘space’ as the minimum value.
 

8. What is the output of the following code?
chr(ord('A'))

A 65

B A

C a

D Erreur

B
The ord() function converts a character to ASCII and chr() converts the ASCII character to a character.
 

9. What is the output of the following code?
x = lambda a : a + 10
print(x(5))

A 10

B 5

C 15

D Error

C
lambdas are concise functions and therefore, the result = 5 + 10 = 15
 

10. When a function is defined in a class, it is called ______?

A Module

B Class

C Method

D None of the above

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