wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Python 2A

Total questions: 80

Worksheet time: 41mins

Name
Class
Date
1.

Which of these is the best description of a list in Python?

a)

A list is a collection of data that has an order and can be changed

b)

A list is a lot of variables

c)

I have no idea

d)

A list is a collection of data that cannot hold duplicated data and cannot be changed

2.

Which of these is the correct code for creating a list of names?

a)

nameList = John, Harry, Jesse, John, Harry, Harry

b)

nameList = ("John", "Harry", "Jesse", "John", "Harry", "Harry")

c)

nameList = ["John", "Harry", "Jesse", "John", "Harry", "Harry"]

d)

nameList = [John, Harry, Jesse, John, Harry, Harry]

3.

What would this out put? print(users[0][0])

a)

smith

b)

ben

c)

sam

d)

james

4.

The list needs one more name added to the end - "Marina". Which piece of code below would do this?

nameList = ["John", "Harry", "Jesse", "John", "Harry", "Harry"]

a)

nameList.append(Marina)

b)

append(nameList,"Marina")

c)

nameList.append["Marina",7]

d)

nameList.append("Marina")

e)

nameList.append("Marina", "Marina", "Marina", "Marina", "Marina", "Marina")

5.

What would this out put? print(users[2][1])

a)

error

b)

16

c)

brown

d)

smith

e)

something

6.

Which of the following methods is used to add an element to the end of a list in Python?

a)

`insert()`

b)

`append()`

c)

`extend()`

d)

`add()`

7.

What will be the output of the following code snippet? ```python my_list = [1, 2, 3] my_list.insert(1, 4) print(my_list) ```

a)

`[1, 4, 2, 3]`

b)

`[1, 2, 3, 4]`

c)

`[4, 1, 2, 3]`

d)

`[1, 2, 4, 3]`

8.

Which of the following operations will remove all elements from a set in Python?

a)

`set.clear()`

b)

`set.remove()`

c)

`set.discard()`

d)

`set.delete()`

9.

What is the result of the following set operation? ```python set1 = {1, 2, 3} set2 = {3, 4, 5} result = set1.union(set2) ```

a)

`{1, 2, 3, 4, 5}`

b)

`{3}`

c)

`{1, 2, 3}`

d)

`{4, 5}`

10.

Which of the following data types is immutable in Python?

a)

List

b)

Set

c)

Tuple

d)

Dictionary

11.

What is the result of the following code? ```python my_set = {1, 2, 3} my_set.add(2) print(my_set) ```

a)

`{1, 2, 3, 2}`

b)

`{1, 2, 3}`

c)

`{1, 3}`

d)

`{2, 3}`

12.

Which of the following is a mutable data type in Python?

a)

String

b)

Tuple

c)

List

d)

Integer

13.

How can you create a set with the elements 1, 2, and 3 in Python?

a)

`set = {1, 2, 3}`

b)

`set = [1, 2, 3]`

c)

`set = (1, 2, 3)`

d)

`set = {1: 2, 3}`

14.

What will be the output of the following code? ```python my_tuple = (1, 2, 3) my_tuple[0] = 4 ```

a)

`(4, 2, 3)`

b)

`(1, 2, 3)`

c)

`TypeError`

d)

`(4, 1, 2, 3)`

15.

What is the primary characteristic that differentiates a list from a set in Python?

a)

Lists are ordered and sets are unordered

b)

Lists are immutable and sets are mutable

c)

Lists do not allow duplicates and sets do

d)

Lists use key-value pairs and sets do not

16.

Which data structure would be most suitable for storing a collection of unique tags for a blog post?

a)

List

b)

Set

c)

Tuple

d)

Dictionary

17.

Which of the following data structures allows for the storage of duplicate items?

a)

Set

b)

Dictionary

c)

Tuple

d)

All of the above

18.

What would be the output of the following code? tags = {'python', 'coding', 'tutorial', 'python'}

a)

{'python', 'coding', 'tutorial', 'python'}

b)

{'python', 'coding', 'tutorial'}

19.

What is the main benefit of using a set over a list in Python?

4 lines
20.

Given the coordinates (10.0, 20.0), which data structure is most appropriate for storing them?

a)

List

b)

Set

c)

Tuple

d)

Dictionary

21.

What will happen if you try to change an item in a tuple?

a)

The item will be changed

b)

An error will be raised

c)

The item will be removed

d)

The tuple will convert to a list

22.

Which of the following is a correct example of a list in Python?

a)

to_do = ('buy groceries', 'write report', 'call mum')

b)

to_do = ['buy groceries', 'write report', 'call mum']

c)

to_do = {'buy groceries', 'write report', 'call mum'}

d)

to_do = {'buy groceries': 1, 'write report': 2, 'call mum': 3}

23.

If you need a collection of items that must remain in the order you add them, which data structure would you use?

a)

Set

b)

Dictionary

c)

List

d)

All of the above

24.

