14. Tuples, Sets, and Dictionaries - 37 Questions


14. Tuples, Sets, and Dictionaries

14. Tuples, Sets, and Dictionaries

37 Questions
Created by Y. Daniel Liang - http://www.cs.armstrong.edu/liang/index.html
Free
Your brain still hurting from the last quiz? Never worry, this one will require a little less thought. We're going back to something simple - variables - but a little more in depth.

Lists are what they seem - a list of values. Each one of them is numbered, starting from zero - the first one is numbered zero, the second 1, the third 2, etc. You can remove values from the list, and add new values to the end. Example: Your many cats' names.

Tuples are just like lists, but you can't change their values. The values that you give it first up, are the values that you are stuck with for the rest of the program. Again, each value is numbered starting from zero, for easy reference. Example: the names of the months of the year.

Dictionaries are similar to what their name suggests - a dictionary. In a dictionary, you have an 'index' of words, and for each of them a definition. In python, the word is called a 'key', and the definition a 'value'. The values in a dictionary aren't numbered - tare similar to what their name suggests - a dictionary. In a dictionary, you have an 'index' of words, and for each of them a definition. In python, the word is called a 'key', and the definition a 'value'. The values in a dictionary aren't numbered - they aren't in any specific order, either - the key does the same thing. You can add, remove, and modify the values in dictionaries. Example: telephone book.
  1. Suppose t = (1, 2, 4, 3), which of the following is incorrect?



    print(t[3])
    t[3] = 45
    print(max(t))
    print(len(t))

    View Answer | Discuss in forum
  2. Suppose t = (1, 2, 4, 3), t[1 : 3] is _________.



    (1, 2)
    (1, 2, 4)
    (2, 4)
    (2, 4, 3)

    View Answer | Discuss in forum
  3. Suppose t = (1, 2, 4, 3), t[1 : -1] is _________.



    (1, 2)
    (1, 2, 4)
    (2, 4)
    (2, 4, 3)

    View Answer | Discuss in forum
  4. Suppose t = (1, 2, 4, 3, 8, 9), [t[i] for i in range(0, len(t), 2)] is _________.



    [2, 3, 9]
    [1, 2, 4, 3, 8, 9]
    [1, 4, 8]
    (1, 4, 8)

    View Answer | Discuss in forum
  5. Suppose t = (1, 2), 2 * t is _________.



    (1, 2, 1, 2)
    [1, 2, 1, 2]
    (1, 1, 2, 2)
    [1, 1, 2, 2]

    View Answer | Discuss in forum
  6. Which of the following statements produces {'a', 'b', 'c'}?



    list("abac")
    tuple("abac")
    set("abac")
    None of these

    View Answer | Discuss in forum
  7. You can use ___________ to create an empty set.



    { }
    ( )
    [ ]
    set()

    View Answer | Discuss in forum
  8. Given two sets s1 and s2, s1 < s2 is _________.



    true if len(s1) is less than len(s2)
    true ifthe elements in s1 are compared less than the elements in s2.
    true if s1 is a proper subset of s2
    true if s1 is a proper superset of s2

    View Answer | Discuss in forum
  9. Suppose s = {1, 2, 4, 3}, _______ returns 4.



    sum(s)
    len(s)
    min(s)
    None

    View Answer | Discuss in forum
  10. Suppose s = {1, 2, 4, 3}, which of the following is incorrect?



    s[3] = 45
    print(max(s))
    print(len(s))
    print(sum(s))

    View Answer | Discuss in forum
  11. Suppose s = {1, 2, 4, 3}, what happens when invoking s.add(4)?



    There is no add method for a set object.
    This method is executed fine and 4 is added to the set.
    Since 4 is already in the set, Python raises a KeyError exception.
    This method is executed fine and 4 is not added to the set since 4 is already in the set.

    View Answer | Discuss in forum
  12. Suppose s = {1, 2, 4, 3}, what happens when invoking s.remove(12)?



    There is no remove method for a set object.
    This method is executed fine and no exception is raised.
    Since 12 is not in the set, Python raises a KeyError exception.
    You cannot remove an element from a set.

    View Answer | Discuss in forum
  13. Suppose s1 = {1, 2, 4, 3} and s2 = {1, 5, 4, 13}, what is s1 | s2?



    {1, 2, 4, 3, 1, 5, 4, 13}
    {1, 2, 4, 3, 5, 13}
    {1, 2, 4, 3}
    {1, 5, 4, 13}

    View Answer | Discuss in forum
  14. Suppose s1 = {1, 2, 4, 3} and s2 = {1, 5, 4, 13}, what is s1 - s2?



    {2, 3, 5, 13}
    {1, 2, 4, 3, 5, 13}
    {1, 2, 4, 3}
    {2, 3}

    View Answer | Discuss in forum
  15. Suppose s1 = {1, 2, 4, 3} and s2 = {1, 5, 4, 13}, what is s1 & s2?



    {2, 3, 5, 13}
    {1, 2, 4, 3, 5, 13}
    {1, 4}
    {1, 5, 4, 13}

    View Answer | Discuss in forum
  16. Suppose s1 = {1, 2, 4, 3} and s2 = {1, 5, 4, 13}, what is s1 ^ s2?



    {2, 3, 5, 13}
    {4, 3, 5, 13}
    {1, 4}
    {1, 5, 4, 13}

    View Answer | Discuss in forum
  17. Which of the following statement is false?



    Lists are mutable
    Tuples are mutable
    Sets are mutable
    None of these

    View Answer | Discuss in forum
  18. You can have duplicate elements in ________?



    list
    tuple
    set
    both list and tuple

    View Answer | Discuss in forum
  19. The elements in ________ are ordered?



    list
    tuple
    set
    both list and tuple

    View Answer | Discuss in forum
  20. Suppose s = {1, 2}, 2 * s is _________.



    (1, 2, 1, 2)
    [1, 2, 1, 2]
    (1, 1, 2, 2)
    illegal

    View Answer | Discuss in forum
  21. Suppose s1 = {1, 2, 4, 3} and s2 = {1, 5, 4, 13}, s1 + s2 is ____________?



    {1, 2, 4, 3, 1, 5, 4, 13}
    {1, 2, 4, 3, 5, 13}
    {1, 2, 4, 3}
    illegal

    View Answer | Discuss in forum
  22. Which of the following statements create a dictionary?



    d = {}
    d = {"john":40, "peter":45}
    d = {40:"john", 45:"peter"}
    All of the above

    View Answer | Discuss in forum
  23. Suppose d = {"john":40, "peter":45}, the keys are __________



    "john", 40, 45, and "peter"
    "john" and "peter"
    40 and 45
    d = (40:"john", 45:"peter")

    View Answer | Discuss in forum
  24. Suppose d1 = {"john":40, "peter":45} and d2 = {"john":466, "peter":45}, d1 == d2 is 



    True
    False
    illegal
    None of these

    View Answer | Discuss in forum
  25. Suppose d1 = {"john":40, "peter":45} and d2 = {"john":466, "peter":45}, d1 > d2 is 



    True
    False
    illegal
    None of these

    View Answer | Discuss in forum
  26. Suppose d = {"john":40, "peter":45}, d["john"] is __________



    40
    45
    "john"
    "peter"

    View Answer | Discuss in forum
  27. Suppose d = {"john":40, "peter":45}, to delete the entry for "john":40, use ________.



    d.delete("john":40)
    B. d.delete("john")
    C. del d["john"]
    D. del d("john":40)

    View Answer | Discuss in forum
  28. Suppose d = {"john":40, "peter":45}, to obtain the number of entries in dictionary, use ________.



    d.size()
    len(d)
    size(d)
    d.len()

    View Answer | Discuss in forum
  29. What will be displayed by the following code?


    d = {"john":40, "peter":45}

    print(list(d.keys()))



    ["john", "peter"]
    ["john":40, "peter":45]
    ("john", "peter")
    ("john":40, "peter":45)

    View Answer | Discuss in forum
  30. Suppose d = {"john":40, "peter":45}, what happens when retieving a value using d["susan"]?



    Since "susan" is not a value in the set, Python raises a KeyError exception.
    It is executed fine and no exception is raised, and it returns None.
    Since "susan" is not a key in the set, Python raises a KeyError exception.
    Since "susan" is not a key in the set, Python raises a syntax error.

    View Answer | Discuss in forum
  31. Suppose d = {"john":40, "peter":45}, what happens when retieving a value using d.get("susan")?



    Since "susan" is not a value in the set, Python raises a KeyError exception.
    It is executed fine and no exception is raised, and it returns None.
    Since "susan" is not a key in the set, Python raises a KeyError exception.
    Since "susan" is not a key in the set, Python raises a syntax error.

    View Answer | Discuss in forum
  32. Which of the following statements are true?



    A Python list is immutable if every element in the list is immutable.
    A Python set is immutable if every element in the set is immutable.
    A Python tuple is immutable if every element in the tuple is immutable.
    A Python tuple is immutable.

    View Answer | Discuss in forum
  33. Which of the following is a Python list?



    [1, 2, 3]
    (1, 2, 3)
    {1, 2, 3}
    {}

    View Answer | Discuss in forum
  34. Which of the following is a Python tuple?



    [1, 2, 3]
    (1, 2, 3)
    {1, 2, 3}
    {}

    View Answer | Discuss in forum
  35. Which of the following is a Python set?



    [1, 2, 3]
    (1, 2, 3)
    {1, 2, 3}
    {}

    View Answer | Discuss in forum
  36. Which of the following is a Python dictionary?



    [1, 2, 3]
    (1, 2, 3)
    {1, 2, 3}
    {}

    View Answer | Discuss in forum
  37. Which of the following sets is equal to {1, 2, 3}?



    {1, 2, 3}
    {2, 1, 3}
    {1, 2, 3, 2, 1, 3}
    All of the above

    View Answer | Discuss in forum