Computer Science Contest #0001-____

Total Page:16

File Type:pdf, Size:1020Kb

Computer Science Contest #0001-____

Computer Science Contest #0506-0 January 21, 2006

Note: Correct responses are based on Java, J2sdk v 1.5.0, from Sun Microsystems, Inc. All provided code segments are intended to be syntactically correct, unless otherwise stated (i. e. error is an answer choice) and any necessary Java 2 Standard Packages have been imported. Ignore any typographical errors and assume any undefined variables are defined as used.

QUESTION 1 xx What is the output? double x = 10/5/2.0; out.println(x); A. 1 B. 4 C. 5.0 D. 1.0 E. 4.0

QUESTION 2 xx What is the output? String s = "Happy New Year"; System.out.println(s.charAt(2)); A. H B. a C. p D. N E. y

QUESTION 3 xx int x = 2; What is the output? switch(x%3){ A. X B. Y C. Z D. XY E. XYZ case 0 : out.print("X"); case 1 : out.print("Y"); break; case 2 : out.print("Z"); }

QUESTION 4 xx Which of the following is a legal reserved word for a 64 bit java data type? A. short B. byte C. int D. char E. double

QUESTION 5 xx What is the output? String s = "12345go12"; s=s.replaceAll("[12345]" ,"x"); A. xgox B. xxxxxgoxx C. xgo12 out.println(s); D. x2345goxx E. error

QUESTION 6 xx What is returned by Math.round(7.83)? A. 8 B. 8.0 C. 7 D. 7.0 E. error

QUESTION 7 xx What of the following statements would not correctly fill < blank 1 > ? Collection x = < blank 1 > ; A. new TreeMap() B. new ArrayList() C. new LinkedList() D. new HashSet() E. new TreeSet() QUESTION 8 xx Which of the following data types could be assigned the value 0 ? A. char B. int C. float D. double E. all of these

QUESTION 9 xx What is the output? boolean one = true; boolean two = true; A. true B. false C. null System.out.println(!(one&&two)); D. one&&two E. true&&false

QUESTION 10 xx Which of the following methods is used to add pairs to a map ? A. add() B. insert() C. put() D. set() E match()

QUESTION 11 xx Which of the following is an Object? A. Char B. float C. Byte D. Int E. None of these CSci 0506-09

QUESTION 12 xx What is the output? char x = 70; System.out.println(x>>3); A. 17 B. 18 C. 35 D. 34 E. 8

QUESTION 13 xx Which of the following interfaces does HashSet implement? A. Set B. SortedSet C. Collection D. A and C only E. A, B, and C QUESTION 14 xx Which of the following sort algorithms was used to write Arrays.sort()if the array to be sorted contains only ints? A. bubble sort B. insertion sort C. selection sort D. merge sort E. quick sort QUESTION 15 xx What is the output? String s = "abc"; String t = s; A. B. muddy abc t = "muddy"; C. ABC D. null E. error out.println(s);