Can a list contain elements of different data types?

a)

No

b)

Yes

c)

Only if they are immutable

d)

Only if they are mutable

25.

What kind of data structure is best suited for representing RGB colour values?

a)

List

b)

Set

c)

Tuple

d)

Dictionary

26.

Which data structure is most appropriate for storing a list of attendees where each attendee should only be listed once?

a)

List

b)

Set

c)

Tuple

d)

Dictionary

27.

List items have an index number. In the following list, which item has the index number of 3?

["John", "Harry", "Jesse", "John", "Harry", "Harry"]

a)

"John"

b)

"Harry"

c)

"Jesse"

28.

Which of these pieces of code would return the name "Harry" from the following list?

nameList = ["John", "Harry", "Jesse", "John", "Harry", "Harry"]

a)

nameList()

b)

nameList[1]

c)

NameList(4)

d)

nameList["4"]

29.

The list needs one more name added to the end - "Felipe". Which piece of code below would do this?

nameList = ["John", "Harry", "Jesse", "John", "Harry", "Harry"]

a)

nameList.append(Felipe)

b)

append(nameList,"Felipe")

c)

nameList.append["Felipe",7]

d)

nameList.append("Felipe")

30.
What output will this code produce?
a)
blue
b)
green
c)
red
d)
error
31.

Consider this list, what will be the highest index the list can have:

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

a)

2

b)

3

c)

4

d)

5

32.

The count() method returns the number of times the specified element appears in the list.

What is the output of this code?

a)

3

b)

2

c)

1

d)

0

33.

Output of this code?

a)

[2,4,6,8,10]

b)

['2','4','6','8','10']

c)

[0,2,4,6,8]

d)

[10,8,6,4,2]

34.

What is the output of this code?

a)

X

b)

Y

c)

Error

35.

Which of the following function is used to count the number of elements in a list ?

a)

count( )

b)

find( )

c)

len( )

d)

index( )

36.

mylist = ["a","b"]

mylist[1] = "c"'

print(mylist)

a)

["a","c"]

b)

["a","b"]

c)

["a","b","c"]

d)

mylist

37.

What would the output of the program be?

thisList = ["square", "circle", "triangle", "octagon"]

if "triangle" in thisList:

print("Yes, 'triangle' is in the list")

else:

print("No triangle here!")

a)

Yes, 'triangle' is in the list

b)

"No triangle here!"

38.

In a list called "players" if you wanted to delete the item with an index of 3 which of the following examples of code would be correct?

a)

players.pop[3]

b)

players.delete(3)

c)

players.remove(3)

d)

players.pop(3)

39.
What output will this code produce?
a)
Exeter
b)
4
c)
6
d)
city
40.

What will line 3 OUTPUT?

a)

3

b)

2

c)

TRUE

d)

FALSE

41.

list1 = ["hello", "hey", "hi"]

print(list1)

How many items will be printed?

a)

1

b)

2

c)

3

d)

0

42.

Which of these is the best description of a list in Python?

a)

A list is a collection of data that has an order and can be changed

b)

A list is a lot of variables

c)

A list is used for shopping

d)

A list is a collection of data that cannot hold duplicated data and cannot be changed

43.

What will be the output of the following Python code? *

x={1,3,5}

y={2,4,6}

x+y

a)

{1,3,5,2,4}

b)

{1,3,5,2,4,6}

c)

Error as the duplicate item 6 is present in both sets

d)

Error as unsupported operand type(s) for +: 'set' and 'set'

44.

Which of the following functions will return the symmetric difference between two sets, x and y? *

a)

x | y

b)

x ^ y

c)

x & y

d)

x – y

45.

What will be the output of the following Python code?

x = {6,5}

y = {2,3,5,6}

x<y

a)

{1,2}

b)

True

c)

False

d)

Invalid operation

46.

The _________ function removes the first element of a set and the last element of a list.

a)

remove

b)

pop

c)

discard

d)

dispose

47.

If x={5,6,7,8}, which of the following statements is false?

a)

print(len(x))

b)

print(min(x))

c)

x.remove(5)

d)

x[2]=45

48.

One of the following functions is not used with Python String

a)

Split

b)

Remove

c)

print

d)

Replace

49.

Choose the correct syntax of the split function:

a)

x.split[]

b)

split(x)

c)

x.split(",", 3)

d)

X.SPLIT(",", 3)

50.

Choose the correct syntax of the replace function:

a)

x.Replace(x)

b)

x.replace(y)

c)

x.replace(3,x)

d)

x.replace("j","3")

51.

Choose the correct code to print line of 10 stars using repetition operation:

a)

print("**********")

b)

print("*"*10)

c)

print("*"^10)

d)

print("*"+10)

52.

The correct syntax for converting the string x to uppercase is :

a)

X.upper()

b)

upper(x)

c)

x.UPPER[ ]

d)

x.upper()

53.

The correct syntax for converting the string myname to lowercase is :

