I Hate CBT's

View Original

7.2.6 Spell It Out

Question: 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])
Answer: my_string = "ABCDE" print(my_string[0])
==================================================
Question: 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))
Answer: my_string = "ABCDE" print(my_string[-1])
==================================================
Question: Which operator allows you to create a string that is the result of putting two different strings together, side by side?
+
-
*
/
Answer: +
==================================================

print sign[:6] + "Birth" + sign[6:]
print sign[:5] + "Birth" + sign[6:]
print sign[:6] + "Birth" + sign[5:]
print sign[:6] + "Birth" + sign[:6]
Answer: print sign[:6] + "Birth" + sign[6:]
==================================================
Question: 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])
Answer: my_string = "ABCDE" print(my_string[2:])
==================================================
Question: Which word applies to strings in Python?
mutable
immutable
Answer: immutable
==================================================
Question: 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.
Answer: Strings in Python can be replaced, but not modified.
==================================================
Question: 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]
Answer: my_string = "abcde" for letter in range(len(my_string)): print letter
==================================================
Question: What does len("hello") evaluate to?

4

5
Answer: 5
==================================================
Question: What does the following program print?

True
False
1

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

Answer: False
==================================================
Question: 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
Answer: s = "apple" x = s.find("l") print x
==================================================
Question: If I'm getting input from a user and having their answer dictate my if/else statement, like so:

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
Answer: lower
==================================================
Question: 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
Answer: upper
==================================================
Question: Which of the following choices will print AeCl? Assume the following variables have been defined.

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:]
Answer: print first_name[0] + first_name[-1] + last_name[0] + last_name[-1]
==================================================
Question: Which of the following expressions will result in "brown"?

print sentence[4:8]
print sentence[4:9]
print sentence[3:8]
print sentence[3:9]
Answer: print sentence[4:9]
==================================================
Question: Which of the following expressions will print "dog"?

print sentence[6:]
print sentence[:]
print sentence[:5]
print sentence[5:]
Answer: print sentence[5:]
==================================================
Question: Which of the following expressions will print "L"?

print word[-4:]
print word[-1]
print word[-2]
print word[-3]
Answer: print word[-2]
==================================================
Question: Which of the following string operation is illegal? Assume that word = "music".

word = word[2] + word[-1]



==================================================
Question: 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
Answer: II, IV
==================================================
Question: 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
Answer: tt hh ee dd oo gg
==================================================
Question: Which of the following if statements checks if the string variable sentence contains the word "the"?
'“
'“
'“
'“
'“
==================================================
Question: 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
Answer: 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
==================================================
Question: What is the value of pos after this function call?
name = "Edsger Wybe Dijkstra" pos = name.find("W")
7
8
W
-1
Answer: 7
==================================================
Question: What is the value of pos after this function call?
name = "Edsger Wybe Dijkstra" pos = name.find("Edsger")
-1
0
E
5
Answer: 0
==================================================
Question: What is the value of pos after this function call?
name = "Edsger Wybe Dijkstra" pos = name.find("z")
-1
20
z
0
Answer: -1
==================================================
Question: 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
Answer: Returns the same sentence, but replaces every space with the letter char
==================================================
Question: 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.
Answer: 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
==================================================
Question: On which line does this program throw an error?

Line 1
Line 2
Line 3
Line 4
Answer: Line 4
ANSWERED
==================================================
Question: 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.
Answer: The variable can be assigned a new value, but the string's value cannot be modified.
==================================================
Question: What is printed out by this program?
word = "killer whale" print word[0:100]
killer whal

killer whale
The program will throw an error
Answer: killer whale
==================================================
Question: 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
Answer: azbycxdwev
==================================================
Question: 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
Answer: def find_secret_word(message): hidden_word = "" for letter in message: if letter != letter.lower(): hidden_word = hidden_word + letter return hidden_word
==================================================