Ordering & Aliasing — Series 3

Preview — 3 of 10 questions

What is the output of the following code?

javascript
from collections import Counter

a = Counter(x=3, y=1)
b = Counter(x=1, y=2, z=5)
print(a & b)
print(a | b)
ACounter({'x': 1, 'y': 1}), Counter({'z': 5, 'x': 3, 'y': 2})
BCounter({'x': 4, 'y': 3, 'z': 5}), Counter({'x': 3, 'y': 1})
CCounter({'x': 1, 'y': 1, 'z': 5}), Counter({'x': 3, 'y': 2, 'z': 5})
DCounter({'x': 3, 'y': 2}), Counter({'x': 1, 'y': 1, 'z': 5})

What is the output of the following code?

javascript
print((1, "a") < (1, "b"))
print((1, "a") < (2, 3))

try:
    print((1, "a") < (1, 3))
except TypeError as e:
    print("TypeError:", e)
ATypeError: '<' not supported between instances of 'str' and 'int'
BTrue, True, then TypeError: '<' not supported between instances of 'str' and 'int'
CTrue, TypeError: '<' not supported between instances of 'str' and 'int'
DTrue, True, True

What is the output of the following code?

javascript
try:
    d = {[1, 2]: "value"}
except TypeError as e:
    print("TypeError:", e)

try:
    s = {[1, 2]}
except TypeError as e:
    print("TypeError:", e)

print({(1, 2): "value"})
A{[1, 2]: 'value'}, {[1, 2]}, {(1, 2): 'value'}
BTypeError: cannot use 'list' as a dict key (unhashable type: 'list'), {[1, 2]}, {(1, 2): 'value'}
CTypeError: cannot use 'list' as a dict key (unhashable type: 'list'), TypeError: cannot use 'list' as a set element (unhashable type: 'list'), {(1, 2): 'value'}
DTypeError: cannot use 'list' as a dict key (unhashable type: 'list'), TypeError: cannot use 'list' as a set element (unhashable type: 'list'), TypeError: cannot use 'tuple' as a dict key (unhashable type: 'tuple')

Sign up free to play

Answer all 10 questions (7 more), see explanations for every answer, and track your score.