wayground logo

Free Printable Worksheets

NEW

Font size

S
M
L
XL
Worksheets

Python Basics Worksheet — Multiple-Choice Questions

Total questions: 43

Worksheet time: 22mins

Name
Class
Date
1.

What will be the output of the following Python code? x = 'abcd' for i in range(len(x)): print(i)

a)

0 1 2 3

b)

1234

c)

a b c d

d)

error

2.

Which of the following is true for variable names in Python?

a)

none of the mentioned

b)

all private members must have leading and trailing underscores

c)

underscore and ampersand are the only two special characters allowed

d)

unlimited length

3.

To pipeline the use of functions which are present in the random library, we must use the option:

a)

import random

b)

random.h

c)

import.random

d)

random.random

4.

Which of the following is used to define a block of code in Python language?

a)

All of the mentioned

b)

Indentation

c)

Key

d)

Brackets

5.

Python supports the creation of anonymous functions at runtime, using a construct called ________.

a)

none of the mentioned

b)

lambda

c)

anonymous

d)

pi

6.

del method is used to destroy instances of a class.

a)

True

b)

False

7.

Which of the following is the use of id() function in python?

a)

None of the mentioned

b)

The id() function compares two objects.

c)

The Python id function returns the unique identity of the object

d)

Not every object in Python has a unique id

8.

What will be the output of the following Python code? f = None for i in range(5): with open("data.txt", "w") as f: if i > 2: break print(f.closed)

a)

None

b)

False

c)

Error

d)

True

9.

Which one of the following is the use of function in Python?

a)

Functions don’t provide better modularity for your application

b)

you can’t also create your own functions

c)

All of the mentioned

d)

Functions are reusable pieces of programs

10.

To read the entire remaining contents of the file as a string from a file object infile, we use ________.

a)

infile.readlines()

b)

infile.read()

c)

infile.read(2)

d)

infile.readline()

11.

Which one of the following is not a keyword in Python language?

a)

assert

b)

global

c)

pass

d)

eval

12.

What will be the value of the following Python expression? 9 + 3 % 5

a)

2

b)

1

c)

12

d)

4

13.

Which of the following is a feature of Python DocString?

a)

In Python all functions should have a docstring

b)

Docstrings can be accessed by the doc attribute on objects

c)

It provides a convenient way of associating documentation with Python modules, functions, classes, and methods

d)

All of the mentioned

14.

Which keyword is used to define a function in Python language?

a)

Function

b)

Define

c)

Fun

d)

def

15.

What will be the output of the following Python code? class A: def init(self): self.multiply(15) print(self.i) def multiply(self, i): self.i = 4 * i class B(A): def init(self): super().init() def multiply(self, i): self.i = 2 * i obj = B()

a)

60

b)

15

c)

30

d)

An exception is thrown

16.

Which of these statements about a dictionary is false?

a)

Dictionaries aren’t ordered

b)

The values of a dictionary can be accessed using keys

c)

Dictionaries are mutable

d)

The keys of a dictionary can be accessed using values

17.

What will be the output of the following Python code snippet? a = [ppl-ai-file-upload.s3.amazonaws] i = 2 for i not in a: print(i) i += 1

a)

None of the mentioned

b)

Error

c)

0

d)

-2 -1

18.

Which of the following is the correct extension of the Python file?

a)

.py

b)

.pl

c)

.python

d)

.p

19.

Which of the following is the truncation division operator in Python?

a)

%

b)

//

c)

|

d)

/

20.

What will be the output of the following Python code? a, b = 6, 7 a, b = b, a a, b

a)

Invalid syntax

b)

Nothing is printed

c)

(7,6)

d)

(6,7)

21.

What will be the output of the following Python code? def foo(): try: return 1 finally: return 2 k = foo() print(k)

a)

3

b)

1

c)

2

d)

error, there is more than one return statement in a single try-finally block

22.

What will be the output of the following Python code? names1 = ['Amir', 'Bala', 'Charlie'] names2 = [name.lower() for name in names1] print(names2)

a)

None

b)

b

c)

a

d)

c

23.

Which function overloads the == operator?

a)

equ()

b)

eq()

c)

none of the mentioned

d)

isequal()

24.

