CML Contest
CML Contest
Spring 2006
ANSWER SHEET
Contestanfs name:
1. 13.
2. 14.
3. 15.
4. 16.
5. 17.
6. 18.
7. 19.
8. 20.
9. 21.
10. 22.
11. 23.
12. 24.
25.
CONTINENTAL MATHEMATICS LEAGUE
Spring 2006
Directions:
Asswne that all fragments of code in this exam have correct syntax, with all variables properly
declared and all required import statements present, unless stated otherwise. The code uses
only Java 1.4 features, out the answers are the same in Java 5.0.
• Adapted from The College Board's AP Computer Science AB: Quick Reference Guide and AP Computer
Science AB: Implementation Classes and Interfaces.
2 CML National Computer Science Contest 2006
System.out.println(
new Double((new Integer(200)) .intValue() / 6) .doubleValue())i
(A) 33
(B) 33.0
(C) 33.334
(D) 33.333333333333336
(E) ClassCastException
(A) -La-Oa
(B) -Da-La
(C) -La-Da-La
CD) -La-Da-La-Da
(E) N one of the above
(A) [1, 3, 2)
(B) [1, 2, 4 ]
(C) [ I, 3, 4]
(D) [2, I, 3, 4]
(E) None of the above
4 CML National Computer Science Contest 2006
5. What values are stored in the array counts after the following code segment has been
executed?
(A) 0, 0, 0, 0, 0, 0,
(B) 0, 0, 0, 0, 0, I, 1°
(C) I, 1, 0, 0, 0, 0, 0
(D) 14, 14, 14, 14, 14, 15, IS
(E) 15, 15, 14, 14, 14, 14, 14
if (a <= c)
{
if (b >= c)
return true;
else
return false;
e~se
if (d >= a)
return true;
else
return false;
(A) 23
(B) 1 23
(C) 233
(D) 123233
(E) 112123
What does
display?
What will
System.out.println(mystery(nGOOD", "IDEA"));
display?
CA) DA
(B) AD
(C) IGDOEOAD
(D) GDEAIOOD
(E) GIODOEDA
11. The integer values stored in a two-dimensional n-by-n array m have the following
property: for any two values in the array, m [rl] [cl] and m [r2] [c2], if rl < r2,
then m [rl] [cl ] < m [r2] [c2]. What is the worst-case time for an optimal
algoritlun that finds out whether a target value is in m?
(A) O(logn)
(B) O(n)
(C) o( (log n)2)
CD) O(n logn)
(E) O(n2)
CML National Computer Science Contest 2006 7
12. Consider the following class Word and its subclass Seco ndWord:
Suppose
prints
Good day
(A) I only
(B) II only
(C) I and II
(D) II and ill
(E) I, II, and ill
8 CML National Computer Science Contest 2006
13. Which of the following arrays arr will end up unsorted after the following code
segment has been executed?
int i = 0, j = arr.length ~;
while (i < j)
{
if (arr[i] < 0)
i ++;
else if (arr[j) > 0)
j--;
else
{
int temp = arr [i Ji
arr [ i] arr [j ];
arr [ j] tempi
i++;
j--;
p ub l ic int fu~int n)
{
if (n == 1)
re t urr. 1 ;
else
{
int m = n/2;
re t ur n f u n (m) + 2 * rn * (n-m} + fu n( n - m);
(A) 0
(B) 9
(C) 18
(D) 41
(E) 81
CML National Computer Science Contest 2006 9
15. Which of the following best describes the most likely output from the following code?
while(!qq[O] .isErnpty())
System.out.print(qq[O] .dequeue())i
while(!qq(l] .isEmpty(l)
System.out.print(qq[l] .dequeue())i
public class B
(
public String with(B xl { return "8";
B b new B();
=
B d =new D () i
System.out.println(b.with(b) + b.wit h (d) +
d.wit h( b ) + d.with(d));
CA) BBBB
(B) BDED
(C) BBBD
(D) BBDD
(E) Syntax error: undefined method
10 CML National Computer Science Contest 2006
17. Consider a linked list with n nodes that hold integer values arranged in ascending order.
What is the worst-case big-O of the best algoritm that detennines whether such a list has
several consecutive nodes whose values add up to a given sum?
(A) O(Iog n)
(B) O(n)
(C) O(n log n)
CD) O(n2)
(E) O(n3)
is, as expected,
100 75 25
20. Compare two implementations of priority queue: one as a heap, the other as a singly-
linked list sorted in order of decreasing priority. In terms of time (big-O), for which of
the fo llowing operations is an optimal linked list implementation not worse than an
optimal heap implementation?
n. adding an object of the highest possible priority (i.e., calling add (x) , where x
has the highest priority among all possible objects in the application)
m. adding an object of the lowest possible priority (i.e ., calling add (x) , where x has
the lowest priority among all possible obj ects in the application)
(A) I only
(B) II only
(C) Iandll
(D) II and ill
(E) I, II, and III
12 CML National Computer Science Contest 2006
21. Recall that in Java, two Sets are deemed equal when they have the same size and equal
elements. (More precisely, s 1 . equa 1 s (s 2) returns t rue if and only if
51. size () == s2. size () and for any x in sl, s2. contains (x) is true.) The
hashCode method for a Set returns the swn of hash Code values for all its elements.
What is, then, the output from the following code?
(A) [[ 1 , 2] I 1, 2]]
[
(B) [[ 1 , 2], ( 1] ]
(C) [ [ 1], [ 1] ]
(D) [ [1, 2]]
(E) [ [1] ]
Which of the following code segments will print [One I Two, Three]?
whi~e (it.hasNext()
{
System.out.print(separator + m.get(it.next(»);
separator = ", IT;
System.out.println("] ");
n. System.out.println{rn.keySet () );
23. Consider the following method that compares two binary trees:
Suppose roo tl , root2, and root3 refer to the roots of the following binary trees,
respectively:
0 0 0
\
/ \ / \ / \
0 0 0 c 0 0
/ \ / / \ / \
0 0 0 0 0 0 0
/ \
0 0
(A) 0, 0, 0
(B) 0, -I, -1
(C) -1, -I, -1
(D) -I, -I, -2
(E) 1, I, 1
14 CAlL National Computer Science Contest 2006
24. Suppose TreeNode roc>t refers to the root of the following binary tree:
A
/ \
B C
/ \
o
else
{
stk.push(root) ;
root = root.getLeft();
(A) ABCDE
(B) ABDEC
(C) DBEAC
CD) DEBCA
(E) None of the above
25. On the eve of a multiple-choice exam, the students learn that the exam consists of25
questions with five answer choices for each, and that the answer key happens to b~ a
periodic sequence in which the same sequence of five letters (not necessarily all
different) repeats five times. Jim Guesling decides not to study at all, but he comes up
with an optimal strategy that guarantees him a certain number of correct answers. Grace
Hoper, on the other hand, has studied very hard and believes, with a good reason, that
she can answer, on average, 9 out of 10 questions correctly. Nevertheless, she decides
to take advantage of the new infonnation about the answer key to further improve her
result and comes up with an optimal strategy to do so. How many questions is Jim sure
to get right) and how many questions is Grace very likely to get right (with, say, 99%
percent certainty)?
(A) 5 and 24
(B) 5 and 25
(C) 10 and 24
(D) 15 and 24
(E) 15 and 25
CIt CONTINENTAL MATHEMATICS LEAGUE
Spring 2006
1. 0 13. A
2. B 14. E
3. C 15. 0
4. A 16. A
5. C 17. B
6. E 18. E
7. C 19. E
8. E 20. C
9. 0 21. A
10. A 22. B
11. B 23. 0
12. C 24. C
25. B