1 Objective |
1 Objective Questions |
2 --------- |
2 ------------------- |
3 |
3 |
4 .. A mininum of 8 questions here. |
4 .. A mininum of 8 questions here. |
5 |
5 |
6 1. Question 1 |
6 1. What function is used to open a file? |
7 2. Question 2 |
7 |
8 3. Question 3 |
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 |
|
81 #. Given a file with ``hello.txt``, which looks like |
|
82 :: |
|
83 |
|
84 Hello, World! |
|
85 |
|
86 What is the output of :: |
|
87 |
|
88 f = open('hello.txt') |
|
89 |
|
90 for line in f: |
|
91 print line |
|
92 |
|
93 f.close() |
|
94 |
|
95 for line in f: |
|
96 print line |
|
97 |
|
98 f.close() |
|
99 |
|
100 Answer: Hello, World! is printed twice. |
|
101 |
|
102 #. Given a file with ``hello.txt``, which looks like |
|
103 :: |
|
104 |
|
105 Hello, World! |
|
106 |
|
107 What is the output of :: |
|
108 |
|
109 f = open('hello') |
|
110 |
|
111 for line in f: |
|
112 print line |
|
113 |
|
114 f.close() |
|
115 |
|
116 for line in f: |
|
117 print line |
|
118 |
|
119 f.close() |
|
120 |
|
121 Answer: IOError - No such file or directory: 'hello' |
9 |
122 |
10 |
123 |
11 Programming |
124 Larger Questions |
12 ----------- |
125 ---------------- |
13 |
126 |
14 .. A minimum of 2 questions here. |
127 .. A minimum of 2 questions here. |
15 |
128 |
16 1. Programming Assignment 1 |
129 1. What does ``f.read(size)`` do? |
17 2. Programming Assignment 2 |
130 |
|
131 #. Print every alternate line of a file, starting at the first line. |
|
132 |
|
133 #. Print the file in reverse. Starting from the last line to the |
|
134 first. |