wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Python Programming Quiz-03

Total questions: 23

Worksheet time: 25mins

Name
Class
Date
1.

Q-1. What will be the output of the following Python code?

a=[1,2,3,4,5,6,7,8,9]

print(a[::2])

a)

[1,2]

b)

[8,9]

c)

[1,3,5,7,9]

d)

[1,2,3]

2.

Q-2. The code below manipulates a Python list using the slicing operator. What will be the output?

a=[1,2,3,4,5,6,7,8,9]

a[::2]=10,20,30,40,50,60

print(a)

a)

ValueError: Attempt to assign a sequence of size 6 to an extended slice of size 5

b)

[10, 2, 20, 4, 30, 6, 40, 8, 50, 60]

c)

[1, 2, 10, 20, 30, 40, 50, 60]

d)

[1, 10, 3, 20, 5, 30, 7, 40, 9, 50, 60]

3.

Q-5. What is the correct command to shuffle the given Python list?

fruit=['apple', 'banana', 'papaya', 'cherry']

a)

fruit.shuffle()

b)

shuffle(fruit)

c)

random.shuffle(fruit)

d)

random.shuffleList(fruit)

4.

The code below uses a Python list and iterates using the range() function. What will be the output?

arr = [1, 2, 3, 4, 5, 6]

for i in range(1, 6):

arr[i - 1] = arr[i]

for i in range(0, 6):

print(arr[i], end = " ")

a)

1 2 3 4 5 6

b)

2 3 4 5 6 1

c)

1 1 2 3 4 5

d)

2 3 4 5 6 6

5.

Draw the flow chart diagram to print the swap two numbers without using intermediate/

temporary variables and also take the input from users.

6.

Make the use of a python program that accepts a sequence of

lines as input and prints the line after making all characters in the

sentence capitalized.

Input: “Hello GL BAJAJ..

Now we start to learn Python Programming Language”.

Output:

HELLO GL BAJAJ..

NOW WE START TO LEARN PYTHON PROGRAMMING

LANGUAGE.

7.

What will be the output of the following code snippet?

fruit_list1 = ['Apple', 'Berry', 'Cherry', 'Papaya']

fruit_list2 = fruit_list1

fruit_list3 = fruit_list1[:]

fruit_list2[0] = 'Guava'

fruit_list3[1] = 'Kiwi'

sum = 0

for ls in (fruit_list1, fruit_list2, fruit_list3):

if ls[0] == 'Guava':

sum += 1

if ls[1] == 'Kiwi':

sum += 20 print (sum)

a)

22

b)

21

c)

0

d)

43

8.

What will be the output of the following Python coding snippet?

init_tuple_a = 'a', 'b'

init_tuple_b = ('a', 'b')

print (init_tuple_a == init_tuple_b)

a)

0

b)

1

c)

false

d)

true

9.

Which of the following statements given below is/are true about a Python tuple?

a)

Tuples have structure, and lists have an order.

b)

Tuples are homogeneous, and lists are heterogeneous.

c)

Tuples are immutable, and lists are mutable.

d)

All of them.

10.

What will be the output of the following Python code?

init_tuple = ((1, 2),) * 7

print(len(init_tuple[3:8]))

a)

Exception

b)

5

c)

4

d)

None

11.

What is the output of the below Python code?

a = {(1,2):1,(2,3):2}

print(a[1,2])

a)

Key Error

b)

1

c)

{(2,3):2}

d)

{(1,2):1}

12.

What will be the output of the following Python coding snippet?

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

print (a['a','b'])

a)

Key Error

b)

[1,2]

c)

{‘a’:1,’b’:2}

d)

(1,2)

13.

The code below has a Python function writing to a dictionary. What will it print in the end?

fruit = {}

def addone(index):

if index in fruit:

fruit[index] += 1

else:

fruit[index] = 1

addone('Apple')

addone('Banana')

addone('apple')

print (len(fruit))

a)

1

b)

2

c)

3

d)

4

14.

What will be the output of the following Python coding snippet?

arr = {}

arr[1] = 1

arr['1'] = 2

arr[1] += 1

sum = 0

for k in arr:

sum += arr[k]

print (sum)

a)

1

b)

2

c)

3

d)

4

15.

What will be the output of the following code snippet?

my_dict = {}

my_dict[(1,2,4)] = 8

my_dict[(4,2,1)] = 10

my_dict[(1,2)] = 12

sum = 0

for k in my_dict:

sum += my_dict[k]

print (sum)

print(my_dict)

a)

Syntax error

b)

30
{(1, 2): 12, (4, 2, 1): 10, (1, 2, 4): 8}

c)

47
{(1, 2): 12, (4, 2, 1): 10, (1, 2, 4): 8}

d)

30
{[1, 2]: 12, [4, 2, 1]: 10, [1, 2, 4]: 8}

16.

What will the following Python dictionary code print in its result?

box = {}

jars = {}

crates = {}

box['biscuit'] = 1

box['cake'] = 3

jars['jam'] = 4

crates['box'] = box

crates['jars'] = jars

print (len(crates[box]))

a)

1

b)

3

c)

4

d)

Type Error

17.

What will be the result of the below Python code?

rec = {"Name" : "Python Programmer", "Age":"20", "Addr" : "NJ", "Country" : "USA"}

id1 = id(rec)

del rec

rec = {"Name" : "Python Programmer", "Age":"20", "Addr" : "NJ", "Country" : "USA"}

id2 = id(rec)

print(id1 == id2)

a)

TRUE

b)

FALSE

c)

1

d)

EXEPTION

18.

Which of the following concepts is the most relevant in Python?

a)

function invocation in Python

b)

pass-by-value in Python

d)

pass by object reference in Python

19.

What is the output of the following Python code? Will it behave differently from the previous question?

def test(x = 1, y = 2):

x = x + y

y += 1

print(x, y)

test(2, 1)

a)

1 3

b)

2 3

c)

3 2

d)

3 3

20.

Which of the following function calls can be used to invoke the below function definition?

def test(a, b, c, d)

a)

test(1, 2, 3, 4)

b)

test(a = 1, 2, 3, 4)

c)

test(a = 1, b = 2, c = 3, 4)

d)

test(a = 1, b = 2, c = 3, d = 4)

e)

test(1, 2, 3, d = 4)

21.

What is the output of the following code snippet?

def test(a, b=5, c=10):

print('a is', a, 'and b is', b, 'and c is', c)

test(3, 7)

test(25, c = 24)

test(c = 50, a = 100)

a)

a is 7 and b is 3 and c is 10
a is 25 and b is 5 and c is 24
a is 5 and b is 100 and c is 50

b)

a is 3 and b is 7 and c is 10
a is 5 and b is 25 and c is 24
a is 50 and b is 100 and c is 5

c)

a is 3 and b is 7 and c is 10
a is 25 and b is 5 and c is 24
a is 100 and b is 5 and c is 50

d)

None of the above

22.

What is the output of the following code snippet?

def func(x = 1, y = 2):

return x + y, x - y

x, y = func(y = 2, x = 1)

print(x, y)

a)

1 3

b)

3 1

c)

The program has a runtime error because the function returns the multiple values

d)

3 -1

e)

-1 3

23.

What is the order of usage of  args, *kwargs, and formal args in the function header?

a)

some_func(formal_args, args, *kwargs)

b)

some_func(**kwargs, *args, formal_args)

c)

some_func(*args, **kwargs, formal_args)

d)

some_func(*args, formal_args, **kwargs)