wayground logo

Free Printable Worksheets

NEW

Font size

S
M
L
XL
Worksheets

Chapter 6 - Part Two: How to Work with Lists and Tuples

Total questions: 39

Worksheet time: 20mins

Name
Class
Date
1.

What does the add_to_list() function do in the given code?

The add_to_list() function

def add_to_list(list, item):

list.append(item) # list object changed

The calling code in the main() function

# list object created

inventory = ["staff", "hat", "bread"]

 

add_to_list(inventory, "robe")

print(inventory) # ["staff", "hat", "bread", "robe"]

 

# NOTE: no need to return list object

a)

Adds an element to the end of a list

b)

Removes an element from a list

c)

Sorts the list in ascending order

d)

Clears all elements from the list

2.

Why is there no need to return the list object in the add_to_list() function?

The add_to_list() function

def add_to_list(list, item):

list.append(item) # list object changed

The calling code in the main() function

# list object created

inventory = ["staff", "hat", "bread"]

 

add_to_list(inventory, "robe")

print(inventory) # ["staff", "hat", "bread", "robe"]

 

# NOTE: no need to return list object

a)

Because the list is immutable.

b)

Because the list is passed by reference.

c)

Because the function automatically returns the list.

d)

Because the list is a global variable.

3.

Based on the hierarchy chart for the Movie List program, which of the following is not a function listed under 'main'?

a)

display menu

b)

list movies

c)

add movie

d)

search movie

4.

What is the purpose of the 'display_menu' function in the Movie List program?

a)

To add a new movie to the list

b)

To display the list of movies

c)

To show the available options to the user

d)

To delete a movie from the list

5.

Which command in the 'display_menu' function is used to add a movie?

a)

list

b)

add

c)

del

d)

exit

6.

How does the 'list' function work in the Movie List program?

a)

It adds a new movie to the list.

b)

It removes a movie from the list.

c)

It displays all movies in the list.

d)

It sorts the movies in the list alphabetically.

7.

What is the purpose of the 'delete' function in the Movie List program?

a)

To add a new movie to the list

b)

To remove a movie from the list

c)

To update the details of a movie

d)

To sort the movies in the list

8.

Which of the following movies is not in the initial 'movie_list'?

a)

Monty Python and the Holy Grail

b)

On the Waterfront

c)

Cat on a Hot Tin Roof

d)

The Godfather

9.

What will happen if the user inputs a number greater than the length of 'movie_list' in the 'delete' function?

a)

The function will delete the last item in the list.

b)

The function will throw an IndexError.

c)

The function will do nothing.

d)

The function will delete the first item in the list.

10.

What is the purpose of the 'while True:' loop in the Movie List program?

a)

To create an infinite loop until a condition is met to break it.

b)

To iterate over a list of movies.

c)

To execute a block of code a specific number of times.

d)

To handle exceptions in the program.

11.

Which command will add a movie to the list in the Movie List program?

a)

A) list

b)

B) add

c)

C) del

d)

D) exit

12.

What will the program print if an invalid command is entered?

a)

Error: Invalid command

b)

Command not recognized

c)

Please enter a valid command

d)

Invalid input, try again

13.

How do you define a list of lists with 3 rows and 4 columns in Python? Use the example of students provided in the image.

a)

students = [['Alice', 'Bob', 'Charlie', 'David'], ['Eve', 'Frank', 'Grace', 'Heidi'], ['Ivan', 'Judy', 'Mallory', 'Niaj']]

b)

students = [['Alice', 'Bob'], ['Charlie', 'David'], ['Eve', 'Frank'], ['Grace', 'Heidi']]

c)

students = [['Alice', 'Bob', 'Charlie'], ['David', 'Eve', 'Frank'], ['Grace', 'Heidi', 'Ivan'], ['Judy', 'Mallory', 'Niaj']]

d)

students = [['Alice', 'Bob', 'Charlie', 'David', 'Eve'], ['Frank', 'Grace', 'Heidi', 'Ivan', 'Judy'], ['Mallory', 'Niaj', 'Oscar', 'Peggy', 'Quentin']]

14.

Fill in the blank: To define a list of lists for movies with 3 rows and 3 columns, you can use the following syntax: movies = [["The Holy Grail", 1975, 9.99], ["Life of Brian", 1979, 12.30], ["The Meaning of Life", 1983, ____.]]

a)

14.50

b)

15.00

c)

13.75

d)

____

15.

What is the title of the movie released in 1975?

a)

Life of Brian

b)

The Holy Grail

c)

The Meaning of Life

16.

Fill in the blank: The movie 'Life of Brian' was released in ____.

a)

1979.

b)

1982.

c)

1975.

d)

1980.

17.

What is the rating of the movie 'The Meaning of Life'?

a)

9.99

b)

12.3

c)

7.5

18.

Fill in the blank: The movie 'Life of Brian' was released in _____.

a)

1979

b)

1982

c)

1975

d)

1985

19.