What will be the output of the following Python code? class A(): def disp(self): print("A disp()") class B(A): pass obj = B() obj.disp()

a)

Nothing is printed

b)

Invalid syntax for inheritance

c)

A disp()

d)

Error because when object is created, argument must be passed

25.

Which of the following statements are true?

a)

When you open a file for writing, if the file exists, the existing file is overwritten with the new file

b)

When you open a file for writing, if the file does not exist, a new file is created

c)

When you open a file for reading, if the file does not exist, an error occurs

d)

All of the mentioned

26.

Which of the following functions is a built-in function in Python?

a)

sqrt()

b)

print()

c)

factorial()

27.

What type does the variable have when the following code line is executed? x = 2 * 3.0

a)

str

b)

bool

c)

float

d)

int

28.

What will be the output of the following Python code? class tester: def __init__(self, id): self.id = str(id) id = "224" temp = tester(12) print(temp.id)

a)

None

b)

error

c)

12

d)

224

29.

What will be the output of the following Python code? class A: def __init__(self, x = 1): self.x = x class B(A): def __init__(self, y = 2): super().__init__() self.y = y def main(): obj = B() print(obj.x, obj.y) main()

a)

2 1

b)

1 2

c)

The program runs fine but nothing is printed

d)

Error, the syntax of the invoking method is wrong

30.

All keywords in Python are in ________.

a)

lower case

b)

Capitalized

c)

UPPER CASE

d)

None of the mentioned

31.

What will be the output of the following Python program? i = 0 while i < 5: print(i) i += 1 if i == 3: break else: print(0)

a)

0 1 2

b)

0 1 2 0

c)

none of the mentioned

d)

error

32.

Is the following Python code valid? a = 2, 3, 4, 5 a

a)

No, too many values to unpack

b)

Yes, (2,3,4,5) is printed

c)

Yes, 2 is printed

d)

Yes, is printed

33.

What type of inheritance is illustrated in the following Python code? class A(): pass class B(): pass class C(A, B): pass

a)

Single-level inheritance

b)

Multi-level inheritance

c)

Hierarchical inheritance

d)

Multiple inheritance

34.

Is Python case sensitive when dealing with identifiers?

a)

yes

b)

none of the mentioned

c)

no

d)

machine dependent

35.

Which of the following statements about properties is true?

a)

The @property decorator allows you to define getter, setter, and deleter methods for attributes.

b)

Properties are useful for validating data, computing attributes, and creating read-only or read-write attributes.

c)

You use properties when you need controlled access or want to encapsulate logic without changing the API.

d)

All of the mentioned.

36.

In ____________________ copy, the modification done on one list affects the other list. In ____________________ copy, the modification done on one list does not affect the other list.

a)

memberwise shallow

b)

deep shallow

c)

deep memberwise

d)

shallow deep

37.

How many except statements can a try-except block have?

a)

one

b)

more than zero

c)

zero

d)

more than one

38.

Which type of Programming does Python support?

a)

object-oriented programming

b)

all of the mentioned

c)

structured programming

d)

functional programming

39.

What is the order of precedence in Python?

a)

Exponential, Parentheses, Division, Multiplication, Addition, Subtraction

b)

Parentheses, Exponential, Multiplication, Addition, Division, Subtraction

c)

Parentheses, Exponential, Multiplication, Division, Addition, Subtraction

d)

Exponential, Parentheses, Multiplication, Division, Addition, Subtraction

40.

What will be the output of the following Python code? def add(c, k): c.test = c.test + 1 k = k + 1 class A: def __init__(self): self.test = 0 def main(): Count = A() k = 0 for i in range(25): add(Count, k) print("Count.test=", Count.test) print("k =", k) main()

a)

Exception is thrown

b)

Count.test=25 k=25

c)

Count.test=25 k=0

d)

Count.test=0 k=0

41.

Which of the following is not a core data type in Python programming?

a)

Class

b)

List

c)

Dictionary

d)

Tuple

42.

_____ is used to create an object.

a)

constructor (the __init__ method)

b)

class

c)

User-defined functions

d)

In-built functions

43.

Which of the following character is used to give single-line comments in Python?

a)

#

b)

//

c)

/*

d)

!