-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathcs1331-exam1-practice.tex
512 lines (401 loc) · 14.5 KB
/
cs1331-exam1-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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
\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 1331 Exam 1 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\ 2014 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 an object-oriented programming test. Java is the required language. Java is case-sensitive. DO NOT WRITE IN ALL CAPS. A Java program in all caps will not compile. Good variable names and style are required. Comments are not required.
\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
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} } A public top-level Java class may be defined in a source file with any base name as long as the file extension is {\tt .java}.\\
\else
\part[1] \makebox[.25in]{\hrulefill} A public top-level Java class may be defined in a source file with any base name as long as the file extension is {\tt .java}.\\
\fi
\ifprintanswers
\part[1] \underline{ {\bf F} } Java identifiers can contain letters, digits, and the underscore symbol and may start with a digit.\\
\else
\part[1] \makebox[.25in]{\hrulefill} Java identifiers can contain letters, digits, and the underscore symbol and may start with a digit.\\
\fi
\ifprintanswers
\part[1] \underline{ {\bf F} } The statement {\tt int x = 3f/4f;} will compile, but the result will be truncated so that {\tt x} gets the value {\tt 0}.\\
\else
\part[1] \makebox[.25in]{\hrulefill} The statement {\tt int x = 3f/4f;} will compile, but the result will be truncated so that {\tt x} gets the value {\tt 0}.\\
\fi
\ifprintanswers
\part[1] \underline{ {\bf F} } In a for loop header, {\tt for (}{\it initializer}{\tt ;} {\it condition}{\tt ;} {\it update}{\tt )}, the Java compiler requires {\it initializer} to initialize a loop variable and {\it update} to update it.\\
\else
\part[1] \makebox[.25in]{\hrulefill} In a for loop header, {\tt for (}{\it initializer}{\tt ;} {\it condition}{\tt ;} {\it update}{\tt )}, the Java compiler requires {\it initializer} to initialize a loop variable and {\it update} to update it.\\
\fi
\ifprintanswers
\part[1] \underline{ {\bf T} } The declarations {\tt double scores[]} and {\tt double[] scores} are equivalent.\\
\else
\part[1] \makebox[.25in]{\hrulefill} The declarations {\tt double scores[]} and {\tt double[] scores} are equivalent.\\
\fi
\ifprintanswers
\part[1] \underline{ {\bf F} } Java arrays have a variable number of elements of mixed types.\\
\else
\part[1] \makebox[.25in]{\hrulefill} Java arrays have a variable number of elements of mixed types.\\
\fi
\ifprintanswers
\part[1] \underline{ {\bf F} } Given an array named {\tt scores}, the statement {\tt scores[scores.length + 1]} will not compile.\\
\else
\part[1] \makebox[.25in]{\hrulefill} Given an array named {\tt scores}, the statement {\tt scores[scores.length + 1]} will not compile.\\
\fi
\ifprintanswers
\part[1] \underline{ {\bf F} } Instance methods of a class can be called without first instaniating an object of the class.\\
\else
\part[1] \makebox[.25in]{\hrulefill} Instance methods of a class can be called without first instaniating an object of the class.\\
\fi
\ifprintanswers
\part[1] \underline{ {\bf F} } Every Java class has a default no-arg constructor in addition to the constructors you write yourself.\\
\else
\part[1] \makebox[.25in]{\hrulefill} Every Java class has a default no-arg constructor in addition to the constructors you write yourself.\\
\fi
\end{parts}
\newpage
\question {\bf Expression Evaluation}
\begin{parts}
For each expression below, write the value and then the Java data type of the evaluated legal expression in the space provided. Be exact. The type you give must be the {\bf exact spelling of a Java primitive type including uppercase vs lowercase as it would appear in your program.}\\\\
\ifprintanswers
\hspace{-.5in} Expression: {\tt 7 / 2}
\part[1] Calculated value: \underline{ 3 }\\
\part[1] Java primitive type: \underline{ {\tt int} }\\\\
\else
\hspace{-.5in} Expression: {\tt 7 / 2}
\part[1] Calculated value: \makebox[1in]{\hrulefill}\\
\part[1] Java primitive type: \makebox[1in]{\hrulefill}\\\\
\fi
\ifprintanswers
\hspace{-.5in} Expression: {\tt 64 - 16 * 2}
\part[1] Calculated value: \underline{ 32 }\\
\part[1] Java primitive type: \underline{ {\tt int} }\\\\
\else
\hspace{-.5in} Expression: {\tt 64 - 16 * 2}
\part[1] Calculated value: \makebox[1in]{\hrulefill}\\
\part[1] Java primitive type: \makebox[1in]{\hrulefill}\\\\
\fi
\ifprintanswers
\hspace{-.5in} Expression: {\tt 2.5f + 3.0 - 1.5f}
\part[1] Calculated value: \underline{ 4.0 }\\
\part[1] Java primitive type: \underline{ {\tt double} }\\\\
\else
\hspace{-.5in} Expression: {\tt 2.5f + 3.0 - 1.5f}
\part[1] Calculated value: \makebox[1in]{\hrulefill}\\
\part[1] Java primitive type: \makebox[1in]{\hrulefill}\\\\
\fi
\ifprintanswers
\hspace{-.5in} Expression: {\tt new Double(1) / 2}
\part[1] Calculated value: \underline{ 0.5 }\\
\part[1] Java primitive type: \underline{ {\tt double} }\\\\
\else
\hspace{-.5in} Expression: {\tt new Double(1) / 2}
\part[1] Calculated value: \makebox[1in]{\hrulefill}\\
\part[1] Java primitive type: \makebox[1in]{\hrulefill}\\\\
\fi
\ifprintanswers
\hspace{-.5in} Expression: {\tt true \&\& "Foo".equals("foo")}
\part[1] Calculated value: \underline{ {\tt false} }\\
\part[1] Java primitive type: \underline{ {\tt boolean} }\\
\else
\hspace{-.5in} Expression: {\tt true \&\& "Foo".equals("foo")}
\part[1] Calculated value: \makebox[1in]{\hrulefill}\\
\part[1] Java primitive type: \makebox[1in]{\hrulefill}\\
\fi
\end{parts}
\newpage
\question {\bf Multiple Choice} Circle the letter of the correct choice.
\begin{parts}
Given:
\begin{lstlisting}
public class Kitten {
private String name = "";
public Kitten(String name) {
name = name;
}
public String toString() {
return "Kitten: " + name;
}
public boolean equals(Object other) {
if (this == other) return true;
if (null == other) return false;
if (!(other instanceof Kitten)) return false;
Kitten that = (Kitten) other;
return this.name.equals(that.name);
}
}
\end{lstlisting}
Assume the following statements have been executed:
\begin{lstlisting}
Kitten maggie = new Kitten("Maggie");
Kitten fiona = new Kitten("Fiona");
Kitten fiona2 = new Kitten("Fiona");
Kitten mittens = fiona;
\end{lstlisting}
\part[2] What is the value of {\tt maggie}?
\begin{choices}
\correctchoice the address of a {\tt Kitten} object
\choice null
\choice automatically set to 0
\choice undefined
\end{choices}
\part[2] What is printed on the console after the following statement is executed?
\verb@ System.out.println(maggie.toString());@
\begin{choices}
\correctchoice Kitten:
\choice Kitten: null
\choice Kitten: Maggie
\end{choices}
\part[2] What is the value of the expression {\tt fiona == mittens)}?
\begin{choices}
\correctchoice true
\choice false
\end{choices}
\part[2] What is the value of the expression {\tt fiona.equals(fiona2) \&\& fiona == mittens}?
\begin{choices}
\correctchoice true
\choice false
\end{choices}
\part[2] After executing {\tt Kitten[] kittens = new Kitten[5];} , what is the value of {\tt kittens[0]} ?
\begin{choices}
\correctchoice null
\choice the address of a {\tt Kitten} object
\choice automatically set to 0
\choice undefined
\end{choices}
\end{parts}
\newpage
\question {\bf Tracing}
\begin{parts}
Consider the following code:
\begin{lstlisting}
public class StrangeLogic {
private static int counter = 0;
private static boolean incrementCounter() {
counter++;
return true;
}
public static void main(String args[]) {
boolean a = true, b = false;
if (b || incrementCounter()) {
System.out.print("Boo");
}
if ((a || b) && incrementCounter()) {
System.out.print(" ya!");
}
System.out.println(counter);
}
}
\end{lstlisting}
\part[5] What is printed when main is executed?
\begin{solution}[1in]
Boo ya! 2 ( 1 pt for Boo, 2 for ya!, 2 for 2)
\end{solution}
Consider the following code:
\begin{lstlisting}
public class AlGore {
public static void main(String[] args) {
String mystery = "mnerigpaba";
String solved = "";
int len = mystery.length();
for (int i = 0, j = len - 1; i < len/2; ++i, --j) {
solved += mystery.charAt(i) + mystery.charAt(j);
}
System.out.println(solved);
}
}
\end{lstlisting}
\part[5] What is printed when main is executed?
\begin{solution}[1in]
manbearpig
\end{solution}
\end{parts}
\newpage
\question {\bf Short Answer}
\begin{parts}
\part[2] Assume you have a Java class named {\tt Foo} that you want to be able to run from the command line. Write the header for the method you need to define in {\tt Foo} to make it executable from the command line.
\begin{solution}[.4in]
{\tt public static void main(String[] args)}
\end{solution}
\part[2] Assume you are at the command line in the directory of the file that contains the definition for a Java class named {\tt Foo}. Write the command that you would execute on the command line to compile {\tt Foo}.
\begin{solution}[.4in]
{\tt javac Foo.java}
\end{solution}
\part[2] If the command above executes successfully, what file will be produced?
\begin{solution}[.4in]
{\tt Foo.class}
\end{solution}
\part[2] Write the command that will execute the {\tt Foo} class you compiled above.
\begin{solution}[.4in]
{\tt java Foo}
\end{solution}
\part[3] What will the following code print?
\begin{lstlisting}[language=Java]
for (int i = 10; i > 0; i--);
System.out.print(``Meow! '');
\end{lstlisting}
\begin{solution}[.5in]
Meow!
\end{solution}
\part[4] Assume you have the following start of a Student class and Student constructor. Finish the constructor. Do not change the code that is given.
\begin{verbatim}
public class Student {
private String name;
private String email;
public Student(String name, String email) {
// Your code goes here
\end{verbatim}
\ifprintanswers
\begin{lstlisting}
this.name = name; // 2 point
this.email = email; // 2 point
\end{lstlisting}
\else
\vspace{.5in}
\fi
\begin{verbatim}
}
\end{verbatim}
\part[5] Convert the following {\tt for} loop to an equivalent {\tt while} loop.
\begin{lstlisting}[language=Java]
for (int i = 10; i > 0; i--) {
System.out.println(i);
}
\end{lstlisting}
\begin{solution}[1.5in]
\begin{lstlisting}[language=Java]
int i = 10; // 1 pts
while (i > 0) { // 1 pt
System.out.println(i); // 1 pt
i--; // 1 pt
}
\end{lstlisting}
\end{solution}
\end{parts}
\newpage
\question {\bf Complete the Method}
\begin{parts}
\part[5] Fill in the code for the following method that takes an array of numbers and returns the number of even numbers in the array argument. Your code should use a {\tt for} loop.
\begin{verbatim}
public int evens(int[] numbers) {
// Your code goes here
\end{verbatim}
\begin{solution}[3in]
\begin{verbatim}
int count = 0; // 1 point
for (int number: numbers) { // 1 point
if ((number % 2) == 0) { // 1 point
count++; // 1 point
}
}
return count; // 1 point
\end{verbatim}
\end{solution}
\begin{verbatim}
}
\end{verbatim}
\part[5] Fill in the code for the following method that takes an array of numbers and a number and returns {\tt true} if the array contains the number, {\tt false} otherwise. You will need a loop, and your loop must not execute more iterations than necessary, and you cannot use {\tt break} or {\tt continue}.
\begin{verbatim}
public boolean contains(int[] numbers, int n) {
// Your code goes here
\end{verbatim}
\begin{solution}[3in]
\begin{verbatim}
boolean found = false; // 1 point for initializations
int i = 0; // 1 point
do {
if (numbers[i] == n) { // 1 point for conditional setting
found = true; // of found to true
}
i++;
} while (!found && (i < numbers.length)) // 1 point for termination condition
return found; // 1 point for return
\end{verbatim}
\end{solution}
\begin{verbatim}
}
\end{verbatim}
\end{parts}
\end{questions}
\end{document}