Fill in the blank: The price of 'The Meaning of Life' is _____.

a)

7.5

b)

10.0

c)

5.0

d)

12.5

20.

What command is used to list all movies in the Movie List 2D program?

a)

add

b)

list

c)

del

d)

exit

21.

The command to exit the Movie List 2D program is ______.

a)

exit

b)

quit

c)

close

d)

terminate

22.

What is the purpose of the 'list' function in the Movie List 2D program?

a)

To add a new movie to the list

b)

To display the list of movies

c)

To sort the movies alphabetically

d)

To remove a movie from the list

23.

What is the purpose of the 'delete' function in the Movie List 2D program?

a)

To add a new movie to the list

b)

To remove a movie from the list

c)

To update the details of a movie

d)

To display all movies in the list

24.

What is the purpose of the 'movie_list' variable in the code?

a)

To store a list of movie titles.

b)

To count the number of movies.

c)

To display movie ratings.

d)

To sort movies by release date.

25.

Which command will add a new movie to the list?

a)

A) list

b)

B) add

c)

C) del

d)

D) exit

26.

What does the count(item) method do in Python lists?

a)

It adds an item to the list.

b)

It removes an item from the list.

c)

It returns the number of occurrences of an item in the list.

d)

It sorts the list in ascending order.

27.

How does the reverse(list) method work in Python?

a)

It returns a new list that is the reverse of the original list.

b)

It reverses the list in place and returns None.

c)

It sorts the list in descending order.

d)

It creates a copy of the list and reverses the copy.

28.

What is the purpose of the sort([key=function]) method in Python lists?

a)

To sort the list in ascending order by default.

b)

To sort the list based on a specified key function.

c)

To reverse the list order.

d)

To shuffle the list randomly.

29.

How does the sorted(list[, key=function]) built-in function differ from the sort method in Python?

a)

sorted() returns a new sorted list, while sort() sorts the list in place.

b)

sorted() can only sort lists, while sort() can sort any iterable.

c)

sorted() is faster than sort() for large lists.

d)

sorted() does not accept a key function, while sort() does.

30.

What is the output of the following code? numlist = [5, 15, 84, 3, 14, 2, 8, 10, 14, 25] count = numlist.count(14)

a)

A) 1

b)

B) 2

c)

C) 3

d)

D) 4

31.

What will be the order of elements in 'numlist' after executing the following code? numlist = [5, 15, 84, 3, 14, 2, 8, 10, 14, 25] numlist.reverse()

a)

[5, 15, 84, 3, 14, 2, 8, 10, 14, 25]

b)

[25, 14, 10, 8, 2, 14, 3, 84, 15, 5]

c)

[2, 3, 5, 8, 10, 14, 14, 15, 25, 84]

d)

[84, 25, 15, 14, 14, 10, 8, 5, 3, 2]

32.

Fill in the blank: After sorting 'numlist', the order of elements will be ____. numlist = [5, 15, 84, 3, 14, 2, 8, 10, 14, 25] numlist.sort()

a)

[2, 3, 5, 8, 10, 14, 14, 15, 25, 84]

b)

[84, 25, 15, 14, 14, 10, 8, 5, 3, 2]

c)

[5, 15, 84, 3, 14, 2, 8, 10, 14, 25]

d)

[3, 2, 5, 8, 10, 14, 14, 15, 25, 84]

33.

What is the result of the following code? foodlist = ["orange", "apple", "Pear", "banana"] foodlist.sort()

a)

["apple", "banana", "orange", "Pear"]

b)

["Pear", "apple", "banana", "orange"]

c)

["orange", "apple", "banana", "Pear"]

34.

Fill in the blank: To sort a list in Python while ignoring case, you can use the key argument in the sort() method like this: foodlist.sort(key=____).

a)

str.lower

b)

str.upper

c)

len

d)

sorted

35.

What is the output of the following code? foodlist = ["orange", "apple", "Pear", "banana"] sorted_foodlist = sorted(foodlist) print(sorted_foodlist)

a)

['apple', 'banana', 'orange', 'Pear']

b)

['Pear', 'apple', 'banana', 'orange']

c)

['orange', 'apple', 'Pear', 'banana']

36.

Fill in the blank: To sort a list in Python while ignoring case, you can use the key argument in the sorted() function as follows: sorted(foodlist, key=______).

a)

str.lower

b)

str.upper

c)

len

d)

int

37.

What does the built-in function min(list) do in Python?

a)

Returns the maximum value in the list

b)

Returns the minimum value in the list

c)

Sorts the list in ascending order

d)

Sorts the list in descending order

38.

What does the built-in function max(list) do in Python?

a)

Returns the minimum value in the list

b)

Returns the maximum value in the list

c)

Sorts the list in ascending order

d)

Sorts the list in descending order

39.

What does the function choice(list) from the random module do in Python?

a)

Selects a random element from a non-empty sequence.

b)

Sorts the list in random order.

c)

Removes a random element from the list.

d)

Duplicates a random element in the list.