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