an HCL GUVI product

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!

Q121

Q121 Pseudocode:

Define a function that multiplies two numbers and returns the result

A

Returns sum

B

Returns product

C

No return

D

Syntax error

Q122

Q122 When using with open(file) as f, what does with do?

A

Ensures file is saved

B

Automatically closes the file

C

Opens file in write mode

D

Reads entire file

Q123

Q123 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()

Q124

Q124 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

Q125

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

A

FileNotFoundError

B

IOError

C

OSError

D

ValueError

Q126

Q126 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

Q127

Q127 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

Q128

Q128 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

Q129

Q129 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

Q130

Q130 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]

Q131

Q131 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']

Q132

Q132 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

Q133

Q133 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

Q134

Q134 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

Q135

Q135 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

Q136

Q136 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

Q137

Q137 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

Q138

Q138 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

Q139

Q139 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

Q140

Q140 Given a class Animal and a subclass Dog, which method demonstrates polymorphism?

A

Animal.speak() and Dog.speak() have different implementations

B

Dog has the same attributes as Animal

C

Dog uses a method from Animal without changing it

D

Animal has a method not present in Dog

Q141

Q141 Identify the issue in this inheritance scenario:

class Bird(Animal):
def fly(self):
pass, class Penguin(Bird):
def fly(self):
print("Can't fly")

A

Birds don't have a 'fly' method by default

B

Penguins shouldn't inherit from 'Bird'

C

fly' method in 'Penguin' contradicts 'Bird'

D

No issue present

Q142

Q142 What is a decorator in Python?

A

A function that modifies the functionality of another function

B

A function that creates new functions

C

A syntax for defining anonymous functions

D

A tool for debugging functions

Q143

Q143 What is a generator in Python?

A

A type of collection

B

A tool for creating iterators

C

A function that returns a single value

D

A module

Q144

Q144 What is a regular expression used for in Python?

A

Error handling

B

Data serialization

C

Pattern matching and text manipulation

D

Memory management

Q145

Q145 What is JSON primarily used for in Python?

A

Web development

B

Data serialization and communication between different systems

C

Numeric computations

D

File compression

Q146

Q146 When working with Excel files in Python, which library is commonly used to read and write .xlsx files?

A

Pandas

B

Numpy

C

OpenPyXL

D

CSV

Q147

Q147 Identify the error in this code for reading a CSV file:

import csv;
reader = csv.reader(open('file.csv', 'wb'))

A

The file mode should be 'rb', not 'wb'

B

csv.reader does not take a file object

C

The file 'file.csv' does not exist

D

Incorrect use of the csv module

Q148

Q148 How does the os.path.join() function benefit file path construction?

A

Increases file access speed

B

Ensures correct path format for the operating system

C

Encrypts the file path

D

Splits the file path into components

Q149

Q149 Identify the error in this code:

import os;
os.rmdir('mydir') assuming 'mydir' contains files and subfolders.

A

Incorrect module imported

B

os.rmdir() cannot delete non-empty directories

C

Wrong syntax for removing directories

D

No error

Q150

Q150 Why is PEP 8 important in Python development?

A

It provides guidelines for error handling

B

It outlines the standard coding conventions

C

It defines the core functionality of Python

D

It lists Python's reserved words