quiz 4 Flashcards

1
Q

What is the output after the following statements?

x = list(‘abcdefgh’)
print(x[::-1][1:4])

A

[‘g’,’ f’,’ e’]

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

What will be the result of the following code in Python:

x = { “a”: 1, “b”: 2, “c”: }
print(x)

A

Generates an error, because dictionary keys must have a value.

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

Which statement is illegal in Python?
a. x = {(1, ), (2, )}

b. y = {[1], [2]}

c. y = {1, 2}

d. All are legal.

A

b. y = {[1], [2]}

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

Which collection is immutable but could contain mutable elements?

A

tuple

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

Which statement is illegal in Python?

a. x = {1 : 2, 3 : 4}

b. x = {(1, ) : 2, (3, ) : 4}

c. x = {[1] : 2, [3] : 4}

d. x = {1 : [2], 3 : [4]}

A

c. x = {[1] : 2, [3] : 4}

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

What is the output after the following?

x = [3,2,1]
id_1 = id(x)
x.sort()
id_2 = id(x)

y = [3,2,1]
id_3 = id(y)
y = sorted(y)
id_4 = id(y)

print(id_1 == id_2, id_3 == id_4)

A

True False

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

Which of the following does not exist in Python?

a. list comprehension

b. tuple comprehension

c. set comprehension

d. dictionary comprehension

A

b. tuple comprehension

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

If x is a list like this:

x = [1, 2, 3, 4]

And we want x to have this value instead:

[1, 2, 3, 4, 5]

Which of the following statements will achieve this?

A

x.append(5)

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

What is the output of the following?

x = [3, 4, 1, 220, 1, 225, 1, 30]
print(x.index(1))

A

4

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

If x is a dictionary and I call x.items(), I will get back the key-value pairs as lists.

A

False

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