getting-started-files/questions.rst
author Nishanth <nishanth@fossee.in>
Tue, 12 Oct 2010 11:19:09 +0530
changeset 333 91b427241f8f
parent 292 de96931fac21
child 345 d82151cc11f5
permissions -rw-r--r--
Finished sets
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
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    80
   
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    81
#. 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
    82
   ::
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    83
     
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    84
     Hello, World!
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
   What is the output of ::
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
     f = open('hello.txt')
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
     for line in f:
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    91
         print line
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    92
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    93
     f.close()
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
     for line in f:
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    96
         print line
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    97
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    98
     f.close()
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
   Answer: Hello, World! is printed twice.
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
#. 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
   103
   ::
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
   104
     
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
   105
     Hello, World!
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
   What is the output of ::
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
     f = open('hello')
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
     for line in f:
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
   112
         print line
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
   113
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
   114
     f.close()
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
     for line in f:
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
   117
         print line
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
   118
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
   119
     f.close()
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
   Answer: IOError - No such file or directory: 'hello'
217
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   122
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   123
245
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
   124
Larger Questions
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
   125
----------------
217
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   126
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   127
.. A minimum of 2 questions here. 
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   128
292
de96931fac21 reviewed questions
Nishanth <nishanth@fossee.in>
parents: 245
diff changeset
   129
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
   130
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
   131
#. 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
   132
3ed6ef2ea91f Added questions to getting started with files LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
   133
#. 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
   134
   first.