-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcs1331-exam3-practice.tex
519 lines (406 loc) · 14.1 KB
/
cs1331-exam3-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
513
514
515
516
517
518
519
\documentclass[addpoints,9pt]{exam}
\usepackage{verbatim, multicol, tabularx,graphicx}
\usepackage{amsmath,amsthm, amssymb, latexsym, listings}
\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 3 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.}
{}
\firstpagefooter{Page \thepage\ of \numpages}
{}
{Points available: \pointsonpage{ \thepage} -
points lost: \makebox[.5in]{\hrulefill} =
points earned: \makebox[.5in]{\hrulefill}.
Graded by: \makebox[.75in]{\hrulefill}}
\runningfooter{Page \thepage\ of \numpages}
{}
{Points available: \pointsonpage{ \thepage} -
points lost: \makebox[.5in]{\hrulefill} =
points earned: \makebox[.5in]{\hrulefill}.
Graded by: \makebox[.75in]{\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.
\end{itemize}
\section*{Note} 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.
\vfill
% Points Table
%\begin{center}
\addpoints
%\gradetable[h][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 Multiple Choice} Circle the letter of the best answer.
\begin{parts}
\part[2] Given the following code:
\begin{lstlisting}[language=Java]
ArrayList tasks = new ArrayList(10);
tasks.add("Eat");
tasks.add("Sleep");
tasks.add("Code");
\end{lstlisting}
How many more items can be added to {\tt tasks}?
\begin{choices}
\choice 0
\choice 7
\correctchoice as many as memory will allow, essentially unlimited
\choice None of the above.
\end{choices}
\part[2] What is true about the following code:
\begin{lstlisting}[language=Java]
ArrayList<Integer> myInts = new ArrayList<Integer>();
myInts.add(2);
myInts.add(3);
\end{lstlisting}
\begin{choices}
\choice It will not compile becuase no capacity was given in the {\tt ArrayList} constructor;
\choice It will not compile because you can only add reference variables to collections.
\correctchoice The {\tt int} arguments to {\tt add} will be auto-boxed to {\tt Integer}s.
\choice None of the above.
\end{choices}
\part[2] After the following lines execute:
\begin{lstlisting}[language=Java]
Map<String, String> capitals = new HashMap<>();
capitals.put("Georgia", "Atlanta");
capitals.put("Alabama", "Montgomery");
capitals.put("Florida", "Tallahassee");
capitals.put("Georgia", "Valdosta");
\end{lstlisting}
What would {\tt capitals.size()} return?
\begin{choices}
\correctchoice 3
\choice 4
\choice 8
\end{choices}
\part[2] After the following lines execute:
\begin{lstlisting}[language=Java]
Map<String, String> capitals = new HashMap<>();
capitals.put("Georgia", "Atlanta");
capitals.put("Alabama", "Montgomery");
capitals.put("Florida", "Tallahassee");
capitals.put("Tennessee", "Atlanta")
\end{lstlisting}
What would {\tt capitals.size()} return?
\begin{choices}
\choice 3
\correctchoice 4
\choice 8
\end{choices}
\end{parts}
\newpage
\question {\bf Multiple Choice} Circle the letter of the best answer.
\begin{parts}
\part[2] Given the following classes and variable initializations:
\begin{lstlisting}[language=Java]
public class A implements Comparable<A> { ... }
public class B extends A { ... }
public class MyComparator implements Comparator<A> { ... }
List<A> aList = ... ;
List<B> bList = ... ;
List<MyComparator> comparatorList = ... ;
\end{lstlisting}
and the signature of Collections.sort():
\begin{lstlisting}[language=Java]
public static <T extends Comparable<? super T>> void sort(List<T> list)
\end{lstlisting}
Which of the following lines will compile?
\begin{choices}
\choice {\tt Collections.sort(aList)}
\choice {\tt Collections.sort(bList)}
\choice {\tt Collections.sort(comparatorList)}
\correctchoice A and B above
\choice All of the above
\end{choices}
\part[2] Given the classes:
\begin{verbatim}
public interface Employee
public class SalariedEmployee implements Employee
public class HourlyEmployee implements Employee
public class SummerIntern extends HourlyEmployee
public class Company<T extends Employee>
\end{verbatim}
\vspace{.1in}
Which of the following lines will {\bf not} compile?
\begin{choices}
\choice {\tt Company<SalariedEmployee> company = new Company<>();}
\choice {\tt Company<HourlyEmployee> company = new Company<>();}
\choice {\tt Company<SummerIntern> company = new Company<>();}
\correctchoice All of the lines above will compile.
\end{choices}
\part[2] Consider the following class:
\begin{lstlisting}[language=Java]
public class MyCollection {
...
public Iterator() iterator() { ... }
}
\end{lstlisting}
What is true about the following code?
\begin{lstlisting}[language=Java]
MyCollection mc = new MyCollection();
mc.add(...);
...
for (Object element: mc) {
...
}
\end{lstlisting}
\begin{choices}
\choice It will compile and run without error.
\choice It will compile but produce a runtime error.
\correctchoice It will not compile.
\end{choices}
\end{parts}
\newpage
\question {\bf Multiple Choice} Circle the letter of the best answer. Assume {\tt Trooper} is defined as follows:
\begin{lstlisting}[language=Java]
public class Trooper {
private String name;
private boolean mustached;
public Trooper(String name, boolean hasMustache) {
this.name = name; this.mustached = hasMustache;
}
public String getName() { return name; }
public boolean hasMustache() { return mustached; }
public boolean equals(Object other) {
if (this == other) return true;
if (null == other || !(other instanceof Trooper)) return false;
Trooper that = (Trooper) other;
return this.name.equals(that.name) && this.mustached == that.mustached;
}
public int hashCode() { return super.hashCode(); }
}
\end{lstlisting}
And the following has been executed in the same scope as the code in the questions below:
\begin{lstlisting}[language=Java]
ArrayList<Trooper> troopers = new ArrayList<>();
troopers.add(new Trooper("Farva", true));
troopers.add(new Trooper("Farva", true));
troopers.add(new Trooper("Rabbit", false));
troopers.add(new Trooper("Mac", true));
\end{lstlisting}
\begin{parts}
\part[2] What would be the result of the statement {\tt Collections.sort(troopers)}?
\begin{choices}
\correctchoice The code will not compile.
\choice {\tt troopers} will be sorted in order by name.
\choice {\tt troopers} will be sorted in order by mustache, then name.
\choice {\tt troopers} will not have any duplicate elements.
\end{choices}
\part[2] After executing the statement {\tt Set<Trooper> trooperSet = new HashSet<>(troopers)}, what would be the value of {\tt trooperSet.contains(new Trooper("Mac", true))}?
\begin{choices}
\choice The code will not compile.
\choice {\tt true}
\correctchoice {\tt false}
\choice {\tt void}
\end{choices}
\part[2] Given the definitions of {\tt troopers} and {\tt trooperSet} above, what would {\tt troopers.size()} return?
\begin{choices}
\choice {\tt true}
\choice {\tt false}
\choice 3
\correctchoice 4
\end{choices}
\part[2] After the statement {\tt Set<String> stringSet = new HashSet<>(Arrays.asList("meow", "meow"))} executes, what would be the value of {\tt stringSet.size()}?
\begin{choices}
\choice {\tt true}
\choice {\tt false}
\correctchoice 1
\choice 2
\end{choices}
\end{parts}
\newpage
\question {\bf Short Answer}
\begin{parts}
\part[5] Given the definition of {\tt Trooper} and the {\tt ArrayList<Trooper> troopers} in the previous question, write a {\bf single statement} that sorts {\tt troopers} by mustache, then name using {\tt Collections}'s\\ {\tt public static <T> void sort(List<T> list, Comparator<? super T> c)} method. Assume that you have no helper objects to use. All the comparison logic must be in this statement.
\ifprintanswers
\begin{lstlisting}[language=Java]
Collections.sort(troopers, new Comparator<Trooper>() {
public int compare(Trooper a, Trooper b) {
if (a.hasMustache() && !b.hasMustache()) {
return 1;
} else if (b.hasMustache() && !a.hasMustache()) {
return -1;
} else {
return a.getName().compareTo(b.getName());
}
}
});
\end{lstlisting}
\else
\vspace{2.5in}
\fi
\part[5] Write a single statement that assigns to a variable named {\tt byMustacheThenName} an object that implements {\tt Comparator<Trooper>} using the methods\\
\begin{lstlisting}[language=Java]
<U extends Comparable<? super U>> Comparator<T>
comparing(Function<? super T,? extends U> keyExtractor)
<U extends Comparable<? super U>> Comparator<T>
thenComparing(Function<? super T,? extends U> keyExtractor)
\end{lstlisting}
from {\tt Comparator} and method references for {\tt Trooper}'s {\tt hasMustache()} and {\tt getName()} methods.
\ifprintanswers
\begin{lstlisting}[language=Java]
Comparator<Trooper> byMustacheThenName = Comparator
.comparing(Trooper::hasMustache)
.thenComparing(Trooper::getName);
\end{lstlisting}
\else
\vspace{2in}
\fi
\part[5] Following from the previous part, re-write the call to {\tt Collections}'s {\tt public static <T> void sort(List<T> list, Comparator<? super T> c)} from above using the helper object .
\ifprintanswers
\begin{lstlisting}[language=Java]
Collections.sort(troopers, byMustacheThenName);
\end{lstlisting}
\else
\vspace{1.5in}
\fi
\end{parts}
\newpage
\question {\bf Short Answer}
\begin{parts}
\part[5] Write a line of code that instantiates an {\tt ArrayList} object named {\tt labels} that can hold {\tt Label} elements (and only {\tt Label}s) with an initial capacity of 20 and does not produce any compiler errors or warnings. Assume necessary imports.
\begin{solution}[.5in]
\verb@ArrayList<Label> labels = new ArrayList<>(20);@
\end{solution}
\part[5] Continuing from the previous question, write a for-each loop that prints to the console the the text of each {\tt Label} in the {\tt labels} that is not disabled. Assume {\tt Label} has {\tt String getText()} and {\tt boolean isDisabled()} methods.
\begin{solution}[1.5in]
\begin{verbatim}
for (Label label: labels) {
if (!label.isDisabled()) {
System.out.println(label.getText());
}
}
\end{verbatim}
\end{solution}
%% \part[5] Write a stream pipeline that does the same thing as in the previous part above.
%% \begin{solution}[1.5in]
%% \begin{verbatim}
%% labels.stream()
%% .filter(e -> !isDisabled())
%% .forEach(e -> System.out.println(e.getText()))
%% \end{verbatim}
%% \end{solution}
\end{parts}
\newpage
\question[10] Fill in the {\tt hasNext()} and {\tt next()} methods in {\tt DynamicArrayIterator}. If {\tt hasNext()} returns {\tt false}, a call to {\tt next()} should throw a {\tt NoSuchElementException}, which as a no-arg constructor.
\begin{verbatim}
import java.util.Arrays;
import java.util.Iterator;
public class DynamicArray<E> implements Iterable<E> {
private class DynamicArrayIterator implements Iterator<E> {
private int cursor = 0;
public boolean hasNext() {
\end{verbatim}
\ifprintanswers
\begin{lstlisting}
return cursor <= lastIndex;
\end{lstlisting}
\else
\vspace{.3in}
\fi
\begin{verbatim}
}
public E next() {
\end{verbatim}
\ifprintanswers
\begin{lstlisting}
if (!hasNext()) { throw new NoSuchElementException(); }
E answer = get(cursor);
cursor++;
return answer;
\end{lstlisting}
\else
\vspace{.4in}
\fi
\begin{verbatim}
}
public void remove() { throw new UnsupportedOperationException(); }
}
private Object[] elements;
private int lastIndex;
public DynamicArray() { this(10); }
public DynamicArray(int capacity) {
this.elements = new Object[capacity];
lastIndex = -1;
}
public Iterator<E> iterator() {
return new DynamicArrayIterator();
}
public void add(E item) {
if (lastIndex == elements.length - 1) {
int newCapacity = elements.length * 2;
elements = Arrays.copyOf(elements, newCapacity);
}
elements[++lastIndex] = item;
}
public E get(int index) {
if ((index < 0) || (index > lastIndex)) {
throw new IndexOutOfBoundsException(new Integer(index).toString());
}
return (E) elements[index];
}
public int size() { return lastIndex + 1; }
}
\end{verbatim}
\end{questions}
\end{document}