Strings(II): Indexing

7 September 2022
4.7 (114 reviews)
11 test answers

Unlock all answers in this set

Unlock answers (7)
question
Assume that name is a variable of type String that has been assigned a value. Write an expression whose value is the first character of the value of name. So if the value of name were "Smith" the expression's value would be 'S'.
answer
name[:1]
question
Write an expression that whose value is the fifth character of the String name.
answer
name[4:5]
question
Given the String variable name, write an expression that evaluates to the third character of name.
answer
name[2:3]
question
Given a String variable named sentence that has been initialized, write an expression whose value is the the very last character in the String referred to by sentence.
answer
sentence[-1]
question
Assume that name is a variable of type String that has been assigned a value. Write an expression whose value is the second character of the value of name. So if the value of name were "Smith" the expression's value would be 'm'.
answer
name[1:2]
question
Write an expression whose value is the str that consists of the third to last character of the str associated with s.
answer
s[-3]
question
Write an expression whose value is the character at index 3 of the str associated with s.
answer
s[3]
question
Write an expression whose value is the last character in the str associated with s.
answer
s[-1]
question
Write an expression whose value is the str that consists of the second to last character of the str associated with s.
answer
s[-2]
question
Assume that given, middle and family are three variables of type String that have been assigned values. Write an expression whose value is a String consisting of the first character of given followed by a period followed by the first character of middle followed by a period followed by the first character of family followed by a period: in other words, the initials of the name. So if the values of these three variables were "John" "Fitzgerald" "Kennedy", then the expression's value would be "J.F.K.".
answer
given[0]+'.'+middle[0]+'.'+family[0]+'.'
question
Assume that name is a variable of type String that has been assigned a value. Write an expression whose value is the last character of the value of name. So if the value of name were "Blair" the expression's value would be 'r'.
answer
name[-1]