Python Programming: String and List Manipulation Q&A, Exams of Advanced Education

A series of questions and answers related to python programming, specifically focusing on string and list manipulation. It covers topics such as string slicing, list methods (.append, .insert, .remove, .pop), and loop iterations. The questions are designed to test understanding of basic python syntax and data structures, making it a useful resource for students learning python. Multiple-choice questions with correct answers marked, offering a practical way to assess and reinforce knowledge of fundamental python concepts. It also touches on file handling operations like reading and writing files, and string methods like .split() and .join().

Typology: Exams

2024/2025

Available from 05/18/2025

Examproff
Examproff 🇺🇸

3

(2)

8.3K documents

1 / 18

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Python Unit 2 mod 1-4 Questions and
Answers Rated A+
3. Choose the correct print output for the following code:
name = "Alton"
print(name[::2])
A
"Atn"
B
"lo"
C
"on"
D
"Al" - ANSWER-"Atn"
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)
A
0
B
5
C
024
D
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12

Partial preview of the text

Download Python Programming: String and List Manipulation Q&A and more Exams Advanced Education in PDF only on Docsity!

Python Unit 2 mod 1-4 Questions and

Answers Rated A+

  1. Choose the correct print output for the following code: name = "Alton" print(name[::2]) A "Atn" B "lo" C "on" D "Al" - ANSWER-"Atn" 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) A 0 B 5 C 024 D

24 - ANSWER-C

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) A 2 B 8 C 2468 D 246 - ANSWER-D 246 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) A São PauloDubai B Dubai

D

cannot contain another list - a list in a list. - ANSWER-B can contain a mix of strings or numbers as list items. Choose the item that best completes the following sentence: "After using a .insert method on a Python list..." A one of the index datavalues gets overwritten. B an error occurs if the inserted object is not of the same type of the other list items. C the length of the list remains the same. D the length of the list increases by 1. - ANSWER-D the length of the list increases by 1. Choose the item that best completes the sentence: "A Python list..." A cannot be changed after initialization. B can add items of the same datatype only. C can add items to the end of the list using the .append() method or the .insert(-1, datavalue). D always appends to index 0 (zero) - [0] no matter how many items are in the list. - ANSWER-C can add items to the end of the list using the .append() method or the .insert(-1, datavalue). Choose the statement that will print the letter "d", given the variable declaration word = "windows"