QUESTION 16 xx Integer[][] mat = new Integer[3][3]; What is the output? for( int r=1; r

QUESTION 17 xx WW Which of the following can be used as a test condition for a switch statement? A. short B. int C. long D. A and B only E. A, B, and C

QUESTION 18 xx class A{ What is the output of line 1 ? private int x = 0; public A(){ x =9; } A. 0 public String toString(){ B. 8 return x+" "; C. 9 } D. 9 0 } E. 9 8 class B extends A{ private int x = 0; QUESTION 19 xx public B(){ x=8; } What is the output of line 2 ? public String toString(){

A. 0 0 return x+" "+super.toString(); } B. 8 8 } C. 9 0 //code in main method D. 9 8 A x = new A(); out.println(x); //line 1 E. 8 9 x = new B(); out.println(x); //line 2

QUESTION 20 xx What is the output? String[] list = {"one","two","three"}; for( String e : list) A. B. one two { C. null D. zero E. error list = new String[3]; } out.println(list[0]);

QUESTION 21 xx 092 + 0x92 = A. 184 B. 259 C. 1243 D. 879 E. error

© Charlotte Scroggs http://www.cistests.com written by Stacey Armstrong http://www.apluscompsci.com Page 2 CSci 0506-09 QUESTION 22 xx What is the output? int[] a = {1,4,9,12,67,88}; int pos = Arrays.binarySearch(a,17); A. 0 B. -1 C. -3 D. -5 E. 2 out.println(pos);

QUESTION 23 xx class A Which of the following code statements would correctly fill { so that the A constructor would be complete ? < blank 1 > public A( String val ){ < blank 1 > } A. val = x; public void change( ){x="change it";} public String toString(){ return x; } B. val = " "; private String x; C. x = val; } D. x = " "; class B { public void mystery(A one, A two) { E. x = null; two.change(); QUESTION 24xx one = two; } Assuming that < blank 1 > was filled correctly, what is output public static void main(String args[]){ by line 1 ? B test = new B(); A. change it two A a1 = new A("one"); A a2 = new A("two"); B. two change it test.mystery(a1,a2); C. two two out.println(a1 + " " + a2); //line 1 D. one change it } } E. two it

QUESTION 25 xx int i=0; What is the last value output? for(i=1;i<9;i+=2) A. 5 B. 7 C. 9 D. 11 E. error out.println(i);

QUESTION 26 xx double[] x = What is the output? {Math.pow(2,3),Math.ceil(8.12)}; A. 9.0 B. 9 C. 10 D. 10.0 E. error out.println(x[1]);

QUESTION 27 xx What is the output? String s = "a 1 b 2 c 3"; A. a 1 b 2 c 3 B. -1-2-3 s=s.replaceAll("\\D+","-"); C. ------D. --1---2---3 out.println(s); E. a-1-b-2-c-3

QUESTION 28 xx int x[] = {1,2,3,4,5,6,7,8,9}, sum=0; What is the output? for(Integer i : x){ A. 20 B. 36 if(i%2==0) sum+=i; } C. D. E. error 28 25 out.println(sum);

QUESTION 29 xx What is the output? int x = 7, y = 0; boolean t = ( y>0 & x++>7 | x>7 ); A. true B. 0 C. false D. 1 E. error out.println(t);

QUESTION 30 xx What is the output? out.printf("%04.1f\n",2.1); A. 2.1 B. 2.10 C. 02.1 D. 002.1 E. error

© Charlotte Scroggs http://www.cistests.com written by Stacey Armstrong http://www.apluscompsci.com Page 3 CSci 0506-09

QUESTION 31xx What is returned by fun(2,8)? static int fun( int x, int y) { A. 12 B. 14 C. 16 D. 18 E. 20 if( y == 2)

QUESTION 32 xx return x; else What is returned by fun(8,5)? return fun( x, y - 1) + x; } A. 4 B. 8 C. 16 D. 32 E. error

QUESTION 33 xx

Which of the following statements would fill < blank 1 > so that Chicken can be compared to other Chickens without a cast? A. Comparable B. Comparable class Chicken implements < blank 1 > C. Comparable { public Chicken(){ D. Comparable } E. more than one of these

QUESTION 34 xx public int compareTo(Chicken c) { Assuming < blank 1 > is filled correctly, which of the following < blank 2 > statements would fill < blank 2 > so that the name instance } variables of 2 Chickens could be compared lexicographically? private String name; A. return name.compareTo(c.name); } B. return compareTo(c.name); C. return c.name.compareTo(name); D. return name.compareTo(name); E. return c.name.compareTo(c.name);

QUESTION 35xx int x[] = {3,7,1,9,3,6,8,2,5,8}; What is the run-time of the code at right? for(int i=0; i

© Charlotte Scroggs http://www.cistests.com written by Stacey Armstrong http://www.apluscompsci.com Page 4 CSci 0506-09

QUESTION 36 xx

What is the output of line 1 ? A. 0 0 0 B. 0 0 1 class A { public A() { c++; } C. 0 0 2 public void fun() { x = 3; } D. 0 0 3 public int back() { return 1; } public String toString() { E. 0 0 4 return x + " " + y + " " + c; QUESTION 37 xx } private int x, y; What is the output of line 2 ? public static int c; A. 0 0 0 }

B. 0 0 1 class B extends A { C. 0 0 2 public void fun() { out.println(back()); } D. 0 0 3 public int back() { return 2; } E. 0 0 4 public String toString() {

QUESTION 38 xx return super.toString(); } What is the output of line 3 ? } A. 0 0 3 class C { B. 0 0 4 public static void main(

C. 0 0 5 String args[]) { D. 0 0 6 C test = new C(); E. 0 0 7 A one = new B(); out.println(one); //line 1 QUESTION 39 xx one = new A(); What is the output of line 4 ? one = new B(); A. 1 one = new A(); B. 2 out.println(one); //line 2 C. 3 one = new A(); D. 4 one = new B(); one.fun(); E. 5 out.println(one); //line 3 QUESTION 40 xx one = new B(); one.fun(); //line 4 What is the output of line 5 ? A. 0 0 3 out.println(one); //line 5 } B. 0 0 4 } C. 0 0 5 D. 0 0 6 E. 0 0 7

© Charlotte Scroggs http://www.cistests.com written by Stacey Armstrong http://www.apluscompsci.com Page 5

Recommended publications