wayground logo

Free Printable Worksheets

NEW

Font size

S
M
L
XL
Worksheets

Python Programming II CSE A

Total questions: 20

Worksheet time: 7mins

Name
Class
Date
1.

What will be the output of the following code? x = 10 if x > 5: x += 2 else: x -= 2 print(x)

a)

10

b)

12

c)

8

d)

7

2.

Which of the following is correct about Python functions?

a)

A function is defined using the def keyword

b)

A function can return multiple values

c)

A function can be passed as an argument to another function

d)

All of the above

3.

What will be the output of the following code? s = 'Hello' s = s[::-1] print(s)

a)

olleH

b)

Hello

c)

oHell

d)

lleHo

4.

Which of the following operations can be used to add an element to a list?

a)

append()

b)

extend()

c)

insert()

d)

All of the above

5.

What will be the result of the following code? l = [1, 2, 3, 4] l[1:3] = [7, 8] print(l)

a)

[1, 7, 8, 4]

b)

[1, 2, 3, 4]

c)

[1, 7, 4]

d)

Error

6.

What will be the output of the following code? my_set = {1, 2, 3, 4} my_set.add(2) print(my_set)

a)

{1, 2, 3, 4}

b)

{1, 2, 3}

c)

{1, 2, 3, 4, 2}

d)

Error

7.

Which method is used to remove a key from a dictionary and return its value?

a)

del

b)

remove()

c)

pop()

d)

discard()

8.

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

a)

(4, 2, 3)

b)

(1, 2, 3)

c)

Error

d)

None

9.

What is the output of the following code? x = [1, 2, 3] y = x y.append(4) print(x)

a)

[1, 2, 3]

b)

[1, 2, 3, 4]

c)

[1, 2]

d)

[4, 2, 3]

10.

Which of the following is true about Python sets?

a)

They are unordered

b)

They are immutable

c)

They allow duplicate elements

d)

They are not iterable

11.

What is the output of the following code? x = ['ab', 'cd'] for i in x: x.append(i.upper()) print(x)

a)

Infinite loop

b)

['ab', 'cd', 'AB', 'CD']

c)

Error

d)

None

12.

Which of the following is not a valid dictionary method?

a)

keys()

b)

values()

c)

append()

d)

get()

13.

What will be the output of the following code? x = 'python' x[0] = 'j' print(x)

a)

jython

b)

python

c)

Error

d)

None

14.

What is the output of the following code? def myfunc(x): return [i ** 2 for i in x] print(myfunc([1, 2, 3]))

a)

[1, 4, 9]

b)

[1, 2, 3]

c)

[1, 8, 27]

d)

Error

15.

What will be the result of the following code? my_dict = {1: 'a', 2: 'b', 3: 'c'} print(list(my_dict.keys()))

a)

[a, b, c]

b)

[1, 2, 3]

c)

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

d)

Error

16.

Which of the following statements is true about tuples?

a)

They are mutable

b)

They can contain elements of different types

c)

They allow duplicate elements

d)

Both B and C

17.

What will be the output of the following code? l = [1, 2, 3, 4] print(l[::2])

a)

[1, 2, 3, 4]

b)

[1, 3]

c)

[2, 4]

d)

[1, 4]

18.

Which of the following can be used to iterate over a dictionary's keys and values at the same time?

a)

items()

b)

keys()

c)

values()

d)

None of the above

19.

What will be the output of the following code? def f(x=[]): x.append(1) print(x) f() f()

a)

[1] [1]

b)

[1] [1, 1]

c)

[1] Error

d)

None

20.

What will be the output of the following code? my_tuple = (1, 2) print(my_tuple * 2)

a)

(1, 2)

b)

(1, 2, 1, 2)

c)

Error

d)

None