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!

Q31

Q31 How do you import a module in Python?

A

using the import keyword

B

using the include keyword

C

using the require keyword

D

using the module keyword

Q32

Q32 What is the difference between arguments and parameters in Python functions?

A

No difference

B

Parameters define, arguments call

C

Arguments define, parameters call

D

Based on data type

Q33

Q33 What is the output of this code:

def add(x, y):
return x + y;
print(add(3, 4))

A

3

B

4

C

7

D

Error

Q34

Q34 Pseudocode:

Function that checks if a number is positive, negative, or zero

A

Prints type of number

B

Returns type of number

C

No output

D

Error

Q35

Q35 Identify the error:

def my_func(x, y):
return x + y;
my_func(1)

A

Missing argument

B

Syntax error

C

Logic error

D

No error

Q36

Q36 Find the mistake:

import math;
print(math.sqr(4))

A

Incorrect module

B

Incorrect function name

C

Wrong number of arguments

D

No error

Q37

Q37 What is the purpose of the open() function in Python?

A

To read data from URL

B

To open a new Python file

C

To open a file

D

To create a Python module

Q38

Q38 Which file mode in Python allows you to read and write to a file?

A

r+

B

w+

C

a+

D

rb+

Q39

Q39 What does the following code do?

with open('file.txt', 'w') as file:
file.write("Hello World")

A

Reads from file.txt

B

Writes "Hello World" to file.txt

C

Deletes file.txt

D

Creates a new file

Q40

Q40 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'

Q41

Q41 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

Q42

Q42 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

Q43

Q43 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

Q44

Q44 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

Q45

Q45 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

Q46

Q46 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

Q47

Q47 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

Q48

Q48 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

Q49

Q49 What is the output of the following lambda expression?

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

A

7

B

12

C

0

D

SyntaxError

Q50

Q50 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

Q51

Q51 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

Q52

Q52 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

Q53

Q53 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

Q54

Q54 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

Q55

Q55 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

Q56

Q56 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

Q57

Q57 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

Q58

Q58 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

Q59

Q59 Which module in Python provides support for regular expressions?

A

sys

B

re

C

os

D

json

Q60

Q60 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'