You are on page 1of 16

Crt CONTINENTAL MATHEMATICS LEAGUE

National Computer Science Contest

Spring 2006

ANSWER SHEET

Time Limit: 40 minutes

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

National Computer Science Contest in Java

Spring 2006

TIME LIMIT: 40 minutes

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.

The following interfaces and classes are used in several questions: *

pub l ic class ListNode


{
private Object value;
private ListNode next;

public ListNode (Object v, ListNode nx) { value Vi next nx; }

public Object getValue() { return valuei


public ListNode getNext() { return nexti

pub l ic void setValue(Object v) { value = Vi


public void setNext(ListNode nx) { next = nXi

public class TreeNode


{
private Object value;
private TreeNode left;
private TreeNode right;

public TreeNode(Object v) { value = Vi left = null; right null; }

public TreeNode(Object v, TreeNode ItJ TreeNode rt )


{ value = v; left = Iti right rt; }

public Object getValue() { return valuei }


public TreeNode getLeft() { return left; }
public TreeNode getRight() { return right;

public void setValue(Object v) { value = v; }


public void setLeft(TreeNode It) { le~t = It; }
public void setRight( TreeNode rt l { right = rt;

• 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

public interface Stack


(
boolean isEmpty();
void push(Object obj);
Object pop();
Object peekTop();

public class ArrayStacK implements Stack


(
private ArrayList arraYi

public ArrayStack() { array = new ArrayList();

public boolean isEmpty() { return array.size() O;}


public void push(Object x) { array.add(x)i }
public Obj ect pop () { return array. remove (array. si ze () - 1); }
public Object peekTop() { return array.get(array.size() - 1); )

public interface Queue


{
boolean isEmpty();
void enqueue(Object x);
Object dequeue();
Object peekFront()i

public class ListQueue implements Queue


(
private LinkedList list;

public ListQueue() { list = new LinkedList();

public boolean isEmpty() { return list.size() O;}


p ub lic void enqueue(Object x) ( list.addLast(x); }
p u blic Obj ect dequeue () { return list. removeFirst () ; _}
public Object peekFront() ( return list.getFirst(); }
CML National Computer Science Contest 2006 3

1. Which of the following statements DOES NOT print 2006?

(A) System.out.println("200" + 6);


(B) System.out.println(200 + "6");
(C) S Ystem. 0 u t . P r i n t 1 n ( (i n t) (2 00 6 * 1. 0 0 00 0 02) ) ;
CD) System.out.println(2006 * 2000000 / 2000000);
(E) System.out.println(2006 / 1000 * 1000 + 2006 % 1000);

2. What is the output from the following statement?

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

3. What is the output from the following code segment?

String la = "-La", da = "-Da", tempi


temp = la;
la += dai
da = tempi
System.out.println(la + da)i

(A) -La-Oa
(B) -Da-La
(C) -La-Da-La
CD) -La-Da-La-Da
(E) N one of the above

4. What is the output from the following code segment?

ArrayList numbers = new ArrayList();


numbers.add("l");
nWfibers.add("2") ;
numbers. a d d ( "3") i
nurnbers.add ( 1 4 ") ;
numbers.set(2, n umbers.remove( l ));
System.out.println(numbe r s) ;

(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?

int[] counts = new int[7);


int i = 0, inc = 1;

for (int k = 1; k <= 100; k++)


{
counts[i++) += inci
if (i >= counts.length)
{
i %= counts.length;
inc = -inc;

(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

6. The if-else statement

if (a <= c)
{
if (b >= c)
return true;
else
return false;

e~se

if (d >= a)
return true;
else
return false;

is equivalent to which of the following statements?

(A) return a <= c && b >= c && a > c && d >= a;


(B) return (a <= c l I b >= c) && (a > c I I d >= a) ;
(C) return a <= c && b >= C I I a > c && d < a;
(D) return ! ( (a <= c && b >= c) I I (a > c && d >= a) ; ;
(E) retur n ! ( (a > c I I b < c) && ( a <= c I I d < a} ) ;
CML National Computer Science Contest 2006 5

7. What is the output from the following statement?

System.out.println(rr123".substring (1 ) + "123 rr .substring ( 2)


+ "123 rr .subs t ring(3))i

(A) 23
(B) 1 23
(C) 233
(D) 123233
(E) 112123

8. Consider the following classes:

public class Greeting


{
public String getMessage( ) { return "How do you do, Mr. "i }
private String getName() { return "Smith"; }
public String toString() { retur n getMessage() + getName()i }

public class InformalGreeting extends Greeting


{
public String getMessage() { return "Hi, "; }
private String getName() { return "Bob"; }
public Stri ng toString() { return super.toString();

public class Veryl n rormalGreeting extends I n ro rmalGreeting


{
public Stri ng getMessage() { return "'sup, ";
private Str ing getName() { return "dude"; }

What does

System.out.println(new Veryl nformalGreeting())i

display?

(A) How do you do, Mr. Smith


(B) Hi, Bob
(C) 'sup, Bob
CD) 'sup, dude
(E) 'sup, Smith
6 CML National Computer Science Contest 2006

9. If Li kable extends Comparable, Friend extends Person, Person implements


Comparable, Friend implements Likable, and the variables Person stranger
and Friend friend are declared and initialized, which of the following statements is
NOT valid?

(A) Person x = friend;


(B) Comparable x = s~ra~ger;
(C) Comparable x = frie~d;
(D) Likable x = stranger;
(E) Likable x = friend;

10. Consider the following method my s te ry:

pub l ic String mystery(String strl, Str i ng str2)


{
if (strl.length() <= 1 I I str2.1engt h () <= 1)
return str2 + s t rl;
else
return mystery (str2.substring(1), strl.substring(l))j

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:

public class Word


{
protected String word;

public Word(String w) ( word = Wi


public String toString() { return word;

public class SecondWord extends Word


{
private String word;

public SecondWord(String wl, String w2) { < code not shown> }


public String toString() { < code not shown> }

Suppose

System.out.println(new SecondWord("Good rr , "day"));

prints

Good day

Which of the following statements could complete SecondWord's constructor and


toString method?

1. Constructor: super(w l ); word = w2; }


toString: return super.toString() + " " + word; }

n. Constructor: super(wl); word = w2;


toString: retc.rn s uper. word + If 11- + word;

m. Constnlctor: super. word = wI ; word w2; }


toString: return s up er.word + " II + word;

(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--;

(A) :'nt[ ] arr { 1 , 2, 3, -1, -2, -3}i


(8) int [] arr {-3, -2, -1, 1, 2, 3} ;
(C) int [] arr {-3, -2, I, -1, 2, 3};
(D) int [] arr {-3, 2, -I, 1, -2, 3} ;
(E) int [] arr { 3 , 2, 1, -1, -2, -3} i

14. The method f un is defined as follows:

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);

What does fu n ( 9) return?

(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?

String abc == "ABCDEFGHIJKLMNOPQRSTUVWXYZ"j

Queue(] qq = new Queue(2)i


qq[O] = new ListQueue();
qq(l) = new ListQueue();

Random rand = new Random()j


for (int i = 0; i < abc.length(); i++)
{
Character x = new Character(abc.charAt(i))j
qq[rand.nextlnt(2)) . enqueue (x) i

while(!qq[O] .isErnpty())
System.out.print(qq[O] .dequeue())i
while(!qq(l] .isEmpty(l)
System.out.print(qq[l] .dequeue())i

(A) Very likely ABCDEFGHIJKLMNOPQRSTUVWXYZ


(B) Very likely ZYXWVUTSRQPONMLKJIHGFEDCBA
(C) Very likely BADCFEHGJILKNMPORQTSVUXWZY
CD) A string of letters, arranged in increasing order, concatenated with a string of the
remaining letters of the alphabet, also arranged in increasing order.
(E) A string that is formed from the original string abc by swapping the two letters in
several (roughly half) of the 13 consecutive pairs of letters.

16. Consider the following base and derived classes:

public class B
(
public String with(B xl { return "8";

public class 0 extends B


{
public St ri ng with ( D x) return "D"; }

Vlhat is the result from the following code?

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)

Questions 18-19 use the following class Gift:

public class Gi ft implements Comparable


{
private int va10ei

p ubl ic Gift() { value = 0; }


p ub lic Gift(int v) { value = Vi }

pub lic int getValue() { return value;


p u blic int compareTo{Object other)
{
Gift otherGift = (Gift)o t heri
r et u rn this.value - ot h erGift.value; II Line 12

public String toString() { re t urn String.valueOf(getValue())i

18. The output from

Gift gif t l = ne w Gi f t( lOO)i


Gift gift2 = new Gift(75)i
System.out.println(giftl + I) " + gift2 + II II +
giftl.compareTo(gift2));

is, as expected,

100 75 25

Which one of the following alternative statements on Line 12 in Gi ft would cause a


syntax error or give a different output for this code?

(A) return value - otherGift.value;


(8) return value - otherGift.getValue();
(C) return getValue() - otherGift.getValue();
0)) return this.getValue() - otherGift.getValue()i
(E) All of the above compile with no errors and produce the same output.
CML National Computer Science Contest 2006 11

19. Consider the following subclass of Gift:

public class WrappedGift extends Gift


(
private Gift gift;

public WrappedGift(Gift g) { gift = g; }

public int getValue() { ret u rn gift.getValue();


public int compareT o (Object other)
{ return gif t .compareTo (other); }

public St r i n g toString () { return gift. toStri~g () ;

\\'hat is the output from the following code?

Gift giftl = new Gift(lCO);


Gift gif t 2 = ~ew WrappedGift(new Gift ( 75));
System.out . p r i ntl:'l(giftl + " + gif t 2 +
1/ II " +
g i ft l.compa~e T o ( gift2) ) ;

(A) Syntax error in the WrappedGi ft class


(B) 100 75 25
(C) 100 0 25
(D) 100 a 100
(E) 100 75 100

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?

1. removing the next object

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?

Set setOfSets = new HashSet();


Set s = ~ew TreeSet() i
s.acd(new lnteger(l))i
setOfSets.add(s}i
s.add(new Integer(2))i
setOfSets.add (s);
Systern.out.println(setOfSets);

(A) [[ 1 , 2] I 1, 2]]
[

(B) [[ 1 , 2], ( 1] ]
(C) [ [ 1], [ 1] ]
(D) [ [1, 2]]
(E) [ [1] ]

22. Consider the following code:

Map rn = new TreeMap();


m.put(new Integer(l), "One");
rn .put(new I nteger(2), "Two");
m.put{new Integer(3), "Three");

Which of the following code segments will print [One I Two, Three]?

1. Iterator it = m.keySet() .iterator();


String separator = " [ " ;

whi~e (it.hasNext()
{
System.out.print(separator + m.get(it.next(»);
separator = ", IT;

System.out.println("] ");

n. System.out.println{rn.keySet () );

TIIT. System.out .print l n(rn);

(A) None of the three


(B) I only
(C) IT only
(D) I and II
(E) I ar.d III
CML National Computer Science Contest 2006 13

23. Consider the following method that compares two binary trees:

public int compare(TreeNode rootl, TreeNode root2)


{
if (rootl != null && root2 != null)
return compare(rootl.getLeft(), root2.getLeft() +
co~pare{rootl.ge t Right(), root2.getRight(»;
else if (rootl != null)
return 1 + compare(rootl.getLeft(), null) +
compare(rootl.getRight(), null);
else if (root2 != null)
return -1 + compare (null, root2.getLeft(» +
compare (null, root2.getRi ght(} ) ;
else
retur~ 0;

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

What are the values returned by compare ( root I, root2),


compa~e(root2, root3),andcompare(rootl, root3)?

(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

Wnat is the output from the following code?

Stack stk = new ArrayStack()i

while (root != null II !stk.isEmpty())


{
if (root == null)
(
root = (TreeNode)stk . pop();
System.out.print(root.getValue());
root = roct.getRight();

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

National Computer Science Contest

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

You might also like