logo
  • Home
  • Rank
  • Tests
  • About
  • Training
  • Programming
  • CS
  • IT
  • IS
  • ECE
  • EEE
  • EE
  • Civil
  • Mechanical
  • Chemical
  • Metallurgy
  • Instrumentation
  • Aeronautical
  • Aerospace
  • Biotechnology
  • Agriculture
  • MCA
  • BCA
  • Internship
  • Contact

Python Multiple Choice Questions | MCQs | Quiz

Python Interview Questions and Answers
Practice Python questions and answers for interviews, campus placements, online tests, aptitude tests, quizzes and competitive exams.

Get Started

•   Variable Names
•   Basic Operators
•   Core Data Types
•   Numeric Types
•   Precedence Associativity-1
•   Precedence Associativity-2
•   Bitwise - 1
•   Bitwise - 2
•   Boolean
•   Formatting - 1
•   Formatting - 2
•   Advanced Formatting
•   Decorators
•   While & For Loops - 1
•   While & For Loops - 2
•   While & For Loops - 3
•   While & For Loops - 4
•   While & For Loops - 5
•   While & For Loops - 6
•   Strings - 1
•   Strings - 2
•   Strings - 3
•   Strings - 4
•   Strings - 5
•   Strings - 6
•   Strings - 7
•   Strings - 8
•   Strings - 9
•   Strings - 10
•   Strings - 11
•   Strings - 12
•   Strings - 13
•   Lists - 1
•   Lists - 2
•   Lists - 3
•   Lists - 4
•   Lists - 5
•   Lists - 6
•   Lists - 7
•   List Comprehension
•   List Comprehension - 1
•   List Comprehension - 2
•   Matrix List Comprehension
•   Tuples - 1
•   Tuples - 2
•   Tuples - 3
•   Sets - 1
•   Sets - 2
•   Sets - 3
•   Sets - 4
•   Sets - 5
•   Dictionary - 1
•   Dictionary - 2
•   Dictionary - 3
•   Dictionary - 4
•   Built-in Functions - 1
•   Built-in Functions - 2
•   Built-in Functions - 3
•   Function - 1
•   Function - 2
•   Function - 3
•   Function - 4
•   Argument Passing - 1
•   Argument Passing - 2
•   Global vs Local Variables-1
•   Global vs Local Variables-2
•   Recursion
•   Shallow Copy vs Deep Copy
•   Functional Tools
•   Mapping Functions - 1
•   Mapping Functions - 2
•   Mapping Functions - 3
•   Python Modules
•   Math - 1
•   Math - 2
•   Math - 3
•   Datetime Module - 1
•   Datetime Module - 2
•   Random Module - 1
•   Random Module - 2
•   Sys Module
•   Operating System
•   Turtle Module - 1
•   Turtle Module - 2
•   Turtle Module - 3
•   Pickle Module
•   Regular Expressions
•   Regular Expressions - 1
•   Regular Expressions - 2
•   Regular Expressions - 3
•   Regular Expressions - 4
•   Regular Expressions - 5
•   Files - 1
•   Files - 2
•   Files - 3
•   Files - 4
•   Files - 5
•   Operator Overloading
•   Classes & Objects - 1
•   Classes & Objects - 2
•   Inheritance - 1
•   Inheritance - 2
•   Polymorphism
•   Encapsulation
•   Exception Handling - 1
•   Exception Handling - 2
•   Exception Handling - 3

Python Tests

Python Tests

Best Reference Books

Python Books

« Prev Page
Next Page »

Python Questions and Answers – Lists – 7

Posted on August 6, 2017 by Manish

This set of Python Multiple Choice Questions & Answers (MCQs) focuses on “Lists-7”.

1. What is the output of the following code?

advertisement
a=[1,2,3]
b=a.append(4)
print(a)
print(b)

a) [1,2,3,4].
[1,2,3,4].
b) [1, 2, 3, 4].
None
c) Syntax error
d) [1,2,3].
[1,2,3,4].
View Answer

Answer: b
Explanation: Append function on lists doesn’t return anything. Thus the value of b is None.

2. What will be the output when executed in python shell?

>>> a=[14,52,7]
>>>> b=a.copy()
>>> b is a

a) True
b) False
View Answer

Answer: b
Explanation: List b is just a copy of the original list. Any copy made in list b will not be reflected in list a.

3. What is the output of the following code?

