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 How does polymorphism enhance code reusability?

A

By allowing methods to perform different tasks based on the object

B

By using the same method in multiple classes

C

By changing the functionality of inherited methods

D

By encapsulating code in modules

Q122

Q122 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

Q123

Q123 Pseudocode:

Create a base class 'Shape', derived classes 'Circle', 'Square'; Implement method 'area' in both

A

Error, as 'area' cannot be implemented in 'Shape'

B

Circle' and 'Square' can't inherit from 'Shape'

C

area' method behaves differently in 'Circle' and 'Square'

D

'Shape' should not have an 'area' method

Q124

Q124 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

Q125

Q125 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

Q126

Q126 How do decorators contribute to cleaner code in Python?

A

By reducing the need for global variables

B

By providing a syntax for error handling

C

By allowing code reuse and adding functionality to existing code

D

By optimizing memory usage

Q127

Q127 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

Q128

Q128 How do generators differ from standard functions?

A

Generators can't take arguments

B

Generators don't return values

C

Generators yield values, maintaining state between each yield

D

Generators execute faster

Q129

Q129 What is the main difference between threading and multiprocessing in Python?

A

Threading is faster

B

Multiprocessing uses more memory

C

Threading involves parallel execution

D

Multiprocessing involves separate memory spaces for each process

Q130

Q130 When should you choose multiprocessing over threading in Python?

A

For I/O-bound tasks

B

For quick tasks

C

For CPU-bound tasks

D

When memory usage is a concern

Q131

Q131 What is a regular expression used for in Python?

A

Error handling

B

Data serialization

C

Pattern matching and text manipulation

D

Memory management

Q132

Q132 Which module in Python provides support for regular expressions?

A

sys

B

re

C

os

D

json

Q133

Q133 What does the regular expression ^a...s$ match?

A

Any seven-letter string starting with 'a' and ending with 's'

B

Any string starting with 'a' and ending with 's'

C

Any five-letter string starting with 'a' and ending with 's'

D

Any string containing 'a' and 's'

Q134

Q134 What will the following Python code return?

re.search(r"\d{3}", "The code 123 is valid")

A

'123'

B

<re.Match object; span=(9, 12), match='123'>

C

None

D

'The code 123 is valid'

Q135

Q135 Identify the issue in this regex:

re.findall(r"[A-Z+]", "Python+C+Java")

A

Incorrect use of escape character

B

Wrong pattern for matching uppercase letters

C

'+' should not be escaped

D

No issue

Q136

Q136 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

Q137

Q137 In Python, which module is used to work with CSV files?

A

csv

B

json

C

excel

D

fileio

Q138

Q138 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

Q139

Q139 What does the following code do?

import json; json.loads('{"name": "John", "age": 30}')

A

Creates a JSON file

B

Parses a JSON string into a Python dictionary

C

Serializes a Python dictionary to JSON

D

Throws an error

Q140

Q140 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

Q141

Q141 What is the purpose of a virtual environment in Python programming?

A

Version control

B

To isolate project-specific dependencies

C

Web development

D

Data analysis

Q142

Q142 Which tool is used to install Python packages?

A

git

B

virtualenv

C

pip

D

setuptools

Q143

Q143 What is the key benefit of using requirements.txt in a Python project?

A

For documenting the code

B

To list all the packages and their versions used in the project

C

To improve the performance of the project

D

For version control

Q144

Q144 What is the primary use of the OS module in Python?

A

Networking

B

Web development

C

Interacting with the operating system

D

Data analysis

Q145

Q145 Which function in the OS module is commonly used to change the current working directory?

A

os.change_directory()

B

os.chdir()

C

os.setcwd()

D

os.update_directory()

Q146

Q146 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

Q147

Q147 What does os.listdir('.') do?

A

Lists all Python files in the current directory

B

Creates a new directory

C

Lists all files and directories in the current directory

D

Deletes the current directory

Q148

Q148 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

Q149

Q149 What is PEP in the context of Python?

A

A Python enhancement proposal

B

A Python error protocol

C

A Python environment path

D

A Python executable program

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

ad vertical
ad