wayground logo

Free Printable Worksheets

NEW

Font size

S
M
L
XL
Worksheets

Python: Built-In Functions and Upto Tuple

Total questions: 50

Worksheet time: 16mins

Name
Class
Date
1.

What will print([1, 2, 3] + [4, 5]) output?

a)

[1, 2, 3, 4, 5]

b)

[5, 4, 3, 2, 1]

c)

Error

d)

[1, 2, 3, [4, 5]]

2.

Which of the following will create a shallow copy of a list a?

a)


b = a[:]

b)


b = a.copy()

c)


b = a[:]
and
b = a.copy()
both options correct

d)


None of the above

3.

What will print(len([[1, 2], [3, 4]])) return?

a)

2

b)

4

c)

8

d)

Error

4.

Which of the following will raise an error for tuple a?

a)


a = (1, 2)

b)


a[0] = 3

c)


len((1, 2, 3))

d)


(1, 2) + (3,)

5.

What is the output of print((1, 2) * 2)?

a)


(1, 2, 1, 2)

b)


(1, 2, 2)

c)


Error

d)


[1, 2, 1, 2]

6.

What will the following code output?
for i in range(3):
 ↳ for j in range(2):
 ↳ print(i * j, end=" ")

Note:  ↳ indicating that the statement is the part of its above block

a)

0 0 0 1 0 2

b)


0 0 0 0 0 1

c)


0 0 0 1

d)


0 0 0 0 1 2

7.

Error Correction: Fix the code to break the loop:
i = 0

while True:

↳print(i)

↳if i == 5:

↳break

Note:  ↳ indicating that the statement is the part of its above block

a)

Add i += 1 before break

b)

Use for instead of while

c)

Add i += 1 after print(i)

d)


Replace break with continue

8.

What will be the output of the following code?
s = "hello"
print(s[-1:-5:-1])

a)


olleh

b)

hello

c)


olle

d)


Error

9.

What is the result of
''.join(sorted("cabac"))?

a)


cabac

b)


aabcc

c)


abacc

d)


Error

10.

Which of the following results in a valid deep copy of a list?

a)


b = a.copy()

b)

b = copy.deepcopy(a)

c)

b = a[:]

d)

b = list(a)

11.

What is the output of print((1, 2) * 0)?

a)


()

b)


(1, 2)

c)


[]

d)


Error

12.

What will be the result of the following code?
t = (1, [2, 3], 4)
t[1][0] = 0
print(t)

a)


(1, [2, 3], 4)

b)

(1, [0, 3], 4)

c)

Error

d)


(1, 2, 3, 4)

13.

What will the following code do?

x = 0
while x < 3:

↳print(x, end=" ")

↳x += 1

else:

↳print("Done")

Note:  ↳ indicating that the statement is the part of its above block

a)


0 1 2

b)


0 1 2 Done

c)


Done

d)


Error

14.

What will the following code output?
result = [x**2 for x in range(5) if x % 2 == 0]
print(result)

a)

[0, 1, 4, 9, 16]

b)


[0, 4, 16]

c)


[1, 9]

d)


Error

15.

What does the following code produce?
result = [x + y for x in "abc" for y in "12"]
print(result)

a)

['a1', 'a2', 'b1', 'b2', 'c1', 'c2']

b)


['a1b2', 'c1']

c)


['a1', 'b2', 'c1', 'c2']

d)


Error

16.

Which of the following comprehensions creates a list of tuples (x, y) where x is odd and y is even for x, y ∈ range(4)? Consider 0 in Even.
A. [(x, y) for x in range(4) for y in range(4) if x % 2 == 1 and y % 2 == 0]

B. [(x, y) for x in range(4) if x % 2 == 1 for y in range(4) if y % 2 == 0]

C. [(x, y) for y in range(4) if y % 2 == 0 for x in range(4) if x % 2 == 1]

a)

A

b)

B

c)

C

d)


All of the above

17.

What will print([x for x in range(10) if x < 5 if x % 2 == 0]) output?

a)


[0, 1, 2, 3, 4]

b)


[0, 2, 4]

c)

[0, 4]

d)

Error

18.

What will print([x if x % 2 == 0 else -x for x in range(5)]) output?

a)


[0, 1, 2, 3, 4]

b)


[0, -1, 2, -3, 4]

c)

[0, -1, -2, -3, -4]

d)


Error

19.

What does this code output?
result = [[x * y for x in range(3)] for y in range(3)]
print(result)

a)

[[0, 0, 0], [0, 1, 2], [0, 2, 4]]

b)

[[0, 0, 0], [1, 2, 3], [4, 5, 6]]

c)


[[0, 1, 2], [1, 2, 3], [2, 3, 4]]

d)


Error

20.