a=[13,56,17]
a.append([87])
a.extend([45,67])
print(a)

a) [13, 56, 17, [87], 45, 67].
b) [13, 56, 17, 87, 45, 67].
c) [13, 56, 17, 87,[ 45, 67]].
d) [13, 56, 17, [87], [45, 67]].
View Answer

Answer: a
Explanation: The append function simply adds its arguments to the list as it is while extend function extends its arguments and later appends it.
advertisement

4. What is the output of the following piece of code?

a=list((45,)*4)
print((45)*4)
print(a)

a) 180
[(45),(45),(45),(45)].
b) (45,45,45,45).
[45,45,45,45].
c) 180
[45,45,45,45].
d) Syntax error
View Answer

Answer: c
Explanation: (45) is an int while (45,) is a tuple of one element. Thus when a tuple is multiplied, it created references of itself which is later converted to a list.

5. What is the output of the following code?

lst=[[1,2],[3,4]]
print(sum(lst,[]))

a) [[3],[7]].
b) [1,2,3,4].
c) Error
d) [10].
View Answer

Answer: b
Explanation: The above piece of code is used for flattening lists.

6. What is the output of the following code?

word1="Apple"
word2="Apple"
list1=[1,2,3]
list2=[1,2,3]
print(word1 is word2)
print(list1 is list2)

a) True
True
b) False
True
c) False
False
d) True
False
View Answer

Answer: d
Explanation: In the above case, both the lists are equivalent but not identical as they have different objects.
advertisement

7. What is the output of the following code?

def unpack(a,b,c,d):
    print(a+d)
x = [1,2,3,4]
unpack(*x)

a) Error
b) [1,4].
c) [5].
d) 5
View Answer

Answer: d
Explanation: unpack(*x) unpacks the list into the separate variables. Now, a=1 and d=4. Thus 5 gets printed.

8. What is the output of the following code?

places = ['Bangalore', 'Mumbai', 'Delhi']
<br class="blank" />places1 = places
places2 = places[:]
<br class="blank" />places1[1]="Pune"
places2[2]="Hyderabad"
print(places)

a) [‘Bangalore’, ‘Pune’, ‘Hyderabad’].
b) [‘Bangalore’, ‘Pune’, ‘Delhi’].
c) [‘Bangalore’, ‘Mumbai’, ‘Delhi’].
d) [‘Bangalore’, ‘Mumbai’, ‘Hyderabad’].
View Answer

Answer: b
Explanation: places1 is an alias of the list places. Hence, any change made to places1 is reflected in places. places2 is a copy of the list places. Thus, any change made to places2 isn’t reflected in places.

9. What is the output of the following piece of code?

x=[[1],[2]]
print(" ".join(list(map(str,x))))

a) [1] [2].
b) [49] [50].
c) Syntax error
d) [[1]] [[2]].
View Answer

Answer: a
Explanation: The elements 1 and 2 are first put into separate lists and then combined with a space in between using the join attribute.

10. What is the output of the following code?

a=165
b=sum(list(map(int,str(a))))
print(b)

a) 561
b) 5
c) 12
d) Syntax error
View Answer

Answer: c
Explanation: First, map converts the number to string and then places the individual digits in a list. Then, sum finds the sum of the digits in the list. The code basically finds the sum of digits in the number.

11. What is the output of the following code?

a= [1, 2, 3, 4, 5]
for i in range(1, 5):
    a[i-1] = a[i]
for i in range(0, 5): 
    print(a[i],end = " ")

a) 5 5 1 2 3
b) 5 1 2 3 4
c) 2 3 4 5 1
d) 2 3 4 5 5
View Answer

Answer: d
Explanation: The items having indexes from 1 to 4 are shifted forward by one index due to the first for-loop and the item of index four is printed again because of the second for-loop.

12. What is the output of the following code?

def change(var, lst):
    var = 1
    lst[0] = 44
k = 3
a = [1, 2, 3]
change(k, a)
print(k)
print(a)

a) 3
[44, 2, 3].
b) 1
[1,2,3].
c) 3
[1,2,3].
d) 1
[44,2,3].
View Answer

Answer: a
Explanation: A list is mutable, hence it’s value changes after function call. However, integer isn’t mutable. Thus its value doesn’t change.

13. What is the output of the following code?

a = [1, 5, 7, 9, 9, 1]
<br class="blank" />b=a[0]
<br class="blank" />x= 0
for x in range(1, len(a)):
    if a[x] > b:
        b = a[x]
        b= x
