Skip to main content

Exercises 7.14 Multiple Choice Questions

1.

    11-9-1: What is printed by the following statements?
    s = "python rocks"
    print(s[1] * s.index("n"))
    
  • yyyyy
  • Correct! s[1] = y and the index of n is 5, so y * 5 prints 5 y characters.
  • 55555
  • Incorrect! s.index("n") = 5, but it is multiplying something else. Try again.
  • y5
  • Incorrect! The print statement contains multiplication. Try again.
  • TypeError
  • Incorrect! Multiplying a string by an int is allowed. Try again.

2.

    11-9-2: What will be printed when the following executes?
    str = "His shirt is red"
    pos = str.find("is")
    print(pos)
    
  • 1
  • Correct! The find function returns the index of the first position that contains the given string.
  • 9
  • Incorrect! The find function returns the index of the FIRST position that contains the given string. Try again.
  • 2
  • Incorrect! Remember, indices start at 0, not 1. Try again.
  • pos
  • Incorrect! pos is a variable, so print will print its value. Try again.

3.

    11-9-3: What will be printed when the following executes?
    str = "This is fun"
    str = str[5]
    print(str)
    
  • i
  • Correct! This will print the character at position 5 in the string, which is i.
  • ' '
  • Incorrect! Remember, indices start at 0. Try again.
  • is fun
  • Incorrect! str[5] equals one specific character of the larger string. Try again.
  • This is fun
  • Incorrect! Line 2 permanently changes the value that the variable str is assigned to. Try again.

4.

    11-9-4: What is the value of s1 after the following code executes?
    s1 = "heY"
    s1 = s1.capitalize()
    s1.lower()
    
  • heY
  • Incorrect! Although strings are immutable, s1 gets reassigned to a different value than it begins with. Try again.
  • hey
  • Incorrect! Strings are immutable, so line 3 just returns a new string and doesn't modify the original. Try again.
  • HEY
  • Incorrect! The capitalize method doesn't capitalize the entire word. Try again.
  • Hey
  • Correct! The capitalize method capitalizes the first letter of the word and lowercases the rest. Then, line 3 returns a new string without modifying the original.

5.

    11-9-5: What would the following code print?
    Mali = 5
    print("Mali" + " is " + str(Mali))
    
  • Mali is Mali
  • Incorrect! There are no quotes around the last Mali, so str() will use the value of the variable Mali. Try again.
  • Mali is 5
  • Correct! The first Mali is in quotes, so it will print the string "Mali". The second Mali is not in quotes, so it will print the value of the variable Mali.
  • 5 is Mali
  • Incorrect! The first Mali is in quotes and the second is not. Try again.
  • 5 is 5
  • Incorrect! The first Mali is in quotes, so it is a string, not a variable. Try again.

6.

    11-9-6: What is printed by the following statements?
    s = "python rocks"
    print(s[3])
    
  • t
  • Incorrect! Indices start at 0, not 1. Try again.
  • h
  • Correct! Indices start with 0.
  • c
  • Incorrect! s[-3] would count from right to left and return c. Try again.
  • Error, you cannot use the [ ] operator with a string.
  • Incorrect! [ ] is the index operator and works with strings. Try again.

7.

    11-9-7: What is printed by the following statements?
    s = "python is awesome"
    print(s[2] + s[-5])
    
  • te
  • Correct! The indexing operator has precedence over concatenation.
  • tw
  • Incorrect! s[-1] is the last character of the string, so what is s[-5]? Try again.
  • o
  • Incorrect! The indexing operator happens before the two strings are concatenated. Try again.
  • Error, you cannot use the [ ] operator with the + operator.
  • Incorrect! [ ] operator returns a string that can be concatenated with another string. Try again.

8.

    11-9-8: What is printed by the following statements?
    s = "python rocks"
    print(len(s))
    
  • 11
  • Incorrect! The space counts as a character. Try again.
  • 12
  • Correct! len() returns the number of characters in the string, including spaces.
  • 1
  • Incorrect! s is a variable, not a character. Try again.
  • Error, missing quotes around s
  • Incorrect! Because s is the name of a string variable, len() can be used on it. Try again.

9.

    11-9-9: What is printed by the following statements:
    s = "Rose"
    s[1] = "i"
    print(s)
    
  • Rose
  • Incorrect! Assignment is not allowed with strings. Try again.
  • Rise
  • Incorrect! Assignment is not allowed with strings. Try again.
  • s
  • Incorrect! Assignment is not allowed with strings. Try again.
  • TypeError
  • Correct! Strings are immutable, so you cannot change an existing string.

10.

    11-9-10: What is printed by the following statements:
    s = "ball"
    r = ""
    for item in s:
        r = item.upper() + r
    print(r)
    
  • Ball
  • Incorrect! Each item is converted to upper case before concatenation. Try again.
  • BALL
  • Incorrect! Pay close attention to the order the characters will be in. Try again.
  • LLAB
  • Correct! The order is reversed due to the order of the concatenation.
  • TypeError
  • Incorrect! String concatenation is allowed. Try again.

11.

    11-9-11: What is printed by the following statements?
    s = "python rocks"
    print(s[7:11] * 3)
    
  • rockrockrock
  • Correct! s[7:11] = "rock", which is then repeated 3 times.
  • rock rock rock
  • Incorrect! s[7:11] is "rock", not " rock". Try again.
  • rocksrocksrocks
  • Incorrect! Slicing will not include the character at index 11, just the characters up to it. Try again.
  • TypeError, you cannot use multiplication with slicing.
  • Incorrect! Multiplying a string by an int is okay. Try again.

12.

    11-9-12: What is printed by the following statements?
    animal = "dog"
    print("animal" + animal)
    
  • dogdog
  • It will print "animal" first.
  • dog dog
  • There is no added space.
  • animal dog
  • There is no added space.
  • animaldog
  • Correct! It will print the characters in the string followed by the value of the variable animal without any space between.

13.

    11-9-13: What is printed by the following statements?
    animal = "dog"
    print("animal " + "animal")
    
  • animal animal
  • Since animal is in quotes it will print those exact letters.
  • animalanimal
  • There is a space in the first string.
  • animal dog
  • Notice that the second animal is also in quotes.
  • animaldog
  • Notice that the second animal is also in quotes and there is a space after the first string.