Strings and dictionaries Flashcards

1
Q

Each element that is stored in a dictionary has two parts: ___

a list and a value.
a list and an index.
a key and a value.
a list and a class.

A

a key and a value

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

A dictionary can include the same value several times but cannot include the same key several times.

TRUE
FALSE

A

TRUE

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

You cannot use a for loop to iterate over the characters in a string.

TRUE
FALSE

A

FALSE

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

The values that are stored in a single dictionary can be of different types.

TRUE
FALSE

A

TRUE

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

The elements in a dictionary are stored in ascending order, by the keys of the key-value pairs.

FALSE

A

FALSE

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What will be assigned to the variable z after the following code executes?

x = '123 Panorama Ave'
z = x[4:]

’ Ave’
‘123 ‘
‘Panorama’
‘Panorama Ave’

A

‘Panorama Ave’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

To test if the user input contains only alphabetical letters, you can use the ________ string method.

isalpha()
ascii()
strip()
isletter()

A

isalpha()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

The ________ method returns all the dictionary key-value pairs in the form of a sequence of tuples.

items()
values()
tuples()
sequence()

A

items()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

If you try to retrieve a value from a dictionary using a nonexistent key, a KeyError exception is raised.

TRUE
FALSE

A

TRUE

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Dictionaries are immutable objects.

TRUE
FALSE

A

FALSE

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

In slicing, if the end index specifies a position beyond the end of the string, Python will use the length of the string instead.

TRUE
FALSE

A

TRUE

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

The following expression is valid: string[0] = ‘k’

TRUE
FALSE

A

FALSE

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

If a whole paragraph is included in a single string, the split() method can be used to obtain a list of the sentences in the paragraph.

TRUE
FALSE

A

TRUE

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

A dictionary can include the same value several times but cannot include the same key several times.

TRUE
FALSE

A

TRUE

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

If you try to retrieve a value from a dictionary using a nonexistent key, an IndexError exception is raised.

TRUE
FALSE

A

FALSE

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What are the valid indexes for the string ‘New York’?

0 through 8
-1 through 6
-1 through -8
0 through 7
A

0 through 7

-1 through -8

17
Q

What will be displayed after the following code executes?

mystr = ‘yes’
yourstr = ‘no’
mystr += yourstr * 2
print(mystr)

yesnono
yes + no * 2
yes + no yes + no
yesnoyesno
A

yesnono

18
Q

What will be assigned to the variable s_string after the following code executes?

special = '1357 Country Ln.'
s_string = special[ :4]
'7'
'7 Country Ln.'
'1357'
5
A

‘1357’

19
Q

What will be assigned to the string variable pattern after the following code executes?

i = 3
pattern = 'z' * (5 * i)
Nothing; this code is invalid
'zzzzz'
'z * 15'
'zzzzzzzzzzzzzzz'
A

‘zzzzzzzzzzzzzzz’

20
Q

Which method would you use to determine whether a certain substring is present in a string?

find(substring)
replace(string, substring)
endswith(substring)
startswith(substring)
A

find(substring)

21
Q

What list will be referenced by the variable list_strip after the following code executes?

my_string = '03/07/2018'
list_strip = my_string.split('/')
['3', '7', '2018']
['03', '07', '2018']
['3', '/', '7', '/', '2018']
['03', '/', '07', '/', '2018']
A

[‘03’, ‘07’, ‘2018’]

22
Q

What is the correct structure to create a dictionary of months where each month will be accessed by its month number (for example, January is month 1, April is month 4)?

{ 1, 2,... 12 : 'January', 'February',... 'December' }
{ 1 : 'January', 2 : 'February', ... 12 : 'December' }
{ 1 ; 'January', 2 ; 'February', ... 12 ; 'December'}
[ '1' : 'January', '2' : 'February', ... '12' : 'December' ]
A

{ 1 : ‘January’, 2 : ‘February’, … 12 : ‘December’ }

23
Q

What will be the result of the following code?

ages = {'Aaron' : 6, 'Kelly' : 3, 'Abigail' : 1 }
value = ages['Brianna']
KeyError
False
0
-1
A

KeyError

24
Q

Which would you use to get the number of elements in a dictionary?

sizeof
len
length
size
A

len

25
Q

In order to avoid KeyError exceptions, you can check whether a key is in the dictionary using the __________ operator.

is in
is not in
included
in
A

in