print(word[-2]) B print(word[-1]) C print(word[2]) D print(word[3]) - ANSWER-print(word[3]) creates an object that can be addressed in python code - ANSWER-.open() delete contents and start writing only - ANSWER-'w' enables curl that can download files - ANSWER-curl In Python: list(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 In the for loop below, the variable "word" is an arbitrary loop control variable name. Any valid variable name can be used since its value is assigned only within this loop. See code example below: word = "the word of the day" for letter in word: print(letter) - ANSWER-False

creates 2 new lists one called list_name and the other called another_list - ANSWER- false To convert a string called line1 that is a sentence made up of words, into a list named words made of line1's words, we can use the .split() method and set the return value to a variable called words. See example below: words = line1.split() - ANSWER-true to free system resources - ANSWER-.close to read data from file, one line at a time - ANSWER-.readline() to read text from files as a list of lines - ANSWER-.readlines() to remove new line characters and other whitespaces - ANSWER-.strip() to set file read or write location - ANSWER-.seek() What is the best prediction for the output of the following Python code using .join()? wisdom_list = ["mistakes", "are", "a", "part", "of", "learning"] wisdom_string = "()".join(wisdom_list) print(wisdom_string) A mistakes()are()a()part()of()learning B mistakes are a part of learning C ()mistakes are a part of learning D wisdom_list - ANSWER-A mistakes()are()a()part()of()learning

What is the best prediction for the output of the following Python code using .split()? code_tip = "-PythonO-usesO-spacesO-forO-indentation" tip_words = code_tip.split('O') print(tip_words) A Python uses spaces for indentation B -Python -uses -spaces -for -indentation C ['O-Python', 'O-uses', 'O-spaces', 'O-for', 'O-indentation'] D ['-Python', '-uses', '-spaces', '-for', '-indentation'] - ANSWER-D ['-Python', '-uses', '-spaces', '-for', '-indentation'] What will this output be: poem_file = open('poem1.txt', 'r') poem_10char = poem_file.read(10) print(poem_10char) poem_10char - ANSWER-Loops I re 'Loops I re' Which best describes a valid Python use of the following list named days which contains a human count of 6 items? A days.append("Wednesday") B days.insert(4, "Wednesday") C print(days[5]) D all of these - ANSWER-D

Which best describes the valid Python syntax use of the following days list? A days.append("Wednesday") B days.append(14) C print(days[-1]) D all of these - ANSWER-D all of these Which choice best represents the output of the following code? student_name = "iteration" new_word = "" for letter in student_name[:3]: new_word += letter print(new_word) A "ration" B "ite" C "iter" D "ation" - ANSWER-"ite" Which choice best represents the output of the following code?

student_name = "iteration" count = 0 for letter in student_name: if letter.lower() == "e": print(count) count += 1 A "2" B "3" C "-4" D "-5" - ANSWER-"2" Which choice will print the first half of the word "Consequences" (Conseq), given the following variable declaration? word = "Consequences" A print(word[6:]) B print(word[:6]) C print(word[1:7]) D print(word[7:1]) - ANSWER-print(word[:6]) Which choice will print the word "tac", given the following variable declaration?

D

all of these - ANSWER-C for number in numbers: Which code is the best use of Python that deletes the integer, 5, in the following list named numbers? numbers = [7, 1, 5, 8, 0] A numbers[3] = "" B numbers[2].delete() C del numbers[2] D numbers.remove(2) - ANSWER-C del numbers[2] Which code overwrites or replaces the "0" with a "5" in the following list named numbers? numbers = [1, 1, 0] A numbers[3] = 5 B numbers[2] = 5 C numbers.append(5) D numbers.insert(2, 5) - ANSWER-B numbers[2] = 5 Which code places another integer, 2, to the end of the following list? numbers = [1, 1, 2]

A

numbers + 2 B numbers.add(2) C numbers += 2 D numbers.append(2) - ANSWER-D numbers.append(2) Which code will result in ltr_cnt = 2 for counting the letter "c" found in the following test_string? test_string = 'Mexico City' A ltr_cnt = test_string.count('C' or 'c'') B ltr_cnt = test_string.count('c') C ltr_cnt = test_string.count('C') D none of these - ANSWER-D none of these Which is NOT a start to a string iteration loop, given the following string variable assignment? word = "health" A for letter in word: B for word in letter:

word.find("y") C word.find("Z") D word.find("o") - ANSWER-word.find("Z") Which print output is the result of the following code? A "w" B "s" C "i" D "" (empty string) - ANSWER-"s" Which Python code results in a list called tip_list being equal to ['Strings', 'are', 'a', 'sequence', 'of', 'characters.'] Given tip = 'Strings are a sequence of characters.' A tip_list = tip[0:-1] B tip_list.split(tip) C tip_list = list(tip) D tip_list = tip.split() - ANSWER-D tip_list = tip.split() Which Python code results in output of a sorted scores list from small to big, with changing the original scores list to the sorted order?

scores = [ 0, 5, 2, 11, 1, 9, 7] A scores.reverse() print(scores) B scores.sort() print(scores) C sorted(scores) print(scores) D all of these - ANSWER-B scores.sort() print(scores) Which Python code results in output of a sorted scores list from small to big, without changing the original scores list defined below? scores = [ 0, 5, 2, 11, 1, 9, 7] A print(scores) B print(scores.sort()) C print(sorted(scores)) D all of these - ANSWER-C print(sorted(scores)) Which Python code results in reversing the order of the scores list permanently until reversed again or reinitialized? scores = [ 0, 5, 2, 11, 1, 9, 7]