Supplemental Material Exam S7, S8, S9 Study Guide

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

What is the correct way to concatenate the variable sign (sign = "Happy day!") to produce the following output? expected output: "Happy Birthday!" A. print(sign[:6] + "Birth" + sign[6:]) B. print(sign[:5] + "Birth" + sign[6:]) C. print(sign[:6] + "Birth" + sign[5:]) D. print(sign[:6] + "Birth" + sign[:6])

A. print(sign[:6] + "Birth" + sign[6:])

Which of the following is the correct way to declare a tuple? A. x = (1, 2, 3) B. x = [1, 2, 3]

A. x = (1, 2, 3)

On which line does this program throw an error? word = "killer whale" pos = word.find(" ") word = word + "!" word[pos] = "-" Line 1 Line 2 Line 3 Line 4

Line 4

What does this function do? # sentence: a string containing a sentence # char: a letter def mystery(sentence, char): res = "" for letter in sentence: if letter == " ": res = res + char else: res = res + letter return res Returns the same sentence, but adds the letter char after every space Returns the same sentence, but replaces every space with the letter char Returns the same sentence, but adds the letter char after every letter in the sentence, except for spaces Returns the same sentence, but adds the letter char after every letter in the sentence

Returns the same sentence, but replaces every space with the letter char

What does "immutable" mean with respect to Python strings? The variable can be assigned a new value, and the string's value can be modified. The variable can be assigned a new value, but the string's value cannot be modified. A string variable can never be changed. The variable cannot be assigned a new value, and the string's value cannot be modified. The variable cannot be assigned a new value, but the string's value can be modified.

The variable can be assigned a new value, but the string's value cannot be modified.

What does this program print? numbers = [x % 2 == 0 for x in range(0, 6)] print(numbers) [True, False, True, False, True, False] [0, 1, 0, 1, 0, 1, 0] [0, 1, 0, 1, 0, 1] [0, 1, 2, 3, 4, 5]

[True, False, True, False, True, False]

W hat does the following program print? my_grid = [[2, 5, 7, 10], [-3, "hello", 9, "hi"], ["hello", "hi", "hello", "hi"], [True, False, True, False]] print(my_grid[:2]) [5, 7, 10, -3, 'hello', 9, 'hi'] [2, 5] [[2, 5, 7, 10], [-3, 'hello', 9, 'hi']] [2, 5, 7, 10]

[[2, 5, 7, 10], [-3, 'hello', 9, 'hi']]

What is printed out by this program? first = "abcde" second = "zyxwv" res = "" for i in range(len(first)): res = res + first[i] + second[i] print(res) abcdezyxwv azbycxdwev az by cx dw ev The program will throw an error

azbycxdwev

Which of the functions below will return a string that is in alternating caps? For example, alt_case("sheep") should return "sHeEp". def alt_case(word): res = "" for i in range(len(word)): if i % 2 == 1: res + word[i].upper() else: res + word[i].lower() return res def alt_case(word): res = "" return word.swapcase() def alt_case(word): res = "" for i in range(word): if i % 2 == 1: res = res + word[i].upper() else: res = res + word[i].lower() return res def alt_case(word): res = "" for i in range(len(word)): if i % 2 == 1: res = res + word[i].upper() else: res = res + word[i].lower() return res

def alt_case(word): res = "" for i in range(len(word)): if i % 2 == 1: res = res + word[i].upper() else: res = res + word[i].lower() return res

Which of the following choices will print AeCl? Assume the following variables have been defined. first_name = "Alice" last_name = "Carmichael" print(first_name[0] + last_name[0]) print(first_name[0] + last_name[0] + first_name[-1] + last_name[-1]) print(first_name[0] + first_name[-1] + last_name[0] + last_name[-1]) print(first_name[1:] + last_name[1:])

print(first_name[0] + first_name[-1] + last_name[0] + last_name[-1])

Suppose you have the following dictionary, which stores the names and ages of people: my_dictionary = { "Bo": 32, "Fiona": 28, "Carlos": 45 } Which line correctly prints Bo's age? print(my_dictionary[0]) print(my_dictionary[0][1]) print(my_dictionary["Bo"]) print(my_dictionary["Bo"][1])

print(my_dictionary["Bo"])

Which of the following expressions will result in "brown"? Let sentence = "The brown lazy dog" print(sentence[4:8]) print(sentence[4:9]) print(sentence[3:8]) print(sentence[3:9])

print(sentence[4:9])

