MCQ

Python MCQ and Answers – Part 12

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

1. What is the output of the following code?
>>>names = ['Alex', 'Bob', 'Yohan', 'Thomas']
>>>print(names[-1][-1])

A Alex

B Thomas

C s

D Error

C
 

2. What is the output of the following code?
x = [i**+1 for i in range(3)]; 
print(x);

A [1, 2, 5]

B [0, 1, 2]

C Error, ‘;’ is not authorised

D Error, **+ is not a valid operator

B
i**+1 evaluates to (i)**(+1).
 

3. Is the following code valid?
>>>a=2,3,4,5
>>>a

A Yes, 2 is displayed

B No, too many values

C Yes, [2,3,4,5] is displayed

D Yes, (2,3,4,5) is displayed

D
A tuple does not have to be enclosed in parentheses.
 

4. Suppose that list1 is [1, 2, 3], so what is the output of list1 * 2?

A [2, 4, 6]

B [1, 2, 3, 1, 2]

C [1, 2, 3, 1, 2, 3]

D [1, 2, 3, 3, 2, 1]  
 

C
 

5. What is the output of the following code?
>>>x,y=1,2
>>>x,y=y,x
>>>x,y

A (1,2)

B (2,1)

C Nothing is displayed

D Invalid Syntax

B
 

6. What is the output of the following code hex(14)?

A 0xe

B 0Xe

C 0xE

D e

A
The hex() function is used to convert the given argument to its hexadecimal representation, in lowercase.
 

7. What is the output of the following code?
x={1:"X",2:"Y",3:"Z"}
print(x.get(1,4))

A 1

B 4

C X

D Invalid Syntax

C
The get() method returns the value of the key if it is in the dictionary and the default value (second parameter) if the key is not in the dictionary.
 

8. What is the output of the following code?
print((1, 2) + (3, 4))

A ((1, 2), (3, 4))

B (1, 2, 3, 4)

C (4, 6)

D Error!

B
 

9. To concatenate two strings in Python we use _______?

A . operator

B + operator

C ^ operator

D strcat() function

B
 

10. What is the output of the following code?
str = "{1}, {2} and {0}".format('Alex', 'Yohan', 'Bob')
print(str)

A Alex, Yohan and Bob

B Yohan, Bob and Alex

C Alex, Yohan

D Yohan, Bob

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