Added questions to getting started with files LO.
authorPuneeth Chaganti <punchagan@fossee.in>
Thu, 07 Oct 2010 15:11:38 +0530
changeset 245 3ed6ef2ea91f
parent 244 b67d5e07cab5
child 246 95e7682e9d43
Added questions to getting started with files LO.
getting-started-files/questions.rst
getting-started-files/script.rst
--- a/getting-started-files/questions.rst	Thu Oct 07 15:11:00 2010 +0530
+++ b/getting-started-files/questions.rst	Thu Oct 07 15:11:38 2010 +0530
@@ -1,17 +1,134 @@
-Objective
----------
+Objective Questions
+-------------------
 
 .. A mininum of 8 questions here. 
 
-1. Question 1
-2. Question 2
-3. Question 3
+1. What function is used to open a file?
+
+   Answer: ``open``
+
+#. The ``open`` function returns a 
+
+   a. string
+   #. list
+   #. file object
+   #. function
+
+   Answer: file object
+
+#. ``open`` function opens a file by default in write mode. T or F?
+   
+   Answer: False
+
+#. The ``read`` method reads a file and returns the contents
+
+   a. string
+   #. list
+   #. file object
+   #. None of the above
+
+   Answer: string
+
+#. Given a file with ``hello.txt``, which looks like 
+   ::
+     
+     Hello, World!
+
+   What is the value of content, at the end of this code block::
+
+     f = open('hello.txt')
+     content = f.read()
+     content = f.read()
+     f.close()
+
+   Answer: It is a null string. 
+
+#. The following code block prints each line of ``hello.txt``::
+
+     f = open('hello.txt')
+     for line in f.read():
+         print line
+
+   True or False?
+
+   Answer: False
+
+#. Given a file with ``hello.txt``, which looks like 
+   ::
+     
+     Hello, World!
+
+   What is the output of ::
+
+     f = open('hello.txt')
+
+     for line in f:
+         print line
+
+     for line in f:
+         print line
+
+     f.close()
+
+   Answer: Hello, World! is printed once.
+
+           .. The actual answer should talk about blank lines, but I'm
+           .. not sure if we should get into such detail. 
+
+           .. Should this be made a multiple-choice?
+
+   
+#. Given a file with ``hello.txt``, which looks like 
+   ::
+     
+     Hello, World!
+
+   What is the output of ::
+
+     f = open('hello.txt')
+
+     for line in f:
+         print line
+
+     f.close()
+
+     for line in f:
+         print line
+
+     f.close()
+
+   Answer: Hello, World! is printed twice.
+
+#. Given a file with ``hello.txt``, which looks like 
+   ::
+     
+     Hello, World!
+
+   What is the output of ::
+
+     f = open('hello')
+
+     for line in f:
+         print line
+
+     f.close()
+
+     for line in f:
+         print line
+
+     f.close()
+
+   Answer: IOError - No such file or directory: 'hello'
 
 
-Programming
------------
+Larger Questions
+----------------
 
 .. A minimum of 2 questions here. 
 
-1. Programming Assignment 1
-2. Programming Assignment 2
+1. f.read(size)
+
+#. Print every alternate line of a file, starting at the first line. 
+
+#. Print the file in reverse. Starting from the last line to the
+   first. 
--- a/getting-started-files/script.rst	Thu Oct 07 15:11:00 2010 +0530
+++ b/getting-started-files/script.rst	Thu Oct 07 15:11:38 2010 +0530
@@ -53,7 +53,8 @@
   f
 
 The file object shows, the file which is open and the mode (read
-or write) in which it is open. 
+or write) in which it is open. Notice that it is open in read only
+mode, here. 
 
 We shall first learn to read the whole file into a single
 variable. Later, we shall look at reading it line-by-line. We use
@@ -74,7 +75,7 @@
 
   pend
 
-Following is an (are) exercise(s) that you must do. 
+Following is an exercise that you must do. 
 
 %%1%% Split the variable into a list, ``pend_list``, of the lines in
 the file. Hint, use the tab command to see what methods the string
@@ -151,8 +152,10 @@
 statement. This will save us the trouble of closing the file, each
 time we open it. 
 
-for line in open('/home/fossee/pendulum.txt'):
-line_list.append(line)
+::
+
+  for line in open('/home/fossee/pendulum.txt'):
+      line_list.append(line)
 
 Let us see what ``line_list`` contains. 
 ::