writing_python_scripts/questions.rst
changeset 335 d5248a15274c
parent 296 641a6ee868c0
child 465 78d20cd87c7e
equal deleted inserted replaced
334:4b1e81da1c80 335:d5248a15274c
     1 Objective Questions
     1 Objective Questions
     2 -------------------
     2 -------------------
     3 
     3 
     4  1. If ``a = [1, 1, 2, 3, 3, 5, 5, 8]``. What is set(a)
     4  1. Which of the following variables contains the locations to search for
       
     5     python modules
     5 
     6 
     6    a. set([1, 1, 2, 3, 3, 5, 5, 8])
     7    a. sys.pythonpath
     7    #. set([1, 2, 3, 5, 8])
     8    #. sys.path
     8    #. set([1, 2, 3, 3, 5, 5])
     9    #. os.pythonpath
     9    #. Error
    10    #. os.path
    10 
    11 
    11    Answer: set([1, 2, 3, 5, 8])
    12    Answer: sys.path
    12 
    13 
    13  2. ``a = set([1, 3, 5])``. How do you find the length of a?
    14  2. What is the type of ``sys.path``
    14 
    15 
    15    Answer: len(a)
    16    a. list of strings
       
    17    #. list of int
       
    18    #. string
       
    19    #. tuple of strings
    16 
    20 
    17  3. ``a = set([1, 3, 5])``. What does a[2] produce?
    21    Answer: list of strings
    18 
    22 
    19    a. 1
    23  3. The script ``utils.py`` is in one of locations of PYTHONPATH and contains
    20    #. 3
    24     the following code:
    21    #. 5
       
    22    #. Error
       
    23 
    25 
    24    Answer: Error
    26       def show(x):
       
    27           print x
    25 
    28 
    26  4. ``odd = set([1, 3, 5, 7, 9])`` and ``squares = set([1, 4, 9, 16])``. What
    29       show("Hello World")
    27     is the value of ``odd | squares``?
       
    28 
    30 
    29    Answer: set([1, 3, 4, 5, 7, 9, 16])
    31       if __name__ == "__main__":
    30 
    32 
    31  5. ``odd = set([1, 3, 5, 7, 9])`` and ``squares = set([1, 4, 9, 16])``. What
    33           show("Hello Test")
    32     is the value of ``odd - squares``?
       
    33 
    34 
    34    Answer: set([3, 5, 7])
    35     How do you import the file.
    35 
    36 
    36  6. ``odd = set([1, 3, 5, 7, 9])`` and ``squares = set([1, 4, 9, 16])``. What
    37    a. import utils
    37     is the value of ``odd ^ squares``?
    38    #. import utils.py
       
    39    #. import /home/user/utils
       
    40    #. import /home/user/utils.py
    38 
    41 
    39    Answer: set([3, 4, 5, 7, 16])
    42    Answer: import utils
    40 
    43 
    41  7. ``odd = set([1, 3, 5, 7, 9])`` and ``squares = set([1, 4, 9, 16])``. What
    44  4. The script ``utils.py`` is in one of locations of PYTHONPATH and contains
    42     does ``odd * squares`` give?
    45     the following code:
    43 
    46 
    44    a. set([1, 12, 45, 112, 9])
    47       def show(x):
    45    #. set([1, 3, 4, 5, 7, 9, 16])
    48           print x
    46    #. set([])
       
    47    #. Error
       
    48 
    49 
    49    Answer: Error
    50       show("Hello World")
    50 
    51 
    51  8. ``a = set([1, 2, 3, 4])`` and ``b = set([5, 6, 7, 8])``. What is ``a + b``
    52       if __name__ == "__main__":
    52 
    53 
    53    a. set([1, 2, 3, 4, 5, 6, 7, 8])
    54           show("Hello Test")
    54    #. set([6, 8, 10, 12])
       
    55    #. set([5, 12, 21, 32])
       
    56    #. Error
       
    57 
    55 
    58  9. ``a`` is a set. how do you check if if a varaible ``b`` exists in ``a``?
    56     How do you use the ``show`` function after doing ``import utils``
    59 
    57 
    60    Answer: b in a
    58    a. utils.show("hey")
       
    59    #. show("hey")
       
    60    #. utils.py.show("hey")
       
    61    #. utils.py.show.py("hey")
    61 
    62 
    62  10. ``a`` and ``b`` are two sets. What is ``a ^ b == (a - b) | (b - a)``?
    63    Answer: utils.show("hey")
    63 
    64 
    64    a. True
    65  5. The script ``utils.py`` is in one of locations of PYTHONPATH and contains
    65    #. False
    66     the following code:
    66 
    67 
    67    Answer: False
    68       def show(x):
       
    69           print x
    68 
    70 
       
    71       show("Hello World")
    69 
    72 
    70 Larger Questions
    73       if __name__ == "__main__":
    71 ----------------
       
    72 
    74 
    73  1. Given that mat_marks is a list of maths marks of a class. Find out the
    75           show("Hello Test")
    74     no.of duplicates marks in the list.
    76 
       
    77     How do you use the ``show`` function after doing ``from utils import show``
       
    78 
       
    79    a. utils.show("hey")
       
    80    #. show("hey")
       
    81    #. utils.py.show("hey")
       
    82    #. utils.py.show.py("hey")
       
    83 
       
    84    Answer: show("hey")
       
    85 
       
    86  5. The script ``utils.py`` is in one of locations of PYTHONPATH and contains
       
    87     the following code:
       
    88 
       
    89       def show(x):
       
    90           print x
       
    91 
       
    92       show("Hello World")
       
    93 
       
    94       if __name__ == "__main__":
       
    95 
       
    96           show("Hello Test")
       
    97 
       
    98     What is printed when you do ``import utils``
    75 
    99 
    76    Answer::
   100    Answer::
       
   101       Hello World
    77 
   102 
    78      unique_marks = set(mat_marks)
   103  6. The script ``utils.py`` is in one of locations of PYTHONPATH and contains
    79      no_of_duplicates = len(mat_marks) - len(unique_marks)
   104     the following code:
    80 
   105 
    81  2. Given that mat_marks is a list of maths marks of a class. Find how many
   106       def show(x):
    82     duplicates of each mark exist.
   107           print x
       
   108 
       
   109       show("Hello World")
       
   110 
       
   111       if __name__ == "__main__":
       
   112 
       
   113           show("Hello Test")
       
   114 
       
   115     What is printed when the script is executed.
    83 
   116 
    84    Answer::
   117    Answer::
       
   118       Hello World
       
   119       Hello Test
    85 
   120 
    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