Which of the following expressions will print "dog"? Let sentence = "lazy dog" print(sentence[6:]) print(sentence[:]) print(sentence[:5]) print(sentence[5:])

print(sentence[5:])

Which of the following expressions will print "L"? Let word = "PINEAPPLE" print(word[-4:]) print(word[-1]) print(word[-2]) print(word[-3])

print(word[-2])

Which of the following is a correct example of packing? my_list = [1, 2, 3] x, y, z = my_list my_list = [1, 2, 3] x = y = z = my_list x = 1 y = 2 z = 3 my_list = [x, y, z] x = 1 y = 2 z = 3 my_list = x = y = z

x = 1 y = 2 z = 3 my_list = [x, y, z]

What is the value of pos after this function call? name = "Edsger Wybe Dijkstra" pos = name.find("z") -1 20 z 0

-1

What is the value of pos after this function call? name = "Edsger Wybe Dijkstra" pos = name.find("Edsger") -1 0 E 5

0

What is the value of pos after this function call? name = "Edsger Wybe Dijkstra" pos = name.find("W") 7 8 W -1

7

Which operator allows you to create a string that is the result of putting two different strings together, side by side? A. + (plus sign) B. - (minus sign) C. * (asterisk) D. / (forward slash)

A. + (plus sign)

What does the following program print? print("a" in "banana") A. True B.False C. 1 D. "aaa"

A. True We are using the 'in' keyword here to write a condition. If 'a' exists in the string 'banana' it will evaluate to True.

Which of the following prints E? A. my_string = "ABCDE" print(my_string[-1]) B. my_string = "ABCDE" print(my_string[5]) C. my_string = "ABCDE" print(my_string[-5]) D. my_string = "ABCDE" print(my_string(5))

A. my_string = "ABCDE" print(my_string[-1]) There is no character at index 5 because index values start at 0.

Which of the following programs DOES NOT PRINT the first five letters of the alphabet, each on its own line? A. my_string = "abcde" for letter in range(len(my_string)): print(letter) B. my_string = "abcde" for letter in my_string: print(letter) C. my_string = "abcde" for i in range(len(my_string)): print(my_string[i])

A. my_string = "abcde" for letter in range(len(my_string)): print(letter) When we use a range value that is the length of the string, we get numbers, not letters.

What does the following program print? x = "ab" y = "banana" print(x in y) A. True B. False C. 0 D. "ba"

B. False We are using the 'in' keyword here to write a condition. If 'ab' exists IN THAT ORDER in the string 'banana' it will evaluate to True, but it does not in this case.

Which word applies to strings in Python? A. Mutable B. Immutable

B. Immutable

If I'm getting input from a user and having their answer dictate my if/else statement, like so: happy = input("Are you happy? (yes/no): ") if happy == "yes": draw_smile() elif happy == "no": draw_frown() else: print("Invalid response") Which string method can I use to make sure that the phrase 'invalid response' only displays if the user has typed something other than 'yes' or 'no', without paying attention to their capitalization? A. upper B. lower C. swapcase D. strip

B. lower If we use the 'lower' method to change all the letters in their response to lowercase, they can type 'Yes' and a smile will still be drawn!

Which of the following prints CDE? A. my_string = "ABCDE" print(my_string[3:]) B. my_string = "ABCDE" print(my_string[2:]) C. my_string = "ABCDE" print(my_string[3:5]) D. my_string = "ABCDE" print(my_string[2:4])

B. my_string = "ABCDE" print(my_string[2:])

What does your answer in question 1 mean? Question 1: Which word applies to strings in Python? Immutable A. Strings in Python can be modified and replaced. B. Strings in Python can be modified, but not replaced. C. Strings in Python can be replaced, but not modified. D. Strings in Python can be neither replaced nor modified.

C. Strings in Python can be replaced, but not modified.

What does this function do? # address: string containing an email address def mystery(address): if "@" not in address: return "" address = address.strip() return address Returns a version of the email address without any symbols in it. Returns the address without any leading or trailing whitespace. Returns an empty string Checks to make sure the email address is valid by checking for '@' in the address. Returns the address without any leading or trailing whitespace.

Checks to make sure the email address is valid by checking for '@' in the address. Returns the address without any leading or trailing whitespace.

What does len("hello") evaluate to? A. "hello" B. 4 C. "—-" D. 5

D. 5

