5.22. Functions and Strings Multiple Choice Questions¶
- final_string = string1[:4] + string2[-3:]
- Correct! Using the slice operator [start:end], the starting index is inclusive, while the ending index is exclusive.
- final_string = string1[:4] + string2[6:]
- Correct! Using the slice operator [start:end], the starting index is inclusive, while the ending index is exclusive.
- final_string = string1[:3] + string2[6:]
- Try again! This would assign final_string to "yeling".
- final_string = string1[:3] + string2[6:-1]
- Try again! This would assign final_string to "yelin".
- final_string = string1[:4].append(string2[-3:])
- Try again! String objects do not have the attribute "append".
Q-1: Given the below code snippet, which of the following options would create a variable called final_string
that is assigned to the word “yelling”?
string1 = "yellow"
string2 = "screaming"
- print("I have " + num_of_apples + " apples.")
- Try again! You can't concatenate a string and an integer together.
- print("I have " + str("num_of_apples") + " apples.")
- Try again! Do not enclose the variable name in a string or you will get just those characters.
- print("I have 4 apples.")
- Try again! This doesn't use the variable num_of_apples.
- print("I have " + str(num_of_apples) + " apples.")
- Correct! By using str, the integer variable is converted into a string.
Q-2: Which of the following choices correctly prints a sentence using the variable num_of_apples
?
num_of_apples = 4
- split()
- Correct!
- lower()
- Correct!
- append()
- Try again! This is a list method, not a string method.
- startswith()
- Correct!
- sort()
- Try again! This is a list method, not a string method.
Q-3: Which of the following are string methods?
- count(string)
- Try again! Count is a python string method that counts the amount of occurrences of a substring.
- len(string)
- Correct!
- int(string)
- Try again! String with words cannot be converted to ints.
- length(string)
- Try again! Instead of length, it should be len, because length is not a built-in python function.
Q-4: Which of the following code corresponds to the amount of characters in the following string
variable?
string = "I love coding!"
- J. Weathers
- Try again! The first letter in the last name should be lowercase.
- Jo. Weathers
- Try again! The end of a slice operator is exclusive (e.g., 1 is exclusive in this example).
- oa. Weathers
- Try again! Strings are indexed starting at 0.
- J. weathers
- Correct!
- j. weathers
- Try again! Only the last name should be lowercase.
Q-5: What does the following code output?
def abbrev(first_name, last_name):
print(first_name[0:1] + ". " + last_name.lower())
abbrev("Joanne", "Weathers")
- Hello KatiePe.
- Try again! This would be correct if there was a print statement.
- Nothing.
- Correct! Nothing would be outputted because there is no print statement.
- Hello Katie Pe.
- Try again! There shouldn't be a space between the first and last name.
- Hello KatiePE.
- Try again! The E should not be capitalized. The upper() method would capitalize the E, but not the capitalize() method.
Q-6: After running this code, what would be the output if the input was first_name = "Katie"
and last_name = "perkins"
?
def abbrev():
first_name = input("What is your first name? ")
last_name = input("What is your last name? ")
return("Hello " + first_name + last_name[0:2].capitalize() + ". ")
def main():
abbrev()
main()
- print(item[2:7])
- Try again! This would print "teboo".
- print(item[2:6])
- Correct!
- print(item[-6:-2])
- Correct!
- print(item[3] + item[4] + item[5] + item[6])
- Try again! This would print "eboo".
- print(item[3:7])
- Try again! This would print "eboo".
Q-7: Given the variable item
, how would you grab the letters “tebo”?
def notebook():
item = "notebook"
# What goes here?
notebook()
You have attempted of activities on this page