sets/questions.rst
changeset 233 ab748264f726
equal deleted inserted replaced
232:da873a5ac918 233:ab748264f726
       
     1 Objective Questions
       
     2 -------------------
       
     3 
       
     4  1. If ``a = [1, 1, 2, 3, 3, 5, 5, 8]``. What is set(a)
       
     5 
       
     6    a. set([1, 1, 2, 3, 3, 5, 5, 8])
       
     7    #. set([1, 2, 3, 5, 8])
       
     8    #. set([1, 2, 3, 3, 5, 5])
       
     9    #. Error
       
    10 
       
    11    Answer: set([1, 2, 3, 5, 8])
       
    12 
       
    13  2. ``a = set([1, 3, 5])``. How do you find the length of a?
       
    14 
       
    15    Answer: len(a)
       
    16 
       
    17  3. ``a = set([1, 3, 5])``. What does a[2] produce?
       
    18 
       
    19    a. 1
       
    20    #. 3
       
    21    #. 5
       
    22    #. Error
       
    23 
       
    24    Answer: Error
       
    25 
       
    26  4. ``odd = set([1, 3, 5, 7, 9])`` and ``squares = set([1, 4, 9, 16])``. What
       
    27     is the value of ``odd | squares``?
       
    28 
       
    29    Answer: set([1, 3, 4, 5, 7, 9, 16])
       
    30 
       
    31  5. ``odd = set([1, 3, 5, 7, 9])`` and ``squares = set([1, 4, 9, 16])``. What
       
    32     is the value of ``odd - squares``?
       
    33 
       
    34    Answer: set([3, 5, 7])
       
    35 
       
    36  6. ``odd = set([1, 3, 5, 7, 9])`` and ``squares = set([1, 4, 9, 16])``. What
       
    37     is the value of ``odd ^ squares``?
       
    38 
       
    39    Answer: set([3, 4, 5, 7, 16])
       
    40 
       
    41  7. ``odd = set([1, 3, 5, 7, 9])`` and ``squares = set([1, 4, 9, 16])``. What
       
    42     does ``odd * squares`` give?
       
    43 
       
    44    a. set([1, 12, 45, 112, 9])
       
    45    #. set([1, 3, 4, 5, 7, 9, 16])
       
    46    #. set([])
       
    47    #. Error
       
    48 
       
    49    Answer: Error
       
    50 
       
    51  8. ``a = set([1, 2, 3, 4])`` and ``b = set([5, 6, 7, 8])``. What is ``a + b``
       
    52 
       
    53    a. set([1, 2, 3, 4, 5, 6, 7, 8])
       
    54    #. set([6, 8, 10, 12])
       
    55    #. set([5, 12, 21, 32])
       
    56    #. Error
       
    57 
       
    58  9. ``a`` is a set. how do you check if if a varaible ``b`` exists in ``a``?
       
    59 
       
    60    Answer: b in a
       
    61 
       
    62  10. ``a`` and ``b`` are two sets. What is ``a ^ b == (a - b) | (b - a)``?
       
    63 
       
    64    a. True
       
    65    #. False
       
    66 
       
    67    Answer: False
       
    68 
       
    69 
       
    70 Larger Questions
       
    71 ----------------
       
    72 
       
    73  1. Given that mat_marks is a list of maths marks of a class. Find out the
       
    74     no.of duplicates marks in the list.
       
    75 
       
    76    Answer::
       
    77 
       
    78      unique_marks = set(mat_marks)
       
    79      no_of_duplicates = len(mat_marks) - len(unique_marks)
       
    80 
       
    81  2. Given that mat_marks is a list of maths marks of a class. Find how many
       
    82     duplicates of each mark exist.
       
    83 
       
    84    Answer::
       
    85 
       
    86      marks_set = set(mat_marks)
       
    87      for mark in marks_set:
       
    88          occurences = mat_marks.count(mark)
       
    89          print occurences - 1, "duplicates of", mark, "exist"
       
    90