python banner

Python Multiple Choice Questions (MCQs) and Answers

Master Python Programming with Practice MCQs. Explore our curated collection of Multiple Choice Questions. Ideal for placement and interview preparation, our questions range from basic to advanced, ensuring comprehensive coverage of Python Programming. Begin your placement preparation journey now!

Q91

Q91 What is the result of this code?

f = open('file.txt', 'r');
print(f.readline())

A

Prints entire file

B

Prints the first line of file.txt

C

Error

D

Prints 'file.txt'

Q92

Q92 Pseudocode:

Open 'data.txt', read each line, print each line

A

Prints name of the file

B

Prints contents of data.txt

C

Error

D

No output

Q93

Q93 Identify the error:

file = open('data.txt', 'r');
data = file.read();
file.close()

A

File not closed properly

B

Incorrect mode for reading

C

No error

D

Error in file.read()

Q94

Q94 Find the mistake:

with open('data.txt', 'r') as file:
data = file.readlines();
print(data[2])

A

Wrong function used

B

Error in print statement

C

Index error

D

No error

Q95

Q95 In Python, what is the purpose of the try statement?

A

To test a block of code for errors

B

To execute code regardless of errors

C

To generate an error

D

To declare variables

Q96

Q96 Which exception is raised by Python when a file you try to open does not exist?

A

FileNotFoundError

B

IOError

C

OSError

D

ValueError

Q97

Q97 When is the else clause in a try-except block executed?

A

Always after the try block

B

When no exceptions are raised

C

When an exception is handled

D

It is never executed

Q98

Q98 What will be the output of the following code?

try: x = 1 / 0 except ZeroDivisionError:
print("Error")

A

0

B

1

C

Error

D

No output

Q99

Q99 Pseudocode:

Try to open a file, read its contents;
if file not found,
print error message

A

File contents are displayed

B

Error message is printed

C

No output

D

File is created and then read

Q100

Q100 Identify the error in this code:

try:
x = 1 / 0
except:
print("An error occurred")

A

Division by zero is not handled properly

B

Generic exception should not be used

C

Syntax error in try statement

D

No error

Q101

Q101 Find the mistake: try:
x = 1 / "0"
except ValueError:
print("Type error occurred")

A

Wrong exception type is caught

B

Should be ZeroDivisionError

C

It will raise a TypeError

D

No error

Q102

Q102 What is a list comprehension in Python?

A

A concise way to create lists

B

A method to iterate through lists

C

A type of Python comprehension

D

A special function for lists

Q103

Q103 What is the key difference between list comprehensions and generator expressions?

A

The syntax used

B

The type of brackets used

C

List comprehensions produce lists, while generator expressions produce generators

D

Execution speed

Q104

Q104 What will this list comprehension produce:

[x**2 for x in range(5)]

A

[0, 1, 2, 3, 4]

B

[1, 4, 9, 16, 25]

C

[0, 1, 4, 9, 16]

D

[1, 2, 3, 4, 5]

Q105

Q105 What does the following dictionary comprehension do?

{x: x*x for x in range(5)
if x%2 == 0}

A

Squares of all numbers from 0 to 4

B

Squares of odd numbers from 0 to 4

C

Squares of even numbers from 0 to 4

D

Cubes of even numbers from 0 to 4

Q106

Q106 Pseudocode:

Create a list of lowercase letters from a list of mixed case letters, 'A', 'b', 'C', 'd', 'E'

A

['A', 'b', 'C', 'd', 'E']

B

['a', 'b', 'c', 'd', 'e']

C

['A', 'C', 'E']

D

['b', 'd']

Q107

Q107 Identify the error in this code:

[x for x in range(10)
if x%2 = 0]

A

Syntax error in range function

B

Incorrect use of assignment operator

C

List comprehension is incorrectly formed

D

No error

Q108

Q108 What is a lambda expression in Python?

A

A one-time anonymous function

B

A named, reusable function

C

A loop structure

D

A data type

Q109

Q109 How is a lambda function different from a regular function defined using def in Python?

A

Lambda can only have one expression

B

Lambda can return multiple values

C

Lambda functions are faster

D

Lambda functions can't have arguments

Q110

Q110 What is the output of the following lambda expression?

(lambda x, y: x * y)(3, 4)

A

7

B

12

C

0

D

SyntaxError

Q111

Q111 Pseudocode:

Define a lambda to square a number, use it to get the square of 5

A

Returns 25

B

Returns 10

C

Syntax error

D

No output

Q112

Q112 Identify the error in this lambda expression:

lambda x, y:
if x > y
return x
else
return y

A

Incorrect syntax for if-else

B

Lambda function cannot use conditional

C

Lambda cannot return values

D

No error

Q113

Q113 What is a class in Python?

A

A blueprint for creating objects

B

A function inside an object

C

A variable inside an object

D

A specific object instance

Q114

Q114 In Python, what is 'self' in a class method?

A

A variable that holds the class name

B

A reference to the class itself

C

A reference to the instance that calls the method

D

A placeholder for future arguments

Q115

Q115 How is a class attribute different from an instance attribute in Python?

A

Class attributes are shared by all instances

B

Instance attributes can't be changed

C

Class attributes can't be accessed by instances

D

No difference

Q116

Q116 What is polymorphism in the context of object-oriented programming?

A

Changing the functionality of methods in subclasses

B

Creating multiple classes that inherit from a single class

C

The ability of different object types to be accessed through the same interface

D

Splitting a class into multiple smaller classes

Q117

Q117 Given the class definition:

class Dog:
def init(self, name):
self.name = name,
what does Dog('Buddy').name return?

A

Dog

B

name

C

Buddy

D

An error occurs

Q118

Q118 Pseudocode:

Define a class 'Car'
with a method 'drive';
Create an instance of 'Car';
Call 'drive' method

A

Error as 'drive' method is undefined

B

The 'drive' method of the 'Car' instance is executed

C

Nothing as 'Car' has no attributes

D

A new 'Car' object is created

Q119

Q119 Identify the error in this code:

class Animal:
def init(self, name):
name = name

A

Incorrect use of the constructor

B

name should be 'self.name'

C

The class should have more methods

D

No error

Q120

Q120 What is inheritance in object-oriented programming?

A

Sharing data between classes

B

Copying methods from one class to another

C

Deriving new classes from existing classes

D

Protecting data within a class

ad vertical
ad