wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Intro to Programming Quiz 2

Total questions: 10

Worksheet time: 5mins

Name
Class
Date
1.

Which of the following methods adds an element to the end of a list?

a)
append()
b)
push()
c)
extend()
d)
insert()
2.

What does the following code return?

my_list = [10, 20, 30, 40, 50]

print(my_list[1:4])

a)
[30, 40]
b)
[20, 30, 40]
c)
[20, 30, 40, 50]
d)
[10, 20, 30]
3.

What will be the output of the following code?

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

print(t['b'])

a)

Key Error

b)
1
c)

3

d)
2
4.

How can you add a new key-value pair to a dictionary?

a)

a) my_dict.add('d', 4)

b)

b) my_dict['d'] = 4

c)

c) my_dict.update({'d': 4})

d)

d) Both b and c

5.

What will be the output of the following code?

for i in range(3):

print(i)

a)

0

  1. 1

  2. 2

  3. 3

b)
4
c)
5
d)
0 1 2
6.

Which of the following is true about the while loop?

a)
The while loop only executes the code block once
b)
The while loop checks the condition after executing the block of code
c)
The while loop is used for iterating over a fixed range of values
d)
The while loop continues to execute a block of code as long as the specified condition is true.
7.

Which of the following is true about the for loop?

a)
A for loop can only be used with integers
b)
A for loop can only execute code once
c)

A for loop is a control flow statement that allows code to be executed repeatedly based on a fixed number of iterations.

d)
A for loop can only iterate in reverse order
8.

What will be the output of the following code?

my_list = [1, 2, 3]

my_list[3] = 4

print(my_list)

a)

[1, 2, 3, 4]

b)

[1, 2, 3, None]

c)

[1, 2, 3]

d)
IndexError
9.

What will be the output of the following code?

my_list = [0, 1] * 2

print(my_list)

a)
[0, 1, 0, 1]
b)

[0, 2]

c)
[0, 0, 1, 1]
d)
[1, 0, 1, 0]
10.

What will be the output of the following code?

in python

for i in range(5):

if i == 2:

break

print(i)

else:

print('Completed')

a)

0 1 2 Completed

b)

0 1 Completed

c)
3, 4
d)
0, 1