LearnFrenzy
Sign in Take a free test

Sets · Exercise 2 of 4

Sets Predict the Output

  • 15 questions
  • Page 1 of 2
  • Answers explained
0Attempted
0Correct
0Wrong
Directions

Read each Python program and choose what it prints. Every program was run on Python 3, so the answers are the real output.

Question 1 Easy

What is the output of the following Python program?

main.py
s = {3, 1, 3, 2, 1}
print(len(s), sorted(s))
A 5 [1, 1, 2, 3, 3]
B 3 [3, 1, 2]
C 3 [1, 2, 3]
D 5 [1, 2, 3]
Question page & discussion
Question 2 Easy

What is the output of the following Python program?

main.py
print(type({}).__name__, type(set()).__name__, type({1}).__name__, type({1: 2}).__name__)
A dict set set dict
B set set set dict
C dict set set set
D set dict set dict
Question page & discussion
Question 3 Easy

What is the output of the following Python program?

main.py
a = {1, 2, 3, 4}
b = {3, 4, 5}
print(a | b, a & b, a - b, a ^ b)
A {1, 2, 3, 4, 5} {3, 4} {5} {1, 2, 5}
B {1, 2, 3, 4, 5} {3, 4} {1, 2} {1, 2, 5}
C {3, 4} {1, 2, 3, 4, 5} {1, 2} {1, 2, 5}
D {1, 2, 3, 4, 5} {3, 4} {1, 2} {3, 4}
Question page & discussion
Question 4 Medium

What is the output of the following Python program?

main.py
print({1, 2} <= {1, 2, 3}, {1, 2} < {1, 2}, {1, 2}.isdisjoint({3}), {1, 2, 3} >= {3})
A True True True True
B True False False True
C True False True True
D False False True True
Question page & discussion
Question 5 Easy

What is the output of the following Python program?

main.py
s = {1, 2}
s.add(2)
s.add(3)
s.discard(10)
print(s, len(s))
A {1, 2, 2, 3} 4
B KeyError
C {1, 2, 3, 10} 4
D {1, 2, 3} 3
Question page & discussion
Question 6 Easy

What is the output of the following Python program?

main.py
letters = set("mississippi")
print(len(letters), sorted(letters))
A 11 ['i', 'm', 'p', 's']
B 4 ['m', 'i', 's', 'p']
C 1 ['mississippi']
D 4 ['i', 'm', 'p', 's']
Question page & discussion
Question 7 Hard

What is the output of the following Python program?

main.py
print({1, True, 1.0, 2}, len({0, False, ""}), {True, 1})
A {1, True, 1.0, 2} 3 {True, 1}
B {1, 2} 2 {True}
C {1, 2} 1 {1}
D {True, 2} 2 {1}
Question page & discussion
Question 8 Medium

What is the output of the following Python program?

main.py
s = {1}
s.update([2, 3], (4,))
s.add((5, 6))
print(len(s), (5, 6) in s, 5 in s)
A 6 False True
B 5 True False
C 7 True True
D 5 False True
Question page & discussion
Question 9 Medium

What is the output of the following Python program?

main.py
print({x % 3 for x in range(10)}, sorted({len(w) for w in ["hi", "to", "sea", "a"]}))
A {0, 1, 2, 0, 1, 2, 0, 1, 2, 0} [2, 2, 3, 1]
B {1, 2, 3} [1, 2, 3]
C {0, 1, 2} [2, 3, 1]
D {0, 1, 2} [1, 2, 3]
Question page & discussion
Question 10 Medium

What is the output of the following Python program?

main.py
fs = frozenset([1, 2])
d = {fs: "pair"}
print(d[frozenset([2, 1])], fs | {3}, type(fs | {3}).__name__)
A pair {1, 2, 3} set
B KeyError
C pair frozenset({1, 2, 3}) set
D pair frozenset({1, 2, 3}) frozenset
Question page & discussion

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links