--- a/writing_python_scripts/questions.rst Tue Oct 12 11:26:09 2010 +0530
+++ b/writing_python_scripts/questions.rst Tue Oct 12 11:44:33 2010 +0530
@@ -1,90 +1,120 @@
Objective Questions
-------------------
- 1. If ``a = [1, 1, 2, 3, 3, 5, 5, 8]``. What is set(a)
+ 1. Which of the following variables contains the locations to search for
+ python modules
- a. set([1, 1, 2, 3, 3, 5, 5, 8])
- #. set([1, 2, 3, 5, 8])
- #. set([1, 2, 3, 3, 5, 5])
- #. Error
+ a. sys.pythonpath
+ #. sys.path
+ #. os.pythonpath
+ #. os.path
+
+ Answer: sys.path
- Answer: set([1, 2, 3, 5, 8])
-
- 2. ``a = set([1, 3, 5])``. How do you find the length of a?
+ 2. What is the type of ``sys.path``
- Answer: len(a)
+ a. list of strings
+ #. list of int
+ #. string
+ #. tuple of strings
- 3. ``a = set([1, 3, 5])``. What does a[2] produce?
+ Answer: list of strings
+
+ 3. The script ``utils.py`` is in one of locations of PYTHONPATH and contains
+ the following code:
- a. 1
- #. 3
- #. 5
- #. Error
+ def show(x):
+ print x
+
+ show("Hello World")
- Answer: Error
+ if __name__ == "__main__":
- 4. ``odd = set([1, 3, 5, 7, 9])`` and ``squares = set([1, 4, 9, 16])``. What
- is the value of ``odd | squares``?
+ show("Hello Test")
+
+ How do you import the file.
- Answer: set([1, 3, 4, 5, 7, 9, 16])
+ a. import utils
+ #. import utils.py
+ #. import /home/user/utils
+ #. import /home/user/utils.py
- 5. ``odd = set([1, 3, 5, 7, 9])`` and ``squares = set([1, 4, 9, 16])``. What
- is the value of ``odd - squares``?
+ Answer: import utils
- Answer: set([3, 5, 7])
+ 4. The script ``utils.py`` is in one of locations of PYTHONPATH and contains
+ the following code:
- 6. ``odd = set([1, 3, 5, 7, 9])`` and ``squares = set([1, 4, 9, 16])``. What
- is the value of ``odd ^ squares``?
+ def show(x):
+ print x
+
+ show("Hello World")
- Answer: set([3, 4, 5, 7, 16])
+ if __name__ == "__main__":
+
+ show("Hello Test")
- 7. ``odd = set([1, 3, 5, 7, 9])`` and ``squares = set([1, 4, 9, 16])``. What
- does ``odd * squares`` give?
+ How do you use the ``show`` function after doing ``import utils``
+
+ a. utils.show("hey")
+ #. show("hey")
+ #. utils.py.show("hey")
+ #. utils.py.show.py("hey")
- a. set([1, 12, 45, 112, 9])
- #. set([1, 3, 4, 5, 7, 9, 16])
- #. set([])
- #. Error
+ Answer: utils.show("hey")
+
+ 5. The script ``utils.py`` is in one of locations of PYTHONPATH and contains
+ the following code:
- Answer: Error
+ def show(x):
+ print x
- 8. ``a = set([1, 2, 3, 4])`` and ``b = set([5, 6, 7, 8])``. What is ``a + b``
+ show("Hello World")
+
+ if __name__ == "__main__":
- a. set([1, 2, 3, 4, 5, 6, 7, 8])
- #. set([6, 8, 10, 12])
- #. set([5, 12, 21, 32])
- #. Error
+ show("Hello Test")
+
+ How do you use the ``show`` function after doing ``from utils import show``
- 9. ``a`` is a set. how do you check if if a varaible ``b`` exists in ``a``?
+ a. utils.show("hey")
+ #. show("hey")
+ #. utils.py.show("hey")
+ #. utils.py.show.py("hey")
- Answer: b in a
-
- 10. ``a`` and ``b`` are two sets. What is ``a ^ b == (a - b) | (b - a)``?
+ Answer: show("hey")
- a. True
- #. False
+ 5. The script ``utils.py`` is in one of locations of PYTHONPATH and contains
+ the following code:
- Answer: False
-
+ def show(x):
+ print x
-Larger Questions
-----------------
+ show("Hello World")
+
+ if __name__ == "__main__":
- 1. Given that mat_marks is a list of maths marks of a class. Find out the
- no.of duplicates marks in the list.
+ show("Hello Test")
+
+ What is printed when you do ``import utils``
Answer::
+ Hello World
- unique_marks = set(mat_marks)
- no_of_duplicates = len(mat_marks) - len(unique_marks)
+ 6. The script ``utils.py`` is in one of locations of PYTHONPATH and contains
+ the following code:
+
+ def show(x):
+ print x
- 2. Given that mat_marks is a list of maths marks of a class. Find how many
- duplicates of each mark exist.
+ show("Hello World")
+
+ if __name__ == "__main__":
+
+ show("Hello Test")
+
+ What is printed when the script is executed.
Answer::
+ Hello World
+ Hello Test
- marks_set = set(mat_marks)
- for mark in marks_set:
- occurences = mat_marks.count(mark)
- print occurences - 1, "duplicates of", mark, "exist"
-