CML Contest

Embed Size (px)

Citation preview

  • 8/14/2019 CML Contest

    1/16

    Crt CONTINENTAL MATHEMATICS LEAGUENational Computer Science ContestSpring 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.

  • 8/14/2019 CML Contest

    2/16

    CONTINENTAL MATHEMATICS LEAGUENational Computer Science Contest in Java

    Spring 2006

    TIME LIMIT: 40 minutesDirections:Asswne that all fragments of code in this exam have correct syntax, with all variables properlydeclared and all required import statements present, unless stated otherwise. The code usesonly 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 c lass ListNode{

    pr iva te Objec t value;p r iv a t e ListNode next ;publ ic ListNode (Object v, ListNode nx) { va luepubl ic Object ge tValue() { re turn valueipubl ic ListNode ge tNext ( ) { re tu rn next ipub l i c void se tValue(Object v) { va lue = V ipubl ic void se tNext (Lis tNode nx) { next = nX i

    publ ic c lass TreeNode{

    pr iva te Objec t value;pr iva te TreeNode l e f t ;p r iv a t e TreeNode r ight ;

    V i next

    publ ic TreeNode(Object v) { value = Vi l e f t = nu l l ; r i gh tpubl ic TreeNode(Object v, TreeNode I tJ TreeNode r t ){ va lue = v; l e f t = I t i r i gh tpubl ic Object getValue() { re turn valuei }pub l ic TreeNode ge tLef t ( ) { r e tu rn l e f t ; }publ ic TreeNode getRight( ) { re tu rn r i gh t ;publ ic void se tValue(Object v) { va lue = v; }publ ic void se tLef t (TreeNode I t ) { l e ~ t = I t ; }publ ic void se tRigh t (TreeNode r t l { r i gh t = r t ;

    nx; }

    nu l l ; }

    r t ; }

    Adapted from The College Board's AP Computer Science AB: Quick Reference Guide and AP ComputerScience AB: Implementation Classes and Interfaces.

  • 8/14/2019 CML Contest

    3/16

    2 CML National Computer Science Contest 2006

    pub l ic i n t e r f a c e Stack( boolean isEmpty();void push(Objec t ob j ) ;Objec t pop() ;Object peekTop() ;

    publ ic c lass ArrayStacK implements Stack(

    pr iva te Ar r ayL i s t arraYipub l ic ArrayStack( ) { ar r ay = new ArrayLis t ( ) ;pub l ic boolean isEmpty() { r e tu rn a r r a y . s i z e ( ) O;}pub l ic void push(Objec t x) { ar r ay .add(x ) i }pub l ic Ob j ec t pop () { r e tu rn ar ray . remove ( a r ray . s i ze () - 1 ) ; }pub l ic Objec t peekTop() { r e tu rn a r r ay .ge t ( a r r ay . s i ze ( ) - 1 ) ; )

    publ ic i n t e r f a c e Queue{ boolean isEmpty();void enqueue(Object x);Object dequeue() ;Objec t peekFront( ) i

    pub l ic c lass ListQueue implements Queue(

    pr iva te LinkedLis t list;pub l ic Lis tQueue() { list = new LinkedLis t ( ) ;pub l ic boolean isEmpty() { r e tu rn l i s t . s i z e ( ) O;}p ub l i c void enqueue(Object x) ( l i s t . a d d La s t ( x ) ; }p ub l i c Ob j e c t dequeue () { r e tu rn list. removeFirst () ; _}publ ic Objec t peekFront ( ) ( r e tu rn l i s t . g e t F i r s t ( ) ; }

  • 8/14/2019 CML Contest

    4/16

    CML National Computer Science Contest 2006

    1. Which of the following statements DOES NOT print 2006?(A) System.out .pr int ln("200" + 6);(B) System.out .pr int ln(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 002) ) ;CD) System.out .pr int ln(2006 * 2000000 / 2000000);(E) System.out .pr int ln(2006 / 1000 * 1000 + 2006 % 1000);

    2. What is the output from the following statement?Sys tem.ou t .p r in t ln (

    3

    new Double((new In teger(200)) . in tValue() / 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?St r ing la = "-La", da = "-Da", tempitemp = l a ;la += daida = tempiSys tem.ou t .p r in t ln ( la + da ) i

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

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

    (A)(B)(C)(D)(E)

    Ar r ayL i s t numbers = new ArrayList ( ) ;numbers .add(" l" ) ;nWfibers .add("2") ;numbers. a d d ("3") inurnbers.add ( 14 ") ;numbers . se t (2 , numbers . remove( l ) ) ;System.out .pr in t ln(numbe r s) ;

    [ 1 , 3, 2)[1 , 2, 4 ][ I , 3, 4][2 , I , 3, 4]None of the above

  • 8/14/2019 CML Contest

    5/16

    4 CML National Computer Science Contest 2006

    5. What values are stored in the array counts after the following code segment has beenexecuted?

    (A)(B)(C)(D)(E)

    0,0,I ,14,

    i n t [ ] counts = new i n t [ 7 ) ;i n t i = 0, in c = 1;fo r ( in t k = 1; k = coun ts . l eng th ){

    0,0 ,1 ,

    i %= c o u n t s . l e n g t h ;in c = - i n c ;

    0, 0, 0, 0, , 0, 0, I , 10, 0, 0, 0, 0

    14, 14, 14, 14, 15, IS15, 15, 14 , 14 , 14, 14, 14

    6. The i f - e l s e statementif (a = c)r e tu rn t r u e ;e lsere turn f a l s e ;

    e ~ s e if (d >= a)r e tu rn t rue ;e l se

    r e tu rn f a l se ;

    is equivalent to which of the following statements?(A) re turn a = c && a > c &&(B) re turn (a = c) && (a > c(C) r e tu rn a = C I I a > c &&(D) r e tu rn ! ( (a = c) I I (a >(E) r e t u r n ! ( (a > c I I b < c) && (a = a ;I I d >= a) ;d < a;c && d >= a) ; ;

    c I I d < a} ) ;

  • 8/14/2019 CML Contest

    6/16

    CML National Computer Science Contest 2006

    7. What is the output from the following statement?System.out .pr in t ln( r r123" .subst r ing (1 ) + "123 rr . subs t r ing (2)+ "123 rr . subs t r i n g ( 3 ) ) i

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

    8. Consider the following classes:pub l i c c l a s s Gree t ing{

    pub l i c S t r ing getMessage( ) { re tu rn "How do you do, Mr. " i }pr iva te S t r ing getName() { re tu rn "Smith"; }publ ic S t r ing toStr ing( ) { r e t u r n getMessage() + getName() i }

    publ ic c lass InformalGree t ing extends Greet ing{

    pub l i c S t r ing getMessage() { re tu rn "Hi, "; }p r iv a t e St r ing getName() { re tu rn "Bob"; }publ ic S t r i ng t oS t r ing ( ) { r e tu r n s u p e r . t o S t r i n g ( ) ;

    publ ic c lass Veryl n rormalGree t ing extends I n ro rmalGree t ing{

    publ ic S t r i ng getMessage() { r e tu rn " ' sup , ";p r iv a t e S tr in g getName() { re turn "dude"; }

    What doesSystem.out .pr in t ln(new Veryl nf ormalGree t ing( ) ) i

    display?(A) How do you do, Mr. Smith(B) H i, Bob(C) ' s up , BobCD) ' s up , dude(E) ' s up , Smith

    5

  • 8/14/2019 CML Contest

    7/16

    6 CML National Computer Science Contest 2006

    9. If Li kable extends Comparable, Fr iend extends Person, Person implementsComparable, Fr iend implements Likable , and the variables Person s t r a nge rand Fr iend f r i end are declared and initialized, which of the following statements isNO T valid?(A) Person x = f r i end ;(B) Comparable x = s ~ r a ~ g e r ; (C) Comparable x = f r i e ~ d ; (D) Likable x = s t r an g e r ;(E) Likable x = f r i end ;

    10. Conside r the following method my s t e ry:pub l ic Str ing mys te ry (St r ing s t r l , S tr i ng s t r2 ){

    if ( s t r l . l eng th ( )

  • 8/14/2019 CML Contest

    8/16

    CML National Computer Science Contest 2006

    12. Consider the following class Word and its subclass Seco ndWord:pub l i c c l a s s Word{

    pro tec ted S t r ing word;pub l i c Word(Str ing w) ( word = Wipub l i c S t r ing toS t r ing( ) { re tu rn word;

    pub l i c c l a s s SecondWord extends Word{

    p r iv a t e St r ing word;pub l i c SecondWord(Str ing wl, St r ing w2) { < code not shown> }pub l i c S t r ing toS t r ing( ) { < code no t shown> }

    SupposeSystem.out .pr in t ln(new SecondWord("Good rr , "day") ) ;

    printsGood day

    Which of the following statements could complete Secon dW ord's constructor andtoS t r ing method?

    1.

    n.m.

    (A)(B)(C)(D)(E)

    Constructor:toString:Constructor:toString:Constnlctor:toString:

    I onlyII onlyI and IIII and i l lI, II, and i l l

    super(w l ) ; word = w2; }r e tu rn super . toS t r ing( ) + " " + word; }super (wl) ; word = w2;retc .rn s upe r . word + If 11- + word;super . word = wI ; word w2; }r e tu rn s up er .word + " II + word;

    7

  • 8/14/2019 CML Contest

    9/16

    8 CML National Computer Science Contest 2006

    13. Which of the following arrays a rr will end up unsorted after the following codesegment has been executed?

    (A)(8 )(C)(D)(E)

    in t i = 0, j = a r r . l eng thwhile ( i < j ){

    if ( a r r [ i ] < 0)i ++ ;e l s e if ( a r r [ j ) > 0)j - - ;e l s e{

    i n t temp = a r r [ i Jia r r [ i] a r r [ j ] ;a r r [ j ] tempii++;j - - ;

    : ' n t [ ] arr { 1 , 2, 3 , -1 , -2 ,i n t [ ] a r r {-3 , -2 , -1 , 1, 2,i n t [] arr {-3, -2 , I , -1 , 2,i n t [] a rr {-3, 2, - I , 1, -2 ,i n t [ ] a r r { 3 , 2, 1 , -1 , -2 ,

    14. The method f un is defined as follows:p ub l ic i n t f u ~ i n t n){

    if (n == 1)re t urr. 1 ;e l s e{

    i n t m = n/2;

    - 3 } i3} ;3};3} ;

    -3} i

    re t u r 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

  • 8/14/2019 CML Contest

    10/16

    CML National Computer Science Contest 2006

    15. Which of the following best describes the mos t likely output from the following code?St r ing abc == "ABCDEFGHIJKLMNOPQRSTUVWXYZ"jQueue(] qq = new Queue(2)iqq[O] = new ListQueue() ;qq( l) = new Lis tQueue( ) ;Random rand = new Random()jfo r ( in t i = 0; i < abc . l eng th ( ) ; i++){Charac t e r x = new Charac te r ( abc .charAt ( i ) ) jqq [ r and .nex t ln t (2 ) ) . enqueue (x) i

    while(!qq[O] . i sErnpty())System.out .pr in t (qq[O] .dequeue() ) i

    whi le ( !qq( l ] . i sEmpty( l )Sys tem.ou t .p r in t (qq[ l ] . dequeue ( ) ) i

    (A) Very likely ABCDEFGHIJKLMNOPQRSTUVWXYZ(B) Very likely ZYXWVUTSRQPONMLKJIHGFEDCBA(C) Very likely BADCFEHGJILKNMPORQTSVUXWZYCD) A string of letters, arranged in increasing order, concatenated with a string of the

    remaining letters of the alphabet, also arranged in increasing order.

    9

    (E) A string that is formed from the original string abc by swapping the two letters inseveral (roughly half) of the 13 consecutive pairs of letters.

    16. Consider the following base and derived classes:pub l ic c l a s s B(

    pub l i c St r ing with(B xl { r e tu rn "8" ;

    pub l i c c lass 0 ex tends B{pub l i c St ring with ( D x) r e tu rn "D"; }

    Vlhat is the result from the following code?B b = new B() ;B d = new D () iSys tem.ou t .p r in t ln (b .wi th (b ) + b .w i t h (d ) +

    CA) BBBB(B) BDED(C) BBBD(D) BBDD(E) Syntax error: undefined method

    d .wi t h( b ) + d .w i t h ( d ) ) ;

  • 8/14/2019 CML Contest

    11/16

    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 hasseveral 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 G i f t :pub l i c c l a s s Gi f t implements Comparable{

    pr iva t e i n t va10ei

    p ubl ic G if t ( ) { va l ue = 0; }p ub l i c G i f t ( i n t v) { value = V i }pub l i c i n t getValue( ) { re tu rn va lue ;p ub l i c i n t compareTo{Object o the r ){

    G if t o the rG i f t = (Gi f t )o t her ir e t u rn t h i s . va l ue - o t h e rG i f t . v a l u e ; I I Line 12

    pub l i c S t r i n g t oSt r ing ( ) { re t urn S t r i n g . v a l u e O f ( g e t V a l u e ( ) ) i

    18. The output fromG i f t g if t l = ne w Gi f t ( lOO)iG i f t g i f t 2 = new G i f t ( 7 5 ) iSys t e m . ou t . p r i n t l n ( g i f t l + I) " + g i f t 2 + I I I I +

    g i f t l . c o mp a re T o (g i f t 2 ) ) ;

    is, as expected,100 75 25

    Which one of the following alternative statements on Line 12 in Gi f t would cause asyntax error or give a different output for this code?(A) re tu rn value - othe rGi f t . va lue ;(8) re tu rn value - othe rGi f t . ge tVa lue ( ) ;(C) r e tu rn getValue() - othe rGi f t . ge tVa lue ( ) ;0)) re tu rn th i s . ge tVa lue ( ) - othe rGi f t . ge tVa lue ( ) i(E) All of the above compile with no errors and produce the same output.

  • 8/14/2019 CML Contest

    12/16

    CML National Computer Science Contest 2006

    19. Consider the following subclass ofGif t :publ ic c la s s WrappedGift extends Gif t(pr i va t e Gif t g i f t ;publ ic WrappedGift (Gift g) { g i f t = g; }pub l i c in t getValue() { r e t u rn gi f t .ge tValue( ) ;pub l i c in t compareT o (Object other ){ r e turn g if t .compareTo (o t he r ) ; }publ ic St r i ng toS t r ing () { r e turn g i f t . t o S t r i ~ g () ;

    \\'hat is the output from the following code?Gif t g i f t l = new Gif t ( lCO);Gif t g if t 2 = ~ e w WrappedGift(new Gif t (75) ) ;System.ou t . p r i nt l : ' l (gi f t l + " 1/ + g if t 2 + II " +

    (A) Syntax error in the WrappedGi ft class(B) 100 75 25(C) 100 0 25(D) 100 a 100(E) 100 75 100

    l . c o m p a ~ e g i f t 2 )

    11

    20. Compare two implementations of priority queue: one as a heap, the other as a singlylinked list sorted in order of decreasing priority. In terms of time (big-O), for which ofthe fo llowing operations is an optimal linked list implementation not worse than anoptimal heap implementation?

    1. removing the next objectn. 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 i l l(E) I, II, and III

  • 8/14/2019 CML Contest

    13/16

    12 CML National Computer Science Contest 2006

    21. Recall that in Java, two Sets are deemed equal when they have the same size and equalelements. (More precisely, s 1 . equa 1s (s 2) returns t rue if and only if51. s ize () == s2 . s ize () and for any x in s l , s2 . con ta in s (x) is t rue . ) ThehashCode 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 se tOfSets = new HashSet ( ) ;S et s = ~ e w TreeSet ( ) is.acd(new l n t eger ( l ) ) isetOfSets .add(s} is.add(new In teger (2 ) ) ise tOfSe t s . add (s ) ;Sys te rn .ou t .p r in t ln ( se tOfSe ts ) ;

    (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 I n t ege r ( l ) , "One");rn .put(new I n teger (2 ) , "Two");m.put{new In teger (3 ) , "Three") ;

    Which of the following code segments will print [One Two, Three]?1. I t e r a t o r it = m.keySet() . i t e r a t o r ( ) ;Str ing separa to r = "[";

    w h i ~ e ( i t . hasNex t ( ){

    Sys tem.ou t .p r in t ( separa to r + m.g e t ( i t . n e x t ( ) ;separa to r = ", IT;Sys tem.ou t .p r in t ln ( " ] " ) ;

    n. System.out .pr in t ln{rn.keySet () ) ;TIIT. System.ou t . p r i n t l n(rn);

    (A) None of the three(B) I only(C) IT only(D) I and II(E) I ar.d III

  • 8/14/2019 CML Contest

    14/16

    CML National Computer Science Contest 2006

    23. Consider the following method that compares two binary trees:pub l i c i n t compare(TreeNode r oo t l , TreeNode root2){

    if ( roo t l != nu l l && root2 != nu l l )

    13

    r e tu rn compare ( roo t l .ge tLef t ( ) , r oo t 2 .ge tLe f t ( ) +c o ~ p a r e { r o o t l . g e R i g h t ( ) , r oo t 2 . g e tR i gh t ( ;e l s e if ( roo t l != nu l l )

    re tu rn 1 + compare ( roo t l .ge tLef t ( ) , nu l l ) +compare( roo t l . ge tRigh t ( ) , nu l l ) ;

    e l s e if (root2 != nu l l )r e tu rn -1 + compare (nu l l , roo t2 .ge tLef t ( +comp a re (nul l , roo t2 .ge tR i ght(} ) ;

    el ser e t u r ~ 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 0What are the values returned by compare ( roo t I , roo t2) ,c o m p a ~ e ( r o o t 2 , r oo t3 ) , andcompare ( roo t l , roo t3)?

    (A) 0, 0, 0(B) 0, - I , -1(C) -1 , - I , -1(D) - I , - I , -2(E) 1, I , 1

  • 8/14/2019 CML Contest

    15/16

    14 CAlL National Computer Science Contest 2006

    24. Suppose TreeNode roc>t refers to the root of the following binary tree:

    o

    A/ \B C/ \

    Wnat is the output from the following code?

    (A)(B)(C)CD)(E)

    Stack s tk = new ArrayStack( ) iwhile (root != nu l l II !s tk . isEmpty() ){ i f ( roo t == nul l )

    (root = (TreeNode)stk . pop() ;Sys tem.ou t .p r in t ( roo t .ge tValue( ) ) ;roo t = roc t .ge tRigh t ( ) ;e l se{s tk .push( roo t ) ;roo t = r oo t . ge tLef t ( ) ;

    ABCDEABDECDBEACDEBCANone of the above

    25. On the eve of a multiple-choice exam, the students learn that the exam consists of25questions with five answer choices for each, and that the answer key happens to aperiodic sequence in which the same sequence of five letters (not necessarily alldifferent) repeats five times. Jim Guesling decides not to study at all, but he comes upwith an optimal strategy that guarantees him a certain number of correct answers. GraceHoper, on the other hand, has studied very hard and believes, with a good reason, thatshe can answer, on average, 9 out of 10 questions correctly. Nevertheless, she decidesto take advantage of the new infonnation about the answer key to further improve herresult and comes up with an optimal strategy to do so. How many questions is Jim sureto 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

  • 8/14/2019 CML Contest

    16/16

    CIt CONTINENTAL MATHEMATICS LEAGUENational Computer Science Contest

    Spring 2006

    1 0 13. A2. B 14. E3. C 15. 04. A 16. A5. C 17. B6. E 18. E7. C 19. E8. E 20. C9. 0 21. A10. A 22. B11. B 23. 012. C 24. C

    25. B