logo
  • Home
  • 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
Pratice 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

Best Reference Books

Python Books

« Prev Page
Next Page »

Python Questions and Answers – While and For Loops – 4

Posted on August 6, 2017 by staff10

This set of Python Questions and Answers for Freshers focuses on “While and For Loops”.

1. What is the output of the following?

x = 123
for i in x:
    print(i)

a) 1 2 3
b) 123
c) error
d) none of the mentioned
View Answer

Answer: c
Explanation: Objects of type int are not iterable.

2. What is the output of the following?

d = {0: 'a', 1: 'b', 2: 'c'}
for i in d:
    print(i)

a) 0 1 2
b) a b c
c) 0 a 1 b 2 c
d) none of the mentioned
View Answer

Answer: a
Explanation: Loops over the keys of the dictionary.

3. What is the output of the following?

d = {0: 'a', 1: 'b', 2: 'c'}
for x, y in d:
    print(x, y)

a) 0 1 2
b) a b c
c) 0 a 1 b 2 c
d) none of the mentioned
View Answer

Answer: d
Explanation: Error, objects of type int aren’t iterable.

4. What is the output of the following?

d = {0: 'a', 1: 'b', 2: 'c'}
for x, y in d.items():
    print(x, y)

a) 0 1 2
b) a b c
c) 0 a 1 b 2 c
d) none of the mentioned
View Answer

Answer: c
Explanation: Loops over key, value pairs.

5. What is the output of the following?

d = {0: 'a', 1: 'b', 2: 'c'}
for x in d.keys():
    print(d[x])

a) 0 1 2
b) a b c
c) 0 a 1 b 2 c
d) none of the mentioned
View Answer

Answer: b
Explanation: Loops over the keys and prints the values.

6. What is the output of the following?

d = {0: 'a', 1: 'b', 2: 'c'}
for x in d.values():
    print(x)

a) 0 1 2
b) a b c
c) 0 a 1 b 2 c
d) none of the mentioned
View Answer

Answer: b
Explanation: Loops over the values.

7. What is the output of the following?

d = {0: 'a', 1: 'b', 2: 'c'}
for x in d.values():
    print(d[x])

a) 0 1 2
b) a b c
c) 0 a 1 b 2 c
d) none of the mentioned
View Answer

Answer: d
Explanation: Causes a KeyError.

8. What is the output of the following?

d = {0, 1, 2}
for x in d.values():
    print(x)

a) 0 1 2
b) None None None
c) error
d) none of the mentioned
View Answer

Answer: c
Explanation: Objects of type set have no attribute values.

9. What is the output of the following?

d = {0, 1, 2}
for x in d:
    print(x)

a) 0 1 2
b) {0, 1, 2} {0, 1, 2} {0, 1, 2}
c) error
d) none of the mentioned
View Answer

Answer: a
Explanation: Loops over the elements of the set and prints them.

10. What is the output of the following?

d = {0, 1, 2}
for x in d:
    print(d.add(x))

a) 0 1 2
b) 0 1 2 0 1 2 0 1 2 …
c) None None None
d) None of the mentioned
View Answer

Answer: c
Explanation: Variable x takes the values 0, 1 and 2. set.add() returns None which is printed.

11. What is the output of the following?

for i in range(0):
    print(i)

a) 0
b) no output
c) error
d) none of the mentioned
View Answer

Answer: b
Explanation: range(0) is empty.

Sanfoundry Global Education & Learning Series – Python.

To practice all questions on Python for freshers, here is complete set of 1000+ Multiple Choice Questions and Answers.

« Prev Page - Python Questions and Answers – While and For Loops – 3
» Next Page - Python Questions and Answers – While and For Loops – 5
« Python Questions and Answers – While and For Loops – 3
Python Questions and Answers – While and For Loops – 5 »

Deep Dive @ Sanfoundry:

  1. C# Programming Examples on Delegates
  2. Ruby Programming Questions and Answers
  3. C# Programming Examples on Arrays
  4. Javascript Questions and Answers
  5. C# Basic Programming Examples
  6. C Questions and Answers
  7. C# Programming Examples on Mathematics
  8. PHP Questions and Answers
  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
TOS & Privacy
Jobs
Bangalore Training
Online Training
SAN Training
Developers Track
Mentoring Sessions
Contact Us
Sitemap
© 2011 Sanfoundry