What will be the output of the following code?
result = [x for x in range(10) if x in [y for y in range(5, 15)]]

print(result)

a)


[5, 6, 7, 8, 9]

b)

[0, 1, 2, 3, 4]

c)


[10]

d)


Error

21.

What does this comprehension produce?

result = [x + y for x, y in zip(range(3), range(3, 6))]

print(result)

a)


[3, 5, 7]

b)

[3, 4, 5]

c)

[4, 5, 6]

d)


Error

22.

What does the del keyword do in Python?

a)

Deletes variables and objects from memory

b)

Deletes keys from dictionaries

c)

Deletes elements from a list

d)


All of the above

23.

Which of the following statements about Python keywords is true?

a)


Python keywords can be used as variable names

b)


The "is" keyword compares identity, not equality

c)

The def keyword is used for declaring classes

d)

The break keyword is used to skip iterations in loops

24.

print(sum([1, 2, 3, 4], 5))

a)

10

b)

15

c)

20

d)

TypeError

25.

What does the sorted() function return?

a)

A sorted list

b)

A sorted tuple

c)

A sorted set

d)

A new list in reverse order

26.

x = "print(10+5)"

exec(x)

a)

15

b)

'10+5'

c)

Error

d)

None

27.

What does the eval() function do?

a)

Compiles Python code

b)

Executes a string as a Python expression

c)

Converts string to integer

d)

Reads input

28.

What does the zip() function return?

a)

A list

b)

A dictionary

c)

A zip object

d)

A tuple

29.

Which of the following functions returns an enumerator object?

a)

enumerate()

b)

sorted()

c)

zip()

d)

map()

30.

What will be the output of abs(-7.5)?

a)

-7.5

b)

7.5

c)

7

d)

None

31.

What does the all() function return for an empty list?

a)

False

b)

True

c)

None

d)

Error

32.

What does the any() function return for an empty list?

a)

False

b)

True

c)

None

d)

Error

33.

What does the any() function return for an iterable containing only False values?

a)

True

b)

False

c)

None

d)

Error

34.

What will be the output of round(4.675, 2)?

a)

4.67

b)

4.68

c)

4.675

d)

4.66

35.

What will be the output of min("Banana")?

a)

a

b)

n

c)

B

d)

Error

36.

What does ord("MAYANK SAXENA".split()[1][-1:-2:-1]) return?

a)

Error

b)

List can't treat as String

c)

65

d)

78

37.

What does chr(sorted(tuple([65, 70, 78, 97, 122, 90]))[-1]-32) return?

a)

Error

b)

-32 is negative so it will not be acceptable

c)

'z'

d)

'Z'

38.

What does bool([]) return?

a)

True

b)

False

c)

Error

d)

None

39.

What will be the output of pow(2, 3, 5)?

a)

8

b)

3

c)

5

d)

Error

40.

What will be the output of divmod(10, 3)?

a)

(3, 1)

b)

(3, 3)

c)

(3, 0)

d)

Error

41.

What will be the output of bin(10)

a)

'0b10'

b)

'0b1010'

c)

'10'

d)

10

42.

What does map(str, [1, 2, 3]) return?

a)

['1', '2', '3']

b)

('1', '2', '3')

c)

A map object

d)

None

43.

What is the result of bool("0")?

a)

True

b)

False

c)

0

d)

None

44.

What is the result of bool(" ")?

a)

True

b)

False

c)

None

d)

Error

45.

Which function is used to apply a function to each item of an iterable?

a)

filter()

b)

map()

c)

reduce()

d)

zip()

46.

for i, v in enumerate(['apple', 'banana'], start=1):

print(i, v)

a)

0 apple

1 banana

b)

1 apple

2 banana

c)

apple 0

banana 1

d)

apple 1

banana 2

47.

Which of the following best describes the purpose of enumerate()?

a)

It generates indices only.

b)

It returns the original list unchanged.

c)

It pairs each element with an index.

d)

It sorts the list by indices.

48.

What is the output of the following code?

print(list(zip([1, 2, 3], ['a', 'b'])))

a)

[(1, 2, 3), ('a', 'b', 'c')]

b)

[(1, 'a'), (2, 'b')]

c)

[[1, 'a'], [2, 'b'], [3, 'c']]

d)

{1: 'a', 2: 'b', 3: 'c'}

49.

How does zip() handle iterables of different lengths?

a)

Raises an error

b)

Fills missing values with None

c)

Truncates to the length of the shortest iterable

d)

Repeats the shorter iterable

50.

Given the following code snippet, what is the value of variable b?
t = (1, 2, 3, 4, (5, 6, 7))

a, *b, (c, d, e) = t

a)

[1, 2, 3, 4]

b)

[2, 3, 4]

c)

(2, 3, 4)

d)

[(2, 3, 4)]