Docsity

Python Lists: Practice Questions and Answers, Exams of Advanced Education

Toggle sidebar
bg1
Python Module 9 Questions and
Answers Graded A+
Choose the best representation of the output expected for the following code.
cities = ["New York", "Shanghai", "Munich", "Tokyo", "Dubai", "Mexico City", "São
Paulo", "Hyderabad"]
for city in cities:
if city.startswith("P"):
print(city)elif city.startswith("D"):print(city) - ANSWER-Dubai
Choose the best representation of the output expected for the following code.
numbers = ""
for x in range(0,5,2):numbers += str(x)print(numbers) - ANSWER-024
Choose the best representation of the output expected for the following code.
numbers = ""
for x in range(2,8,2):numbers += str(x)print(numbers) - ANSWER-246
Choose the best representation of the output expected for the following code.
numbers = ""
for x in range(7):
numbers += str(x)
print(numbers) - ANSWER-0123456
Choose the item that best completes the following sentence:
"A Python list..." - ANSWER-can contain both strings and numbers together as list
items.
Choose the item that best completes the following sentence:
"After using an insert on a Python list..." - ANSWER-the length of the list increases by 1.
Choose the item that best completes the sentence:
"A Python list..." - ANSWER-can add items to the end of the list using the .append()
method.
In Python, range(0,5) is equivalent to the list [0, 1, 2, 3, 4]. - ANSWER-True
In the following for loop code, the variable name is "student" because it can only be
called "student" and cannot be replaced with another valid variable name such as
"pupil".
students = ["Alton", "Hiroto", "Skye", "Tobias"]
for student in students:
print("Hello", student) - ANSWER-False
pf3

Partial preview of the text

Download Python Lists: Practice Questions and Answers and more Exams Advanced Education in PDF only on Docsity!

Python Module 9 Questions and

Answers Graded A+

Choose the best representation of the output expected for the following code. cities = ["New York", "Shanghai", "Munich", "Tokyo", "Dubai", "Mexico City", "São Paulo", "Hyderabad"] for city in cities: if city.startswith("P"): print(city)elif city.startswith("D"):print(city) - ANSWER-Dubai Choose the best representation of the output expected for the following code. numbers = "" for x in range(0,5,2):numbers += str(x)print(numbers) - ANSWER- Choose the best representation of the output expected for the following code. numbers = "" for x in range(2,8,2):numbers += str(x)print(numbers) - ANSWER- Choose the best representation of the output expected for the following code. numbers = "" for x in range(7): numbers += str(x) print(numbers) - ANSWER- Choose the item that best completes the following sentence: "A Python list..." - ANSWER-can contain both strings and numbers together as list items. Choose the item that best completes the following sentence: "After using an insert on a Python list..." - ANSWER-the length of the list increases by 1. Choose the item that best completes the sentence: "A Python list..." - ANSWER-can add items to the end of the list using the .append() method. In Python, range(0,5) is equivalent to the list [0, 1, 2, 3, 4]. - ANSWER-True In the following for loop code, the variable name is "student" because it can only be called "student" and cannot be replaced with another valid variable name such as "pupil". students = ["Alton", "Hiroto", "Skye", "Tobias"] for student in students: print("Hello", student) - ANSWER-False

The .append() method adds an item to the beginning of a list. - ANSWER-False The .insert() method overwrites (replaces) the list item at the selected index. - ANSWER-False The .pop() method both returns and deletes an item from a list. - ANSWER-True The first item in a Python List is located at index 0 (zero). - ANSWER-True Which best describes a valid Python use of the following days List? - ANSWER- days.insert(1, "Wednesday") Which best describes the result of the following code? days = [12, 9, "Monday", 1] days.remove("Friday") - ANSWER-Error, because "Friday" was not found and can't be removed Which best describes the result of the following code? numbers = [2, 3, 2]total = 0while numbers:total += numbers.pop() - ANSWER-numbers is an empty list and total is 7 Which best describes the valid Python use of the following days List? - ANSWER-All of the above Which code is the best use of Python that deletes the "5" in the following numbers list? numbers = [7, 1, 5, 8, 0] - ANSWER-del numbers[2] Which code is the best use of Python to iterate through the list "numbers"? numbers = [7, 1, 5, 8, 0] - ANSWER-for number in numbers: Which code overwrites (replaces) the "0" with a "5" in the following numbers list? numbers = [1, 1, 0] - ANSWER-numbers[2] = 5 Which code places another "2" integer to the end of the following list? numbers = [1, 1, 2] - ANSWER-numbers.append(2) Which code will result in the ltr_cnt = 2 for counting the letter "c" found in the following test_string? test_string = 'Mexico City' - ANSWER-None of the above Which is a valid way to initialize a Python list? - ANSWER-scores = [11, 8, 0, 24, 31, 12, 19] Which is valid Python code that will print "Green", given the following code? colors = ["Red", "Blue", "Green", "Yellow"] - ANSWER-print(colors[2])