ch 11 practice questions Flashcards

1
Q

What will print?

t1 = (‘a’,)
print(type(t1))

t2 = (‘a’)
print(type(t2))

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

Do you need a comma for an empty tuple?

A

no

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

Which of these lines of code correctly creates a tuple?
a. t = ()
b. t = tuple()
c. t = tup()
d. t = ‘a’, ‘b’, ‘c’, ‘d’

A

a, b , d

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

Can you modify a tuple? what can you do instead?

A

no, you cannot modify a tuple
you can replace one tuple with another

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

What does the following print?
t = (‘a’, ‘b’, ‘c’, ‘d’, ‘e’)
t = (‘A’,) + t[1:]
print(t)

A

(‘A’, ‘b’, ‘c’, ‘d’, ‘e’)

example of replacing instead of modifying

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

What is returned? Why?
(5, 7, 3) < (7, 4, 2)

A

True, only compares the first values and ignores the rest (because the first one is true)

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

how does sort() work for a list of tuples?

A

sort() compares the first elements of a tuple, if they tie, it moves onto the second

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

What does DSU stand for?

A

Decorate: building a list of tuples with one or more sort metrics preceding the elements from the sequence

Sort: sort

Undecorate: extracting the sorted elements of the sequence

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

Identify DSU in the following Code:

txt = ‘but soft what light in yonder window breaks’
words = txt.split()

tupleList = []
for word in words:
tupleList.append((len(word), word))

tupleList.sort(reverse = True)

sortedWordsList = []
for length, word in tupleList:
sortedWordsList.append(word)

print(sortedWordsList)

A

decorate would be adding the len(word) and word in tupleList

sort is the use of the sort function

Undecorate:
sortedWordsList = []
for length, word in tupleList:
sortedWordsList.append(word)

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

What is associated with the variable ‘domain’ once the following code is run?

email = yeezy@hotmail.com
uname, domain = email.split(‘@’)

A

hotmail.com

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

what is the dictionary method .items()?

A

returns a list of tuples, where each tuple is a key-value pair.

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

what does the following print?

d = {‘a’:10, ‘b’:1, ‘c’:22}
t = list(d.items())
print(t)

A

[(‘a’, 10), (‘b’, 1), (‘c’, 22)]

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

How will the list below be sorted (if there is any order at all)?

grocery_dict = {‘apple’: 5, ‘pineapple’: 3, ‘chicken’: 8, ‘kiwi’: 7}
grocery_list = list(grocery_dict.items())
grocery_list.sort()

A

ascending order by key’s first letter

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

How will the contents of list “lst” be ordered after the following code is run?

d = {‘a’: 10, ‘b’: 15, ‘c’: 17, ‘d’: 4}
lst = []
for key, val in d.items():
lst.append( (val, key) )
print(lst)

A

no order b/c it’s hash key order

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

How will the following code be sorted once this code is run?

weather = {‘Reykjavik’: 60, ‘Buenos Aires’: 55, ‘Cairo’: 96, ‘Berlin’: 89, ‘Caloocan’: 78}
sorted_weather = weather.sort()

A

error, you can’t sort dictionaries

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

Which of these options can be keys of a dictionary? Select all that apply.

a) dictionaries
b) strings
c) tuples
d) integers
e) lists

A

B, C, D

17
Q

A(n) ________ is an error that is caused when a data structure has the wrong type, size, or composition

A

shape error