Which of the following is NOT true about tuples? A. Tuples are ordered. B. Tuples are immutable. C. Tuples can contain elements of different types. D. Tuples use parentheses to access individual elements, rather than square brackets.

D. Tuples use parentheses to access individual elements, rather than square brackets. We need to use square brackets to access individual elements using index values no matter if we are indexing a string, tuple, or list.

Which of the following prints the letter A? A. my_string = "ABCDE" print(my_string[1]) B. my_string = "ABCDE" print(my_string(0)) C. my_string = "ABCDE" print(my_string(1)) D. my_string = "ABCDE" print(my_string[0])

D. my_string = "ABCDE" print(my_string[0]) 0 is the first index value.

Which of the following functions prints 3, and nothing else? A. s = "hello" x = s.find("l") print(x) B. s = "banana" x = s.find("a") print(x) C. s = "hello" for i in range(len(s)): print(i) D. s = "apple" x = s.find("l") print(x)

D. s = "apple" x = s.find("l") print(x) This program will print 3 because the first 'l' is located at index 3!

Which of the following expressions will get the last character in a string? Assume that word is a string variable. I. word[0] II. word[-1] III. word[len(word)] IV. word[len(word)-1] I, II, III II, IV II, III, IV I, IV

II, IV

What does this program print? numbers = [x + x for x in range(4)] print(numbers) [0, 1, 2, 3, 4] [0, 1, 2, 3] [2, 4, 6, 8] [0, 2, 4, 6]

[0, 2, 4, 6]

What does the following program print? my_grid = [[2, 5, 7, 10], [-3, "hello", 9, "hi"], ["hello", "hi", "hello", "hi"], [True, False, True, False]] print(my_grid[0]) [5, 7, 10, -3, 'hello', 9, 'hi'] [2, 5] [[2, 5, 7, 10], [-3, 'hello', 9, 'hi']] [2, 5, 7, 10]

[2, 5, 7, 10]

What is printed out by this program? word = "killer whale" print(word[0:100]) killer whal "killer whale " killer whale The program will throw an error

killer whale

What is the output of the following program? sentence = "the dog" for letter in sentence: print(letter + letter) t h e d o g t h e d o g t h e d o g tt hh ee dd oo gg The program will cause an error

tt hh ee dd oo gg

What function should be used in the blank to capitalize the first letter of the word stored in word? first_char = word[0] word = first_char.______() + word[1:] upper lower swapcase find

upper

Which of the following string operation is illegal? Assume that word = "music". word[0] = "M" word = word[2] + word[-1] word = "musical" word = word + "al"

word[0] = "M"

Which of the following functions would return the word "CAT" when given "iCjnyAyT"? def find_secret_word(message): hidden_word = "" for letter in message: if letter.upper(): hidden_word = hidden_word + letter return hidden_word def find_secret_word(message): hidden_word = "" for letter in message: if "ABCDEFGHIJKLMNOPQRSTUVWXYZ" in message: hidden_word = hidden_word + letter return hidden_word def find_secret_word(message): hidden_word = "" for letter in message: if letter != letter.lower(): hidden_word = hidden_word + letter return hidden_word def find_secret_word(message): hidden_word = "" for letter in message: if letter in message: hidden_word = hidden_word + letter return hidden_word

def find_secret_word(message): hidden_word = "" for letter in message: if letter != letter.lower(): hidden_word = hidden_word + letter return hidden_word

Which of the following if statements checks if the string variable sentence contains the word "the"? if "t" or "h" or "e" in sentence: print("Contains 'the'") if "the" in sentence: print("Contains 'the'") if "the" == sentence: print("Contains 'the'") if sentence in "the": print("Contains 'the'")

if "the" in sentence: print("Contains 'the'")

Which of the following is a correct example of unpacking? my_list = [1, 2, 3] x, y, z = my_list my_list = [1, 2, 3] x = y = z = my_list x = 1 y = 2 z = 3 my_list = [x, y, z] x = 1 y = 2 z = 3 my_list = x = y = z

my_list = [1, 2, 3] x, y, z = my_list


Ensembles d'études connexes

Emergence in Global society 1000C: A history of world societies volume 2: Chapter 19: New Worldviews and Ways of life

View Set

Managerial Finance Quiz Ch. 1 & 2

View Set

CH2 Matching Exercises for Organs and Systems #1

View Set

Communication Arts 100 Final Exam *

View Set

5.4 Lines of Credits and Credit Card

View Set

BEC Homework 1 - Chapter 1 and 2

View Set