Introduction to Programming Using Python, Y. Daniel Liang

Many questions in this edition have been updated in the new edition. Please check with the publisher on the newest edition.

This quiz is for students to practice. A large number of additional quiz is available for instructors using Quiz Generator from the Instructor's Resource Website. Videos for Java, Python, and C++ can be found at https://yongdanielliang.github.io/revelvideos.html.

Chapter 14 Tuples, Sets, and Dictionaries


Section 14.2 Tuples
14.1  Suppose t = (1, 2, 4, 3), which of the following is incorrect?
A. print(t[3])
B. t[3] = 45
C. print(max(t))
D. print(len(t))
E. print(sum(t))

14.2  Suppose t = (1, 2, 4, 3), t[1 : 3] is _________.
A. (1, 2)
B. (1, 2, 4)
C. (2, 4)
D. (2, 4, 3)
E. (1, 2, 4, 3)

14.3  Suppose t = (1, 2, 4, 3), t[1 : -1] is _________.
A. (1, 2)
B. (1, 2, 4)
C. (2, 4)
D. (2, 4, 3)
E. (1, 2, 4, 3)

14.4  Suppose t = (1, 2, 4, 3, 8, 9), [t[i] for i in range(0, len(t), 2)] is _________.
A. [2, 3, 9]
B. [1, 2, 4, 3, 8, 9]
C. [1, 4, 8]
D. (1, 4, 8)
E. (2, 3, 9)

14.5  Suppose t = (1, 2), 2 * t is _________.
A. (1, 2, 1, 2)
B. [1, 2, 1, 2]
C. (1, 1, 2, 2)
D. [1, 1, 2, 2]
E. illegal

14.6  Suppose t1 = (1, 2, 4, 3) and t2 = (1, 2, 3, 4), t1 < t2 is ________.
A. True
B. False

Section 14.3 Sets
14.7  Which of the following statements produces {'a', 'b', 'c'}?
A. list("abac")
B. tuple("abac")
C. set("abac")
D. None

14.8  You can use ___________ to create an empty set.
A. { }
B. ( )
C. [ ]
D. set()

14.9  Given two sets s1 and s2, s1 < s2 is _________.
A. true if len(s1) is less than len(s2)
B. true ifthe elements in s1 are compared less than the elements in s2.
C. true if s1 is a proper subset of s2
D. true if s1 is a proper superset of s2
E. illegal

14.10  Suppose s = {1, 2, 4, 3}, _______ returns 4.
A. sum(s)
B. len(s)
C. min(s)
D. max(s)
E. None

14.11  Suppose s = {1, 2, 4, 3}, which of the following is incorrect?
A. print(s[3])
B. s[3] = 45
C. print(max(s))
D. print(len(s))
E. print(sum(s))

14.12  Suppose s = {1, 2, 4, 3}, what happens when invoking s.add(4)?
A. There is no add method for a set object.
B. This method is executed fine and 4 is added to the set.
C. Since 4 is already in the set, Python raises a KeyError exception.
D. You cannot add an element from a set.
E. This method is executed fine and 4 is not added to the set since 4 is already in the set.

14.13  Suppose s = {1, 2, 4, 3}, what happens when invoking s.remove(12)?
A. There is no remove method for a set object.
B. This method is executed fine and no exception is raised.
C. Since 12 is not in the set, Python raises a KeyError exception.
D. You cannot remove an element from a set.

14.14  Suppose s1 = {1, 2, 4, 3} and s2 = {1, 3, 4, 2}, ________ is true.
A. s1 == s2
B. s1 != s2

14.15  Suppose s1 = {1, 2, 4, 3} and s2 = {0, 1, 5, 3, 4, 2, 13}, ________ is true.
A. s1.issubset(s2)
B. s1.issuperset(s2)
C. s2.issubset(s1)
D. s2.issuperset(s1)

14.16  Suppose s1 = {1, 2, 4, 3} and s2 = {1, 5, 4, 13}, what is s1 | s2?
A. {1, 2, 4, 3, 1, 5, 4, 13}
B. {1, 2, 4, 3, 5, 13}
C. {1, 2, 4, 3}
D. {1, 5, 4, 13}

14.17  Suppose s1 = {1, 2, 4, 3} and s2 = {1, 5, 4, 13}, what is s1 - s2?
A. {2, 3, 5, 13}
B. {1, 2, 4, 3, 5, 13}
C. {1, 2, 4, 3}
D. {1, 5, 4, 13}
E. {2, 3}

14.18  Suppose s1 = {1, 2, 4, 3} and s2 = {1, 5, 4, 13}, what is s1 & s2?
A. {2, 3, 5, 13}
B. {1, 2, 4, 3, 5, 13}
C. {1, 4}
D. {1, 5, 4, 13}
E. {2, 3}

