Code Hs quiz Nov 13

¡Supera tus tareas y exámenes ahora con Quizwiz!

What does the following program print? x = "ab" y = "banana" print x in y True False 0 "ba"

False

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

II, IV

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 ANSWERED

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 word applies to strings in Python? mutable immutable

immutable

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

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? upper lower swapcase strip

lower

Which of the following prints E? my_string = "ABCDE" print(my_string[-1]) my_string = "ABCDE" print(my_string[5]) my_string = "ABCDE" print(my_string[-5]) my_string = "ABCDE" print(my_string(5))

my_string = "ABCDE" print(my_string[-1])

which of the following prints the letter A? my_string = "ABCDE" print(my_string[1]) my_string = "ABCDE" print(my_string(0)) my_string = "ABCDE" print(my_string(1)) my_string = "ABCDE" print(my_string[0])

my_string = "ABCDE" print(my_string[0])

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

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

Which of the following programs does not print the first five letters of the alphabet, each on its own line? my_string = "abcde" for letter in range(len(my_string)): print letter my_string = "abcde" for letter in my_string: print letter my_string = "abcde" for i in range(len(my_string)): print my_string[i]

my_string = "abcde" for letter in range(len(my_string)): print letter

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]

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:]

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

print sign[:6] + "Birth" + sign[6:]

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 functions prints 3, and nothing else? s = "hello" x = s.find("l") print x s = "banana" x = s.find("a") print x s = "hello" for i in range(len(s)): print i s = "apple" x = s.find("l") print x

s = "apple" x = s.find("l") print x

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 operator allows you to create a string that is the result of putting two different strings together, side by side? + - * /

+

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 does len("hello") evaluate to? "hello" 4 "—-" 5

5

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

7

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. ANSWERED

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 your answer in question 1 mean? Strings in Python can be modified and replaced. Strings in Python can be modified, but not replaced. Strings in Python can be replaced, but not modified. Strings in Python can be neither replaced nor modified.

Strings in Python can be replaced, but not modified.

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 the following program print? print "a" in "banana" True False 1 "aaa"

True

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 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


Conjuntos de estudio relacionados

CH. 3- General Guidelines and Notations

View Set

Social Psychology Final Exam - Pro Social Behavior/Aggression/Prejudice/Groups/Social Dilemmas/Integrating

View Set

Chapter 8 - Risk Evaluation and Mitigation Strategies

View Set

Chapter 10: Cloud and Virtualization Security

View Set

Java Part 2: Questions on Operators and Control Statements

View Set

CompTIA A+ Certification Practice Test 8 (Exam 220-902)

View Set

US History 10 Chapter 3 (England & Its Colonies)

View Set

CH 2 Operating-System Structures

View Set

Kidney Stones & Benign Prostatic Hyperplasia

View Set