print(b)

a) 5
b) 3
c) 4
d) 0
View Answer

Answer: c
Explanation: The above piece of code basically prints the index of the largest element in the list.

14. What is the output of the following code?

a=["Apple","Ball","Cobra"]
<br class="blank" />a.sort(key=len)
print(a)

a) [‘Apple’, ‘Ball’, ‘Cobra’].
b) [‘Ball’, ‘Apple’, ‘Cobra’].
c) [‘Cobra’, ‘Apple’, ‘Ball’].
d) Invalid syntax for sort().
View Answer

Answer: b
Explanation: The syntax isn’t invalid and the list is sorted according to the length of the strings in the list since key is given as len.

15. What is the output of the following code?

num = ['One', 'Two', 'Three']
for i, x in enumerate(num):
    print('{}: {}'.format(i, x),end=" ")

a) 1: 2: 3:
b) Exception is thrown
c) One Two Three
d) 0: One 1: Two 2: Three
View Answer

Answer: d
Explanation: enumerate(iterator,start=0) is a built-in function which returns (0,lst[0]),(1,lst[1]) and so on where lst is a list(iterator).

Sanfoundry Global Education & Learning Series – Python.

To practice all areas of Python, here is complete set of 1000+ Multiple Choice Questions and Answers.

« Prev Page - Python Questions and Answers – Lists – 6
» Next Page - Python Questions and Answers – List Comprehension

« Python Questions and Answers – Lists – 6
Python Questions and Answers – List Comprehension »
advertisement

Deep Dive @ Sanfoundry:

  1. C++ Programming Examples on Data-Structures
  2. C Programming Examples without using Recursion
  3. Java Programming Examples on Collections
  4. C++ Programming Examples on STL
  5. Compilers Questions and Answers
  6. C Programming Examples on Data-Structures
  7. PHP Questions and Answers
  8. C Programming Examples on Linked List
  9. Python Questions and Answers
  10. Python Programming Examples
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer and SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage & Cluster Administration, Advanced C Programming, SAN Storage Technologies, SCSI Internals and Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him below:
LinkedIn | Facebook | Twitter | Google+

Best Careers

Developer Tracks
SAN Developer
Linux Kernel Developer
Linux Driver Developer
Linux Network Developer

Live Training Photos
Mentoring
Software Productivity
GDB Assignment
Sanfoundry is No. 1 choice for Deep Hands-ON Trainings in SAN, Linux & C, Kernel Programming. Our Founder has trained employees of almost all Top Companies in India such as VMware, Citrix, Oracle, Motorola, Ericsson, Aricent, HP, Intuit, Microsoft, Cisco, SAP Labs, Siemens, Symantec, Redhat, Chelsio, Cavium, ST-Micro, Samsung, LG-Soft, Wipro, TCS, HCL, IBM, Accenture, HSBC, Mphasis, Tata-Elxsi, Tata VSNL, Mindtree, Cognizant and Startups.

Best Trainings

SAN I - Technology
SAN II - Admin
Linux Fundamentals
Advanced C Training
Linux-C Debugging
System Programming
Network Programming
Linux Threads
Kernel Programming
Kernel Debugging
Linux Device Drivers

Best Reference Books

Computer Science Books
Algorithm & Programming Books
Electronics Engineering Books
Electrical Engineering Books
Chemical Engineering Books
Civil Engineering Books
Mechanical Engineering Books
Industrial Engineering Books
Instrumentation Engg Books
Metallurgical Engineering Books
All Stream Best Books

Questions and Answers

1000 C Questions & Answers
1000 C++ Questions & Answers
1000 C# Questions & Answers
1000 Java Questions & Answers
1000 Linux Questions & Answers
1000 Python Questions
1000 PHP Questions & Answers
1000 Hadoop Questions
Cloud Computing Questions
Computer Science Questions
All Stream Questions & Answers

India Internships

Computer Science Internships
Instrumentation Internships
Electronics Internships
Electrical Internships
Mechanical Internships
Industrial Internships
Systems Internships
Chemical Internships
Civil Internships
IT Internships
All Stream Internships

About Sanfoundry

About Us
Copyright
Terms
Privacy Policy
Jobs
Bangalore Training
Online Training
Developers Track
Mentoring Sessions
Contact Us
Sitemap
© 2011 Sanfoundry. All Rights Reserved.