INTRO TO CS (QUIZ 6)
How many times is the letter o printed by the following statements? s = "python rocks" idx = 1 while idx < len(s): print(s[idx]) idx = idx + 2
0 idx goes through the odd numbers starting at 1. o is at position 4 and 8.
How many times is the word HELLO printed by the following statements? s = "python rocks" for ch in s: print("HELLO")
12
What is printed by the following statements? s = "python rocks" print(len(s))
12
How many times is the letter o printed by the following statements? s = "python rocks" for idx in range(len(s)): if idx % 2 == 0: print(s[idx])
2 it will print all the characters in even index positions and the o character appears both times in an even location.
What is printed by the following statements? v = 2.34567 print('{:.1f} {:.2f} {:.7f}'.format(v, v, v))
2.3 2.35 2.3456700
What is printed by the following statements? s = "python rocks" print(s.count("o") + s.count("p"))
3
def count(text, aChar): lettercount = 0 for c in text: if c == aChar: lettercount = lettercount + 1 return lettercount print(count("banana","a"))
3
How many times is the word HELLO printed by the following statements? s = "python rocks" for ch in s[3:8]: print("HELLO")
5
s = "ball" r = "" for item in s: r = item.upper() + r print(r)
LLAB
in and not in operator
The in operator tests whether one string is contained inside another string. Examples: 'heck' in "I'll be checking for you." evaluates to True. 'cheese' in "I'll be checking for you." evaluates to False.
string comparison
The six common comparision operators work with strings, evaluating according to lexigraphical order. Examples: 'apple' < 'banana' evaluates to True. 'Zeta' < 'Appricot' evaluates to False. 'Zebra' <= 'aardvark' evaluates to True because all upper case letters precede lower case letters.
for loop traversal
Traversing a string means accessing each character in the string, one at a time a p p l e
slicing
[ : ] A slice is a substring of a string. Example: 'bananas and cream'[3:6] evaluates to ana (so does 'bananas and cream'[1:4]
indexing
[ ] Access a single character in a string using its position (starting from 0). Example: 'This'[2] evaluates to 'i'
What is printed by the following statements? s = "python rocks" print(s[-3])
c
What is printed by the following statements: s = "Ball" s[0] = "C" print(s)
error
Evaluate the following comparison: "dog" < "Dog"
false
Evaluate the following comparison: "dog" < "Doghouse"
false
What is printed by the following statements? s = "python rocks" print(s[3])
h
What is printed by the following statements? s = "python rocks" print(s[3:8])
hon r
length function
len Returns the number of characters in a string. Example: len('happy') evaluates to 5.
What is printed by the following statements? s = "python" excl = "!" print(s+excl*3)
python!!!
What is printed by the following statements? s = "python" t = "rocks" print(s + t)
pythonrocks
What is printed by the following statements? s = "python rocks" print(s[len(s)-5])
r
What is printed by the following statements? s = "python rocks" print(s[7:11] * 3)
rockrockrock
What is printed by the following statements? x = 2 y = 6 print('sum of {} and {} is {}; product: {}.'.format( x, y, x+y, x*y))
sum of 2 and 6 is 8; product: 12.
What is printed by the following statements? s = "python rocks" print(s[2] + s[-5])
tr
Evaluate the following comparison: "Dog" < "Doghouse"
true
What is printed by the following statements? s = "python rocks" print(s[1] * s.index("n")) A. yyyyy B. 55555 C. n D. Error, you cannot combine all those things together.
yyyyy