Gaddis Python 2e Chapter 09
Download file with the answers
Gaddis Python 2e Chapter 09
1 file(s) 215.62 KB
If you are not a member register here to download this file
Chapter Nine
MULTIPLE CHOICE
1. What are the valid indexes for the string ‘New York’?
a. 0 through 7
b. 0 through 8
c. -1 through -8
d. -1 through 6
2. What will be assigned to s_string after the execution of the following code?
special = ‘1357 Country Ln.’
s_string = special[ :4]
a. ‘7’
b. ‘1357’
c. ‘1357 ‘
d. Invalid code
3. What will be assigned to s_string after the execution of the following code?
special = ‘1357 Country Ln.’
s_string = special[4: ]
a. ‘1357’
b. ‘Country Ln.’
c. ‘ Country Ln.‘
d. Invalid code
4. What will be assigned to the string variable even after the execution of the following code?
special = ‘0123456789’
even = special[0:10:2]
a. ‘13579’
b. ‘2468’
c. ‘02468’
d. Invalid code
5. What will be assigned to s_string after the execution of the following code?
special = ‘1357 Country Ln.’
s_string = special[-3: ]
a. ‘531’
b. ‘Ln.’
c. ‘ Ln.’
d. ‘7531’
6. If the start index is _____ the end index, the slicing expression will return an empty string.
a. equal to
b. less than
c. greater than
d. not equal to
7. Which of the following string methods can be used to determine if a string contains only ‘\n’ characters?
a. ischar()
b. isalpha()
c. istab()
d. isspace()
8. What is the return value of the string method lstrip()?
a. The string with all whitespace removed
b. The string with all leading spaces removed
c. The string with all leading tabs removed
d. The string with all leading whitespaces removed
9. What will be assigned to the string variable pattern after the execution of the following code?
i = 3
pattern = ‘z’ * (5*i)
a. ‘zzzzzzzzzzzzzzz’
b. ‘zzzzz’
c. Error: ‘*’ operator used incorrectly
d. The right side of the ‘*’ must be an integer.
10. Which list will be referenced by the variable list_strip after the execution of the following code?
list_string = ’03/07/2008’
list_strip = list_string.split(‘/’)
a. [‘3’, ‘7’, ‘2008’]
b. [‘03’, ‘07’, ‘2008’]
c. [‘3’, ‘/’, ‘7’, ‘/’, ‘2008’]
d. [‘03’, ‘/’, ‘07’, ‘/’, ‘2008’]
11. What is the first negative index in a string?
a. 0
b. -1
c. -0
d. Size of the string minus one
12. Which method would you use to determine whether a substring is present in a string?
a. endswith(substring)
b. find(substring)
c. replace(string, substring)
d. startswith(substring)
13. Which method would you use to determine whether a substring is the suffix of a string?
a. endswith(substring)
b. find(substring)
c. replace(string, substring)
d. startswith(substring)
14. What is the value of the variable string after the execution of the following code?
string = ‘abcd’
string.upper()
a. ‘abcd’
b. ‘Abcd’
c. ‘ABCD’
d. Invalid code
15. What is the value of the variable string after the execution of the following code?
string = ‘Hello’
string += ‘ world’
a. ‘Hello’
b. ‘ world’
c. ‘Hello world’
d. Invalid code
TRUE/FALSE
1. True/False: Invalid indexes do not cause slicing expressions to raise an exception.
ANS: T
2. True/False: Indexing works with both strings and lists.
ANS: T
3. True/False: Indexing of a string starts at 1, so the index of the first character is 1, the index of the second character is 2, and so forth.
ANS: F
4. True/False: The index – 1 identifies the last character in a string.
ANS: T
5. True/False: In slicing, if the end index specifies a position beyond the end of the string, Python will use the length of the string instead.
ANS: T
6. True/False: An expression of the form string[i] = ‘i’ is a valid expression.
ANS: F
7. True/False: If the + operator is used on strings, it produces a string that is the combination of the two strings used as its operands.
ANS: T
8. True/False: When accessing each character in a string, such as for copying purposes, you would typically use a while loop.
ANS: F
9. True/False: If a whole paragraph is included in a single string, the split() method can be used to obtain a list of the sentences included in the paragraph.
ANS: T
10. True/False: The strip() method returns a copy of the string with all leading whitespace characters removed, but does not remove trailing whitespace characters.
ANS: F
FILL IN THE BLANK
1. Each character in a string has a(n) _______________ which specifies its position in the string.
ANS: index
2. A(n) _______________ exception will occur if you try to use an index that is out of range for a particular string.
ANS: IndexError
3. Strings are _______________, which means that once a string is created, it cannot be changed.
ANS: immutable
4. A(n) _______________ is a span of characters that are taken from within a string.
ANS: slice
5. The _______________ operator can be used to determine whether one string is contained in another string.
ANS: in
6. The _______________ method returns true if the string contains only numeric digits.
ANS: isdigit()
7. When the operand on the left side of the * symbol is a string and the operand on the right side is an integer, it becomes the _______________ operator.
ANS: repetition
8. The _______________ method returns the list of the words in the string.
ANS: split()
9. The _______________ method returns a copy of the string with all alphabetic letters converted to lower case.
ANS: lower()
10. The third number in string slicing brackets represents the _______________ value.
ANS: step
Leave a reply