a)

myNAME.lower[ ]

b)

myname.lower( )

c)

myname.upper( )

d)

myname_lower(12 )

54.

To remove the spaces present at start and end of the string, you can use

a)

split()

b)

len()

c)

strip()

d)

remove()

55.

Let x= "Python", the output of print(x.upper()) is

a)

python

b)

PYTHON

c)

_PYTHON

d)

"PYTHON"

56.

Let x= "python is a high level language", the output of print(x[5].replace("n","*")) is

a)

pytho* is a high level language

b)

python is a high level language

c)

*

d)

Error

57.

Let x= "python is a high level language", the output of print(len(x[-1])) is

a)

1

b)

31

c)

Error

d)

25

58.

Let x= "Python is a high level language", the output of print(x.replace(x[-3],"3")) is

a)

Error

b)

Python is 3 high level l3ngu3ge

c)

Python is a high l3v3l languag3

d)

Python is a high level language

59.

Let x= "Python is a high level language", the output of print(x[7].upper()) is

a)

Python is a high level language

b)

PYTHON IS A HIGH LEVEL LANGUAGE

c)

I

d)

S

60.

Let x= "Python is a high level language", the output of print(x[2].upper()+x[3].lower()+x[-1]) is

a)

THE

b)

The

c)

ThE

d)

the

61.

Let x= "Python is a high level language", the output of print(x[-4].upper()+x[8]+x[-1]) is

a)

Use

b)

Age

c)

USE

d)

Error

62.

Let x= "Python is a high level language", the output of

print(x.split("i", 2))

a)

['Pyth', 'n is a high level language']

b)

['Python is ', ' high level l', 'nguage']

c)

['Python ', 's a h', 'gh level language']

d)

Error

63.

Let x= "Python", the output of

print(x[0])

print(x[1])

print(x[2])

a)

PYT

b)

P

y

t

c)

T

h

o

d)

n

oh

64.

Let x= "Python", the output of

print(x[0])

print(x[-1])

print(x[-2])

a)

P

N

O

b)

p

O

n

c)

t

h

y

d)

P

n

o

65.

How to create a single element tuple

a)

(1,)

b)

(1)

c)

{1}

d)

[1]

e)

tuple([1])

66.

Result of the program above is

a)

('1', '2', '3', '4', '5')

b)

(1, 2, 3, 4, 5)

c)

12345

d)

"12345"

67.

What is the output of the program above

a)

<class 'tuple'>

b)

<class 'None'>

c)

<class 'list'>

d)

<class 'int'>

68.

What is the output of the code above

a)

(1, '2', 3, 4, 5)

b)

syntax error

c)

(1, 2, 3, 4, 5)

d)

('1', '2', '3', '4', '5')

69.

Which of the following is true

a)

a is assigned a value of 1

b)

b is assigned a value of [2,3]

c)

b is assigned a value of (2,3)

d)

b is assigned a value of 2

70.

What is the output of the program above ?

a)

b is bigger

b)

a is bigger

c)

Syntax error : tuples can't be compared

71.

What is the output of the program above ?

a)

True

b)

False

72.

Which function is used to return the count of a specific element in a tuple?

a)

D. count()

b)

C. index()

c)

B. min()

d)

A. max()

73.

How can a tuple be indirectly modified in Python?

a)

C. Using tuple unpacking

b)

A. Using the del statement

c)

D. Using the remove() method

d)

B. Using the append() method

74.

What is the result of the expression: (10,20,30)[1]?

a)

D. Error

b)

C. 30

c)

B. 20

d)

A. 10

75.

What is the result of the expression: (10,20,30,40,50,60,70,80,90)[2:7:2]?

a)

A. (30, 40, 50)

b)

B. (30, 50, 70)

c)

D. (30, 40, 60)

d)

C. (30, 50, 90)

76.

Which function is used to return the maximum value from a tuple in Python?

a)

D. find_max()

b)

C. maximum_value()

c)

B. max()

d)

A. maximum()

77.

 

What will be the output of below Python code?

tuple1=(5,1,7,6,2)

tuple1.pop(2)

print(tuple1)

a)

(5,1,6,2)

b)

(5,1,7,6)

c)

(5,1,7,6,2)

d)

Error

78.

What will be the output of below Python code?

tuple1=(2,4,3)

tuple3=tuple1*2

print(tuple3)

a)

(4,8,6)

b)

(2,4,3,2,4,3)

c)

(2,2,4,4,3,3)

d)

Error

79.

What will be the output of below Python code?

tupl=([2,3],"abc",0,9)

tupl[0][1]=1

print(tupl)

a)

([2,3],"abc",0,9)

b)

([1,3],"abc",0,9)

c)

([2,1],"abc",0,9)

d)

Error

80.

Find the output of the given Python program?

t = (5,4,3,2,1)

x = sum(t,2)

print(x)

a)

15

b)

17

c)

30

d)

Error