Peer Instruction: Strings Multiple Choice QuestionsΒΆ
- "abcddd q"
- Incorrect! The value of s changes in the following order from the first line- s="abc", s="dddabc", s="dddabc" and s="dddabcq".
- "abcddd" "" "" "q"
- Incorrect! The value of s changes in the following order from the first line- s="abc", s="dddabc", s="dddabc" and s="dddabcq".
- "qdddabc"
- Incorrect! The value of s changes in the following order from the first line- s="abc", s="dddabc", s="dddabc" and s="dddabcq".
- "dddabcq"
- Correct! The value of s changes in the following order from the first line- s="abc", s="dddabc", s="dddabc" and s="dddabcq".
- I don't know
- Incorrect! The value of s changes in the following order from the first line- s="abc", s="dddabc", s="dddabc" and s="dddabcq".
11-9-1: What is the value of s
after the following code runs?
s = "abc"
s = "d" * 3 + s
s = s + "" * 3
s = s + "q"
- C
- Incorrect! Here, len(s) = 4 and s[len(s)-1] = s[3]. So, the value of y will be the fourth character in the string i.e. "s".
- t
- Incorrect! Here, len(s) = 4 and s[len(s)-1] = s[3]. So, the value of y will be the fourth character in the string i.e. "s".
- s
- Correct! Here, len(s) = 4 and s[len(s)-1] = s[3]. So, the value of y will be the fourth character in the string i.e. "s".
- Nothing, this will cause an error
- Incorrect! Here, len(s) = 4 and s[len(s)-1] = s[3]. So, the value of y will be the fourth character in the string i.e. "s".
- I don't know
- Incorrect! Here, len(s) = 4 and s[len(s)-1] = s[3]. So, the value of y will be the fourth character in the string i.e. "s".
11-9-2: What is the value of y
after the following code runs?
s = "Cats"
y = s[len(s)-1]
- ats
- Incorrect! Here len(s) = 4. So, s[0:4] = "bats" i.e. the first four characters.
- bat
- Incorrect! Here len(s) = 4. So, s[0:4] = "bats" i.e. the first four characters.
- bats
- Correct! Here len(s) = 4. So, s[0:4] = "bats" i.e. the first four characters.
- This will throw an exception
- Incorrect! Here len(s) = 4. So, s[0:4] = "bats" i.e. the first four characters.
- I don't know
- Incorrect! Here len(s) = 4. So, s[0:4] = "bats" i.e. the first four characters.
11-9-3: What will the following code print?
s = "bats"
print(s[0:len(s)])
- mpire
- Incorrect! Negative index- "-1" has been used in the slice. So, the slice will end at the second last character.
- pires
- Incorrect! Negative index- "-1" has been used in the slice. So, the slice will end at the second last character.
- pire
- Correct! Negative index- "-1" has been used in the slice. So, the slice will end at the second last character.
- pir
- Incorrect! Negative index- "-1" has been used in the slice. So, the slice will end at the second last character.
- I don't know
- Incorrect! Negative index- "-1" has been used in the slice. So, the slice will end at the second last character.
11-9-4: What will the following code print?
s = "Vampires"
print(s[3:-1]
- Returns a copy of s
- Incorrect! For example, take s = "xyz". With each step in the loop, the value of new_s will change in the order- new_s = x, new_s = yx and new_s = zyx.
- Returns the reverse of s
- Correct! For example, take s = "xyz". With each step in the loop, the value of new_s will change in the order- new_s = x, new_s = yx and new_s = zyx.
- Returns a string with only the last character of s
- Incorrect! For example, take s = "xyz". With each step in the loop, the value of new_s will change in the order- new_s = x, new_s = yx and new_s = zyx.
- Returns a string with only the first character of s
- Incorrect! For example, take s = "xyz". With each step in the loop, the value of new_s will change in the order- new_s = x, new_s = yx and new_s = zyx.
- I don't know
- Incorrect! For example, take s = "xyz". With each step in the loop, the value of new_s will change in the order- new_s = x, new_s = yx and new_s = zyx.
11-9-5: What does the following code return?
def mystery(s):
new_s = ""
for c in s:
new_s = c + new_s
return new_s
- "abcddd q"
- Incorrect! Here, 'd' * 3 + s = 'ddd' + 'abc' = 'dddabc'. Then ' ' adds three spaces at the end of the string followed by a 'q' at the end.
- "abcddd q"
- Incorrect! Here, 'd' * 3 + s = 'ddd' + 'abc' = 'dddabc'. Then ' ' adds three spaces at the end of the string followed by a 'q' at the end.
- "abcdddq"
- Incorrect! Here, 'd' * 3 + s = 'ddd' + 'abc' = 'dddabc'. Then ' ' adds three spaces at the end of the string followed by a 'q' at the end.
- "qdddabc"
- Incorrect! Here, 'd' * 3 + s = 'ddd' + 'abc' = 'dddabc'. Then ' ' adds three spaces at the end of the string followed by a 'q' at the end.
- "dddabc q"
- Correct! Here, 'd' * 3 + s = 'ddd' + 'abc' = 'dddabc'. Then ' ' adds three spaces at the end of the string followed by a 'q' at the end.
11-9-6: What is the value of s
after the following code runs?
s = 'abc'
s = 'd' * 3 + s
s = s + ' ' * 3
s = s + 'q'
- Returns a copy of s
- Incorrect! Consider s = 'abc'. In the first iteration, new_s = c + new_s = 'a' + '' = 'a'. In the second iteration, new_s = 'b' + 'a' = 'ba' and so on. So, it will return the reverse of s.
- Returns the reverse of s
- Correct! Consider s = 'abc'. In the first iteration, new_s = c + new_s = 'a' + '' = 'a'. In the second iteration, new_s = 'b' + 'a' = 'ba' and so on. So, it will return the reverse of s.
- Returns a string consisting of only the final character of s
- Incorrect! Consider s = 'abc'. In the first iteration, new_s = c + new_s = 'a' + '' = 'a'. In the second iteration, new_s = 'b' + 'a' = 'ba' and so on. So, it will return the reverse of s.
- Returns a string consisting of only the first character of s
- Incrrect! Consider s = 'abc'. In the first iteration, new_s = c + new_s = 'a' + '' = 'a'. In the second iteration, new_s = 'b' + 'a' = 'ba' and so on. So, it will return the reverse of s.
11-9-7: What is a good description of what the following function does?
def mystery(s):
new_s = ''
for c in s:
new_s = c + new_s
return new_s
- 1
- Incorrect! For each 'a' and 'b', the nested for loop will run twice. Thus, val gets incremented by 1 four times resulting in val = 4.
- 2
- Incorrect! For each 'a' and 'b', the nested for loop will run twice. Thus, val gets incremented by 1 four times resulting in val = 4.
- 4
- Correct! For each 'a' and 'b', the nested for loop will run twice. Thus, val gets incremented by 1 four times resulting in val = 4.
- 8
- Incorrect! For each 'a' and 'b', the nested for loop will run twice. Thus, val gets incremented by 1 four times resulting in val = 4.
- 16
- Incorrect! For each 'a' and 'b', the nested for loop will run twice. Thus, val gets incremented by 1 four times resulting in val = 4.
11-9-8: What is the value of val
after this code executes?
val = 0
for i in 'ab':
for j in 'cd':
val += 1
- 1
- Incorrect! For each 'a', 'b' and 'c', the nested for loop will run thrice. Thus, val gets incremented by 1 nine times resulting in val = 9.
- 3
- Incorrect! For each 'a', 'b' and 'c', the nested for loop will run thrice. Thus, val gets incremented by 1 nine times resulting in val = 9.
- 6
- Incorrect! For each 'a', 'b' and 'c', the nested for loop will run thrice. Thus, val gets incremented by 1 nine times resulting in val = 9.
- 9
- Correct! For each 'a', 'b' and 'c', the nested for loop will run thrice. Thus, val gets incremented by 1 nine times resulting in val = 9.
- 27
- Incorrect! For each 'a', 'b' and 'c', the nested for loop will run thrice. Thus, val gets incremented by 1 nine times resulting in val = 9.
11-9-9: What is the value of val
after this code executes?
val = 0
for i in 'abc':
for j in 'def':
val += 1
You have attempted of activities on this page