getting-started-files/questions.rst
author Anoop Jacob Thomas<anoop@fossee.in>
Thu, 21 Oct 2010 18:17:28 +0530
changeset 345 d82151cc11f5
parent 292 de96931fac21
permissions -rw-r--r--
Reviewed script getting started with files.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
245
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
     1
Objective Questions
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
     2
-------------------
217
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
     3
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
     4
.. A mininum of 8 questions here. 
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
     5
245
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
     6
1. What function is used to open a file?
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
     7
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
     8
   Answer: ``open``
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
     9
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    10
#. The ``open`` function returns a 
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    11
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    12
   a. string
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    13
   #. list
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    14
   #. file object
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    15
   #. function
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    16
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    17
   Answer: file object
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    18
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    19
#. ``open`` function opens a file by default in write mode. T or F?
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    20
   
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    21
   Answer: False
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    22
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    23
#. The ``read`` method reads a file and returns the contents
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    24
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    25
   a. string
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    26
   #. list
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    27
   #. file object
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    28
   #. None of the above
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    29
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    30
   Answer: string
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    31
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    32
#. Given a file with ``hello.txt``, which looks like 
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    33
   ::
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    34
     
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    35
     Hello, World!
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    36
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    37
   What is the value of content, at the end of this code block::
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    38
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    39
     f = open('hello.txt')
292
de96931fac21 reviewed questions
Nishanth <nishanth@fossee.in>
parents: 245
diff changeset
    40
     pre_content = f.read()
245
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    41
     content = f.read()
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    42
     f.close()
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    43
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    44
   Answer: It is a null string. 
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    45
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    46
#. The following code block prints each line of ``hello.txt``::
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    47
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    48
     f = open('hello.txt')
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    49
     for line in f.read():
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    50
         print line
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    51
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    52
   True or False?
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    53
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    54
   Answer: False
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    55
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    56
#. Given a file with ``hello.txt``, which looks like 
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    57
   ::
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    58
     
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    59
     Hello, World!
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    60
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    61
   What is the output of ::
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    62
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    63
     f = open('hello.txt')
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    64
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    65
     for line in f:
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    66
         print line
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    67
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    68
     for line in f:
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    69
         print line
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    70
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    71
     f.close()
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    72
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    73
   Answer: Hello, World! is printed once.
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    74
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    75
           .. The actual answer should talk about blank lines, but I'm
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    76
           .. not sure if we should get into such detail. 
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    77
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    78
           .. Should this be made a multiple-choice?
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    79
345
d82151cc11f5 Reviewed script getting started with files.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 292
diff changeset
    80
	   .. #[[Anoop: I think it will better if we make this multiple
d82151cc11f5 Reviewed script getting started with files.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 292
diff changeset
    81
	      choice]]
245
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    82
   
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    83
#. Given a file with ``hello.txt``, which looks like 
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    84
   ::
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    85
     
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    86
     Hello, World!
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    87
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    88
   What is the output of ::
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    89
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    90
     f = open('hello.txt')
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    91
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    92
     for line in f:
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    93
         print line
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    94
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    95
     f.close()
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    96
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    97
     for line in f:
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    98
         print line
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    99
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
   100
     f.close()
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
   101
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
   102
   Answer: Hello, World! is printed twice.
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
   103
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
   104
#. Given a file with ``hello.txt``, which looks like 
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
   105
   ::
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
   106
     
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
   107
     Hello, World!
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
   108
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
   109
   What is the output of ::
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
   110
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
   111
     f = open('hello')
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
   112
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
   113
     for line in f:
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
   114
         print line
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
   115
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
   116
     f.close()
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
   117
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
   118
     for line in f:
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
   119
         print line
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
   120
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
   121
     f.close()
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
   122
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
   123
   Answer: IOError - No such file or directory: 'hello'
217
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   124
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   125
245
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
   126
Larger Questions
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
   127
----------------
217
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   128
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   129
.. A minimum of 2 questions here. 
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   130
292
de96931fac21 reviewed questions
Nishanth <nishanth@fossee.in>
parents: 245
diff changeset
   131
1. What does ``f.read(size)`` do?
245
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
   132
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
   133
#. Print every alternate line of a file, starting at the first line. 
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
   134
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
   135
#. Print the file in reverse. Starting from the last line to the
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
   136
   first.