14.19  Suppose s1 = {1, 2, 4, 3} and s2 = {1, 5, 4, 13}, what is s1 ^ s2?
A. {2, 3, 5, 13}
B. {4, 3, 5, 13}
C. {1, 4}
D. {1, 5, 4, 13}
E. {2, 3}

14.20  Which of the following statement is false?
A. Lists are mutable
B. Tuples are mutable
C. Sets are mutable
D. Strings are mutable

14.21  You can have duplicate elements in a ________?
A. list
B. tuple
C. set

14.22  The elements in a ________ are ordered?
A. list
B. tuple
C. set

14.23  Suppose s = {1, 2}, 2 * s is _________.
A. (1, 2, 1, 2)
B. [1, 2, 1, 2]
C. (1, 1, 2, 2)
D. [1, 1, 2, 2]
E. illegal

14.24  Suppose s1 = {1, 2, 4, 3} and s2 = {1, 5, 4, 13}, s1 + s2 is ____________?
A. {1, 2, 4, 3, 1, 5, 4, 13}
B. {1, 2, 4, 3, 5, 13}
C. {1, 2, 4, 3}
D. {1, 5, 4, 13}
E. illegal

Section 14.5 Dictionary
14.25  Which of the following statements create a dictionary?
A. d = {}
B. d = {"john":40, "peter":45}
C. d = {40:"john", 45:"peter"}
D. d = (40:"john", 45:"peter")

14.26  Suppose d = {"john":40, "peter":45}, the keys are __________
A. "john", 40, 45, and "peter"
B. "john" and "peter"
C. 40 and 45
D. d = (40:"john", 45:"peter")

14.27  Suppose d = {"john":40, "peter":45}, "john" in d is __________
A. True
B. False

14.28  Suppose d1 = {"john":40, "peter":45} and d2 = {"john":466, "peter":45}, d1 == d2 is _______.
A. True
B. False
C. illegal

14.29  Suppose d1 = {"john":40, "peter":45} and d2 = {"john":466, "peter":45}, d1 > d2 is _______.
A. True
B. False
C. illegal

14.30  Suppose d = {"john":40, "peter":45}, d["john"] is __________
A. 40
B. 45
C. "john"
D. "peter"

14.31  Suppose d = {"john":40, "peter":45}, to delete the entry for "john":40, use ________.
A. d.delete("john":40)
B. d.delete("john")
C. del d["john"]
D. del d("john":40)

14.32  Suppose d = {"john":40, "peter":45}, to obtain the number of entries in dictionary, use ________.
A. d.size()
B. len(d)
C. size(d)
D. d.len()

14.33  What will be displayed by the following code?

d = {"john":40, "peter":45}
print(list(d.keys()))
A. ["john", "peter"]
B. ["john":40, "peter":45]
C. ("john", "peter")
D. ("john":40, "peter":45)

14.34  Suppose d = {"john":40, "peter":45}, what happens when retieving a value using d["susan"]?
A. Since "susan" is not a value in the set, Python raises a KeyError exception.
B. It is executed fine and no exception is raised, and it returns None.
C. Since "susan" is not a key in the set, Python raises a KeyError exception.
D. Since "susan" is not a key in the set, Python raises a syntax error.

14.35  Suppose d = {"john":40, "peter":45}, what happens when retieving a value using d.get("susan")?
A. Since "susan" is not a value in the set, Python raises a KeyError exception.
B. It is executed fine and no exception is raised, and it returns None.
C. Since "susan" is not a key in the set, Python raises a KeyError exception.
D. Since "susan" is not a key in the set, Python raises a syntax error.

14.36  Which of the following statements are true?
A. A Python list is immutable if every element in the list is immutable.
B. A Python set is immutable if every element in the set is immutable.
C. A Python tuple is immutable if every element in the tuple is immutable.
D. A Python tuple is immutable.

14.37  Which of the following is a Python list?
A. [1, 2, 3]
B. (1, 2, 3)
C. {1, 2, 3}
D. {}

14.38  Which of the following is a Python tuple?
A. [1, 2, 3]
B. (1, 2, 3)
C. {1, 2, 3}
D. {}

14.39  Which of the following is a Python set?
A. [1, 2, 3]
B. (1, 2, 3)
C. {1, 2, 3}
D. {}

14.40  Which of the following is a Python dictionary?
A. [1, 2, 3]
B. (1, 2, 3)
C. {1, 2, 3}
D. {}

14.41  Which of the following sets is equal to {1, 2, 3}?
A. {1, 2, 3}
B. {2, 1, 3}
C. {3, 2, 1}
D. {2, 3, 1}
E. {1, 2, 3, 2, 1, 3}