forked from nathankatica/python-data
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cs2316-exam2-practice.tex
422 lines (323 loc) · 10.9 KB
/
cs2316-exam2-practice.tex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
\documentclass[addpoints,9pt]{exam}
\usepackage{verbatim, multicol, tabularx,}
\usepackage{amsmath,amsthm, amssymb, latexsym, listings, qtree}
\lstset{frame=tb,
language=Java,
aboveskip=1mm,
belowskip=0mm,
showstringspaces=false,
columns=flexible,
basicstyle={\ttfamily},
numbers=none,
frame=single,
breaklines=true,
breakatwhitespace=true
}
\title{CS 2316 Exam 2 Practice}
\date{}
\setcounter{page}{0}
\begin{document}
\maketitle
\thispagestyle{head}
%% \firstpageheader{}
%% {\tiny Copyright \textcopyright\ 2014 All rights reserved. Duplication and/or usage for purposes of any kind without permission is strictly forbidden.}
%% {}
\runningheader{}
{\tiny Copyright \textcopyright\ 2017 All rights reserved. Duplication and/or usage for purposes of any kind without permission is strictly forbidden.}
{}
\footer{Page \thepage\ of \numpages}
{}
{Points available: \pointsonpage{\thepage} -
points lost: \makebox[.5in]{\hrulefill} =
points earned: \makebox[.5in]{\hrulefill}.
Graded by: \makebox[.5in]{\hrulefill}}
\ifprintanswers
\begin{center}
{\LARGE ANSWER KEY}
\end{center}
\else
\vspace{0.1in}
\hbox to \textwidth{Name (print clearly): \enspace\hrulefill}
\vspace{0.3in}
\hbox to \textwidth{Signature: \hrulefill}
\vspace{0.3in}
\hbox to \textwidth{GT account username (gtg, gth, msmith3, etc): \enspace\hrulefill}
\fi
\vfill
\begin{itemize}
\item Signing signifies you are aware of and in accordance with the {\bf Academic Honor Code of Georgia Tech}.
\item Calculators and cell phones are NOT allowed.
\item This is a Python programming test. Where asked for Python statements or expressions you must print them exactly as they would be typed in a Python source file or interactive shell.
\end{itemize}
\vfill
% Points Table
%\begin{center}
\addpoints
%\gradetable[v][pages]
%\end{center}
% Points Table
\begin{center}
\renewcommand{\arraystretch}{2}
\begin{tabularx}{\textwidth}{|l|c|X|X|X|}
\hline
Question & Points per Page & Points Lost & Points Earned & Graded By \\
\hline
Page 1 & \pointsonpage{1} & - & = &\\
\hline
Page 2 & \pointsonpage{2} & - & = &\\
\hline
Page 3 & \pointsonpage{3} & - & = &\\
\hline
Page 4 & \pointsonpage{4} & - & = &\\
\hline
Page 5 & \pointsonpage{5} & - & = &\\
\hline
Page 6 & \pointsonpage{6} & - & = &\\
\hline
Page 7 & \pointsonpage{7} & - & = &\\
\hline
TOTAL & \numpoints & - & = & \\
\hline
\end{tabularx}
\end{center}
\newpage
%\normalsize
\pointsinmargin
\bracketedpoints
\marginpointname{}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{questions}
\question {\bf True or False}
In each of the blanks below, write ``T'' if the statement beside the blank is true, ``F'' otherwise.\\
\begin{parts}
\ifprintanswers
\part[1] \underline{ {\bf F} } Comma-separated values (CSV) files have a tree structure, meaning they have elements that are nested to arbitray depths.\\
\else
\part[1] \makebox[.25in]{\hrulefill} Comma-separated values files have a tree structure, meaning they have elements that are nested to arbitray depths.\\
\fi
\ifprintanswers
\part[1] \underline{ {\bf F} } CSV files are binary files whose contents can only be viewed with proprietary software.\\
\else
\part[1] \makebox[.25in]{\hrulefill} CSV files are binary files whose contents can only be viewed with proprietary software.\\
\fi
\ifprintanswers
\part[1] \underline{ {\bf T} } Files in Python are buffered, meaning that they are not guaranteed to be completely written to disk until they are successfully closed.\\
\else
\part[1] \makebox[.25in]{\hrulefill} Files in Python are buffered, meaning that they are not guaranteed to be completely written to disk until they are successfully closed.\\
\fi
\ifprintanswers
\part[1] \underline{ {\bf F} } XML documents are not human-readable.\\
\else
\part[1] \makebox[.25in]{\hrulefill} XML documents are not human-readable.\\
\fi
\ifprintanswers
\part[1] \underline{ {\bf T} } XML documents can be edited with any text editor.\\
\else
\part[1] \makebox[.25in]{\hrulefill} XML documents can be edited with any text editor.\\
\fi
\ifprintanswers
\part[1] \underline{ {\bf T} } The fundamental data abstraction in relational databases is the table.\\
\else
\part[1] \makebox[.25in]{\hrulefill} The fundamental data abstraction in relational databases is the table.\\
\fi
\end{parts}
\newpage
\question {\bf Multiple Choice} Circle the letter of the correct choice.
\begin{parts}
\part[2] Given the following CSV file contents:
\begin{lstlisting}[language=Python]
'a'; 'b'; 'c'
'd'; 'e'; 'f'
'g'; 'e'; 'h'
\end{lstlisting}
What is the field delimiter?
\begin{choices}
\choice {\tt ','}
\correctchoice {\tt ';'}
\choice {\tt "'"'}
\choice {\tt '"'}
\end{choices}
\part[2] What is the quote character?
\begin{choices}
\choice {\tt ','}
\choice {\tt ';'}
\correctchoice {\tt "'"}
\choice {\tt '"'}
\end{choices}
\part[2] If you read the CSV file above with a {\tt csv.DictReader} like this:
\begin{lstlisting}[language=Python]
quote = ... # from question above
delim = ... # from question above
reader = csv.DictReader(csvfile, delimiter=delim, quotechar=quote)
\end{lstlisting}
what would it return for the first record?
\begin{choices}
\choice {\tt ['a', 'b', 'c']}
\choice {\tt ('a', 'b', 'c')}
\correctchoice {\tt \{'a': 'd', 'b': 'e', 'c': 'f'\}}
\choice {\tt \{None: 'a', None: 'b', None: 'c'\}}
\end{choices}
\part[2] Which of the following is {\bf not} well-formed XML?
\begin{choices}
\choice {\tt <a> <b> c </b> </a>}
\choice {\tt <a> <b> <c/> </b> </a>}
\correctchoice {\tt <a> <b> <c> </b> </a>}
\choice {\tt <a> <b> <c> d </c> </b> </a>}
\end{choices}
\part[2] A musician can be a member of many bands and a band can have many musicians. What kind of cardinality relationship exists between musicians and bands?
\begin{choices}
\choice one to one
\choice one to many
\correctchoice many to many
\end{choices}
\end{parts}
\newpage
\question {\bf Short Answer}
\begin{parts}
\part[5] Say you have a python program that was run from the command line with {\tt python my\_program one two three}. Within your program, how do you get the first command line argument {\it to your program} (not to the Python interpreter)?
\begin{solution}[1in]
\begin{verbatim}
import sys
first = sys.argv[1]
\end{verbatim}
\end{solution}
\part[5] Say you have a python program that was run from the command line. Write a snippet of Python code that checks whether any command line arguments were supplied to the Python program.
\begin{solution}[1in]
\begin{verbatim}
if len(sys.argv) > 1:
# at least one command line argument was supplied to the Python program
\end{verbatim}
\end{solution}
\part[5] Write a Python statement that opens a file for reading as text. The file name is supplied by the first command line argument.
\begin{solution}[1in]
\begin{verbatim}
fin = open(sys.argv[1], 'rt')
\end{verbatim}
\end{solution}
\part[5] Write a Python statement that opens a file for writing as text. The file name is the first command line argument with the text ".out" appended.
\begin{solution}[1in]
\begin{verbatim}
fout = open(sys.argv[1] + '.out', 'wt')
\end{verbatim}
\end{solution}
\end{parts}
\newpage
\question {\bf Short Answer}
\begin{parts}
Given the following table schema for a dive log:
\begin{lstlisting}[language=SQL]
create table dive (
timedate datetime, -- the day and start time of the dive
duration int, -- duration of the dive from descent to ascent
location char(32), -- the location and dive site name
buddy char(32), -- dive buddy
);
\end{lstlisting}
\part[5] Which field, or fields, would make the best primary key? Why?
\begin{solution}[1in]
\begin{verbatim}
timedate should be unique - can't do two dives at once
\end{verbatim}
\end{solution}
Given the following schema:
\begin{lstlisting}[language=SQL]
create table dog (
dogid int primary key,
name char(32) not null check(name != ''),
sex char not null check(sex = 'M' or sex = 'F'),
ownerid references owner(ownerid)
);
create table owner (
ownerid integer primary key autoincrement,
name char(32) not null check(name != '')
);
\end{lstlisting}
\part[5] Write a {\tt select} statement that will return all the male dogs.
\begin{solution}[1in]
\begin{verbatim}
select * from dog where sex = 'M';
\end{verbatim}
\end{solution}
\part[5] Write a {\tt select} statement that will return all the dogs owned by 'Tom'.
\begin{solution}[1in]
\begin{verbatim}
select * from dog join owner using (ownerid) where owner.name = 'Tom';
\end{verbatim}
\end{solution}
\part[5] Write a {\tt select} statement that will return all the dogs whose names start with 'Butter'.
\begin{solution}[1in]
\begin{verbatim}
select * from dog where name like 'Butter%;
\end{verbatim}
\end{solution}
\end{parts}
\newpage
\question[10] Write Python code that reads a CSV file that uses commas for delimiters, no quote characters and has no header row and stores the data as a list of lists. For example, for a file like:\\
\begin{verbatim}
a,b,c
d,e,f
\end{verbatim}
your code should create a list of lists like:
\begin{verbatim}
[['a', 'b', 'c'], ['d', 'e', 'f']]
\end{verbatim}
Use whatever Python programming techniques you wish.
\ifprintanswers
\begin{lstlisting}[language=Python]
import csv
with open(fine_name, 'r') as fin
reader = csv.reader(fin)
data = [line for line in reader]
\end{lstlisting}
Or
\begin{lstlisting}[language=Python]
import csv
data = []
with open(fine_name, 'r') as fin
reader = csv.reader(fin)
for line in reader:
data.append(line)
\end{lstlisting}
Or
\begin{lstlisting}[language=Python]
with open(fine_name, 'r') as fin
data = [line.split(',') for line in fin]
\end{lstlisting}
Or
\begin{lstlisting}[language=Python]
data = []
with open(fine_name, 'r') as fin
for line in fin:
data.append(line.split(','))
\end{lstlisting}
Or ...
\fi
\newpage
\question[10] Write a funtion called {\tt last\_name} that takes an {\tt ElemenetTree} element parameter and returns the text of the {\tt lastName} sub-element of the parameter. Assume that {\tt xml.etree.ElementTree} has been imported as {\tt et}.
\ifprintanswers
\begin{lstlisting}[language=Python]
def last_name(element):
return element.find("lastName").text
\end{lstlisting}
\fi
\newpage
\question[10] Write the create statements for a database of employees and departments. For employees store name, hire date, salary, and department. For department store the name, description, and the department head, who is an employee. Add any fields you need for keys and be sure to include foreign keys as necessary.
\ifprintanswers
\begin{lstlisting}[language=SQL]
create table employee (
empid int primary key,
name char(32),
hiredate date,
salary decimal(8,2),
dept_id references department(dept_id)
);
create table department (
dept_id integer primary key,
name char(32),
description char(64),
head references employee(emp_id)
);
\end{lstlisting}
\fi
\end{questions}
\end{document}