2005
1.
What is the size of a double variable =
in=20
Java?
2 bytes=20
4 bytes=20
8 bytes=20
It depends on the =
compiler=20
setting=20
It depends on the =
operating=20
system
2.
What is displayed by
System.out.println("1" + new Integer(2) +=20
3);
The statement has a =
syntax error=20
and won't compile=20
6=20
15=20
123=20
ClassCastException =
3.
Which of the following best describes the set of all =
pairs=20
of values for boolean variables a and =
b , such=20
that
(!a && b) =3D=3D !(a || =
b)
evaluates to true?
Empty set=20
Only one pair: =
a =3D=3D true, b=20
=3D=3D false=20
Two pairs in which =
a =3D=3D=20
true=20
Two pairs in which =
a !=3D=20
b=20
All four possible =
combinations of=20
values
4. (AB)
When you try to compile MyClass, the =
java=20
compiler gives an error message
MyClass is not abstract and does not override abstract =
method=20
<some method> in =
java.util.Comparator
Which of the following is <some method> in =
the error=20
message?
=
equals(myClass)=20
=
compareTo(myClass)=20
=
compare(myClass,=20
myClass)=20
=20
compareTo(java.lang.Object)=20
=
compare(java.lang.Object,=20
java.lang.Object)
5.
Consider the following classes:
public class Year2005 { =
public=20
String toString() { return "2005"; } }
public class Test2005 extends=20
Year2005 { public void=20
print() =20
{ =
<missing=20
statement> =
} }
Which of the following could replace <missing=20
statement> so that Test2005 would compile =
with no=20
errors and
Test2005 test =3D new=20
Test2005(); test.print();
would display 2005?
I. System.out.println(new=20
Year2005()); II. System.out.println(new=20
Test2005()); III.=20
System.out.println(this);
I only=20
II only=20
I and II=20
II and III=20
I, II, and III =
6.
What is the value of a[1] after the =
following=20
code is executed?
int[] a =3D {0, 2, 4, 1, 3}; for (int i =3D 0; i =
< a.length;=20
i++) a[i] =3D a[(a[i] + 3) %=20
a.length];
0=20
1=20
2=20
3=20
4
7.
Consider the method
public String mystery(String=20
s) { String s1 =3D=20
s.substring(0,1); String s2 =3D =
s.substring(1,=20
s.length() - 1); String s3 =3D=20
s.substring(s.length() - 1); if =
(s.length()=20
<=3D =
3) =20
return s3 + s2 + s1; =20
else =
return s1=20
+ mystery(s2) + s3; }
What is the output of
=
System.out.println(mystery("DELIVER"));
<=
/TD>
DELIVER=20
DEVILER=20
REVILED=20
RELIVED=20
DLEIEVR =
8. (AB)
Consider the following code segment:
List list =3D new =
LinkedList(); list.add("["); =20
list.add("A"); =20
list.add("]"); System.out.println(list);
ListIterator it =3D=20
=
list.listIterator(); while(it.hasNext() { &nbs=
p;=20
if ("[".equals(it.next()) ||=20
=
"]".equals(it.next()))  =
; =20
it.remove(); =20
else =20
=
it.add("*"); } System.out.println(list);
The first output line is
[[, A, ]]
What is the second output line?
[A]=20
[A, B] =
[B, A] =
=
ClassCaseException=20
=20
NoSuchElementException
9.
Which of the following is not a method of=20
java.util.ArrayList?
add(Object =
x);=20
remove(Object =
x);=20
insert(int i, =
Object=20
x);=20
=
contains(Object x);=20
set(int i, =
Object=20
x);
Questions 10-11 refer to
public interface InterfaceA { void methodA(); =
}
public interface InterfaceB extends InterfaceA { void=20
methodB(); }
public class ClassA implements=20
InterfaceA { public void =
methodA()=20
{} public void methodB() =
{} }
public class ClassB extends ClassA implements=20
InterfaceB { public ClassB()=20
{} ... <methods not=20
shown> }
10.
What is the minimum number of methods that must be =
defined=20
in classB for it to compile with no errors?
No particular =
methods are=20
required=20
=
methodA=20
=
methodB=20
methodA =
and=20
methodB=20
methodA =
,=20
methodB, and toString
11.
Which of the following statements causes a syntax=20
error?
InterfaceA =
obj =3D new=20
ClassA();=20
InterfaceB =
obj =3D new=20
ClassA();=20
InterfaceA =
obj =3D new=20
ClassB();=20
InterfaceB =
obj =3D new=20
ClassB();=20
ClassA obj =
=3D new=20
ClassB();
Questions 12-13 refer to the following method=20
rotate that takes a two-dimensional array=20
a and returns a two-dimensional array b =
made by=20
rotating a by 90=B0 counterclockwise.
public int[][]=20
rotate(int[][]a) { int rows =3D=20
<expression 1>; int cols =3D=20
<expression 2>; int[][] b =3D=20
<expression 3>; for (int r =
=3D 0; r=20
< rows;=20
r++) =
for (int=20
c =3D 0; c < cols;=20
=
c++)  =
; =20
b[r][c] =3D <expression =
4>; return=20
b; }
For example, if a holds
1 2 3 4 5 6
b =3D rotate(a) will hold
3 6 2 5 1 4
12. (AB)
Which of the following should replace =
<expression=20
1>, <expression 2>, and =
<expression=20
3>?
<expression=20
1> =20
<expression=20
2> =20
<expression 3>
=20
=
a.cols =
a.rows new =
int(rows, cols)=20
=20
=
a.cols =
a.rows new =
int[][](rows, cols)=20
=20
a.cols() =20
a.rows() new int(rows, =
cols)=20
=20
a[0].length =20
a.length new=20
int[rows][cols]=20
=20
a[0].length =20
a.length new =
int[][](rows,=20
cols)
13. (AB)
Which of the following should replace =
<statement=20
4>?
=
a[c][r]=20
a[c][rows - =
1 - r]=20
a[cols - 1 - =
c][r]=20
a[cols - 1 - =
c][rows - 1 -=20
r]=20
a[rows - 1 - =
r][cols - 1 -=20
c]
14.
What is the value of n after the =
following code=20
is executed?
int n =3D 2005; for (int i =3D 0; i < 50;=20
i++) n =3D (n + 3) /=20
2;
0=20
1=20
2=20
3=20
65
15.
What is the output of the following code segment?
List cities =3D new=20
=
ArrayList(); cities.add("Atlanta"); cities.add("Boston");
for (int i =3D 1; i < cities.size();=20
i++) cities.add(i, "+");
=
System.out.println(cities);
[Atlanta, =
Boston]=20
[Atlanta, +, =
Boston]=20
[Atlanta, =
Boston,=20
+]
[Atlanta, +, =
Boston,=20
+]
No output because =
the program=20
goes into an infinite loop
16.
Which of the following statements is true?
A static variable =
cannot be=20
initialized in a constructor=20
A static variable =
must be=20
declared final=20
An instance =
variable can't be=20
declared final=20
A static method =
can't access an=20
instance variable=20
Only a static =
method can access=20
a static variable
17. (AB)
a and b are arrays of =
integers and=20
each of them has n elements. a is sorted =
in=20
ascending order and b is sorted in descending =
order. =20
What is the big-O, in terms of n, for the number of=20
comparisons in an optimal algorithm that determines whether there=20
exists i, such that a[i] =3D=3D =
b[i]?
O (log =
n )=20
O ( (log=20
n )2 )=20
O (n ) =
O (n =
log n )=20
=
O (n 2 )=20
18. (AB)
What is the output of the following code?
String key =3D ""; Map map =3D new TreeMap(); for =
(int k =3D 0;=20
k < 3; k++) { key +=3D=20
k; String value =3D=20
"A"; map.put(key,=20
value); value +=3D=20
"B"; map.put9key,=20
=
value); } System.out.println(map.size());
=
TD>
1=20
2=20
3=20
4=20
6
19. (AB)
Consider the following method:
private TreeNode mysteryProcess(TreeNode=20
root) { if (root =3D=3D null ||=20
(root.getLeft() =3D=3D null && root.getRight() =3D=3D=20
null)) =
return=20
null; =
else =20
{ =20
=
root.setLeft(mysteryProcess(root.getLeft())); =
=20
=
root.setRight(mysteryProcess(root.getRight())); &nbs=
p; =20
return root; =
} }
If root refers to the root of the tree
=
&nbs=
p; 1 =
=20
=
/ \ &=
nbsp; =20
2 =20
=
3 &n=
bsp;=20
/ /=20
=
\ =20
4 5 6
which of the following trees is returned by=20
mysteryProcess(root)?
20. (AB)
What is the output of the following code?
Stack stk =3D new=20
=
ArrayStack(); stk.push("A"); stk.push("B"); stk.push(stk); wh=
ile(!stk.isEmpty()) { =20
Object obj =3D stk.pop(); if (obj =
instanceOf=20
Stack) =20
{ =20
=
while(!((Stack)obj).isEmpty()) &nb=
sp; =20
{ =20
=
System.out.print(((Stack)obj).pop()); =20
} =20
else =20
System.out.print(obj); }
BA=20
ABBA=20
BAAB=20
BABA=20
ClassCastException =
21. (AB)
A priority queue is represented as a minimum heap, =
stored in=20
an array. When a node is added to the heap, it is first added at =
the=20
leftmost vacant spot in the current level (or, if the current =
level is=20
full, at the leftmost spot in the next level), and then the =
reheap-up=20
procedure is applied. What is the order of the values in the array =
after=20
"M", "A", "N", =
"G",=20
"O", "E", "S" are added to =
the=20
queue?
=
AEGMNOS=20
=
AGEMONS=20
=
AEGMNOS=20
=
AEGSOMN=20
=
AGESNOM
22. (AB)
Suppose an array of n elements is stored in =
ascending order.=20
Then 5 elements are picked randomly and assigned random values. =
Which of=20
the following sorting algorithms guarantees to restore the =
ascending order=20
in than array using no more than O (n ) =
comparisons?
I. Selection Sort II. Insertion=20
Sort III. =
Mergesort
I only=20
II only=20
I and II=20
II and III=20
I, II, and III =
23.
For any object obj, a call=20
obj.getClass().getName() returns the name of the=20
obj's class.
Suppose
System.out.println(new A() + "+" + new=20
B());
displays
A+B
Which of the following implementations would produce that =
result?
I. Class A has a method
=
public=20
String toString() { return "A"; }
and class B has a method
=
public=20
String toString() { return "B"; }
II. Both class A and class B extend class X that has a =
method
=
public=20
String toString() { return getClass().getname(); }
III. Both class A and class B extend an abstract class X that =
has=20
methods
=
public=20
abstract String=20
=
getName(); =20
public String toString() { return getname(); }
Class A has a method
=
public=20
String toString() { return "A"; }
and class B has a method
=
public=20
String toString() { return "B"; =
}
I only=20
II only=20
I and II=20
II and III=20
I, II, and III =
24. (AB)
Consider the following class:
public class Widget implements=20
Comparable { private int nuts,=20
bolts;
public Widget(int nuts, int =
bolts) {=20
this.nuts =3D nuts; this.bolts =3D bolts; }
public int compareTo(Object =
other) {=20
return nuts - ((Widget)other).nuts; }
public boolean equals(Object =
other) {=20
return bolts =3D=3D ((Widget)other).bolts; }
public String toString() { =
return "" +=20
nuts; } }
Suppose a non-empty List list holds several=20
Widget objects and the following statements have been =
executed:
=
Collections.sort(list); System.out.println(list); Set=20
tSet =3D new TreeSet(list); Set hSet =3D new=20
HasSet(list);
Which of the following statements is NOT necessarily =
true?
tSet.size() =
<=3D=20
list.size()=20
hSet.size() =
<=3D=20
list.size()=20
hSet.size() =
=3D=3D=20
tSet.size()=20
=20
tSet.contains(list.get(list.size() - 1))returns=20
true=20
The output lists =
the values of=20
nuts in Widget objects in =
list in=20
ascending order
25.
A multiple-choice test has 25 questions, each with =
five=20
answer choices, A - E. On the eve of the test, Casey and other =
students=20
had learned that the correct answers on the test were not balanced =
properly: 3 correct answers were C, 7 were D, and 15 were E (A and =
B were=20
never correct answers). Casey spent the rest of the night devising =
an=20
optimal strategy for using this information (neglecting to review =
the test=20
material). In the worst case, how many questions is Casey =
guaranteed to=20
get right using the optimal strategy?
3=20
5=20
7=20
10=20
15
2004
26.
twist is defined as follows:
public void twist(String[] =
w) { =20
String temp =3D w[0].substring(0, =
1); w[0] =3D=20
w[1].substring(0, 1) + =
w[0].substring(1); =20
w[1] =3D temp + w[1].substring(1); }
What is the output of the following code segment?
String[] words =3D {"HOW",=20
"NEAT"}; twist(words): System.out.println(words[0] + " " + =
words[1]);
NOW =
NOW=20
HOW =
HOW=20
NOW =
HOW=20
HOW =
NEAT=20
NOW =
HEAT=20
27.
If Crossword extends Puzzle and implements Solvable, =
and
Puzzle p =3D new Puzzle(); Crossword cw =3D new =
Crossword(10,=20
20);
are declared, which of the following statements will cause a =
syntax=20
error?
p =3D =
cw;=20
cw =3D new =
Puzzle();=20
p =3D new =
Crossword(10,=20
20);=20
Solvable x =
=3D new=20
Crossword(10, 20);=20
All of the above =
will compile=20
with no errors.
28.
What is the output of the following code?
List nums =3D new ArrayList(3); nums.add(New=20
Integer(1)); nums.add(New Integer(2)); nums.add(0,=20
nums.get(1));
Object x =3D nums.get(0); Object y =3D =
nums.get(2);
if (x =3D=3D y) =
System.out.println(x +=20
" is equal to " + y); else =20
System.out.println(x + " is NOT equal to " +=20
y);
1 is equal =
to 2=20
1 is NOT =
equal to 2=20
2 is equal =
to 2=20
2 is NOT =
equal to 2=20
=20
IndexOutOfBoundsException
29. (AB)
Which of the following best describes the loop =
invariant in=20
the following code?
double x =3D 3.0, y =3D 1.0; while (x - y >=20
0.01) { x =3D (x + y) /=20
2; y =3D 3.0 /=20
x; }
3=20
x =3D 3=20
xy =3D 3=20
xy =3D 3 =
and x - y =20
> 0.01=20
x =3D =
1.7321428571428572=20
30.
What is the output of the following code?
int sum =3D 0, p =3D 1; for (int count =3D 1; count =
<=3D 50;=20
count++) { sum +=3D=20
p; p *=3D =
2; }
-1=20
562949953421311 =
(=3D=20
249 - 1)=20
1125899906842623 =
(=3D=20
250 - 1)=20
=
ArithmeticException=20
=20
IllegalArgumentException
31.
What is the output of the following code?
String barb =3D=20
=
"BARBARA"; scramble(barb); System.out.println(barb);
The=20
method scramble is defined as follows:
public String scramble(String=20
str) { if (str.length() >=3D=20
2) =20
{ int =
n =3D=20
str.length() /=20
2; str =
=3D=20
scramble(str.substring(n)) + str.substring(0,=20
n); } =
return=20
str; }
=
BARBARA=20
=
ARBABAR=20
AABAR =
=
ARBABARB=20
=
ARABARBARB=20
32.
What are the values in arr after the =
following=20
statements are executed?
int[] arr =3D {1, 1, 0, 0, 0};
for (int i =3D 2; i < arr.length;=20
i++) arr[i] =3D arr[i-1] +=20
arr[i-2];
11011=20
11210=20
11222=20
11235=20
11248 =
33.
Given
double x =3D 5, y =3D 2;
what is =
the value of=20
m after the following statement is executed?
int m =3D (int)(x + y + x / y - x * y - x / (10 *=20
y));
-1=20
-0.75=20
-0.5=20
0=20
1
34.
What is the value of sum after the =
following=20
code segment is executed?
int p =3D 3, q =3D 1, sum =3D 0; while (p <=3D=20
10) { sum +=3D p %=20
q; p++; =20
q++; }
0=20
10=20
12=20
14=20
52
35. (AB)
What is the output of the following code segment?
List words =3D new LinkedList(); int k;
for (k =3D 1; k <=3D 9; =
k++) =20
words.add("word", k);
for (k =3D 1; k <=3D words.size();=20
k++) if (k % 3 =3D=3D=20
0) =20
words.remove(k);
=
System.out.println(words);
[word1, =
word2, word4,=20
word5, word7, word8]=20
[word2, =
word3, word5,=20
word6, word7, word8]=20
[word2, =
word3, word5,=20
word6, word8, word9]=20
[word1, =
word2, word3,=20
word5, word6, word7, word8]=20
[word1, =
word2, word3,=20
word5, word6, word8, word9]
36.
A class Particle has a private field=20
double velocity and public methods double=20
getVelocity() and void setVelocity(double v). =
It also=20
has a method
public void hit(Particle p) { < Missing =
statements=20
> }
Which of the following could=20
replace < Missing statements > in=20
hit to make it compile with no errors?
I. double v =3D=20
getVelocity(); =20
setVelocity(p.getVelocity()); =20
p.setVelocity(v);
II. double v =3D=20
velocity; velocity =3D=20
p.getVelocity(); =20
p.setVelocity(v);
III. double v =3D=20
velocity; velocity =3D=20
p.velocity(); p.velocity =3D=20
v;
I only=20
II only=20
I and II=20
II and III=20
I, II, and III =
37.
Which of the following conditions must always be =
true after=20
the while loop finishes?
while (hours < hours0 || (hours =3D=3D hours0 =
&& mins=20
< mins0)) { mins +=3D=20
5; if (mins >=3D=20
60) =20
{ mins =
-=3D=20
60; =20
hours++; =
} }
hours > =
hours0=20
&& mins >=3D mins0=20
hours =
>=3D hours0=20
&& mins >=3D mins0=20
hours < =
hours0 ||=20
(hours =3D=3D hours0 && mins < mins0)=20
hours =
>=3D hours0=20
&& (hours !=3D hours0 && mins >=3D =
mins0)=20
hours =
>=3D hours0=20
&& (hours !=3D hours0 || mins >=3D mins0) =
38.
Consider the following class:
public class SaleItem implements=20
Comparable { public SaleItem(int =
p) {=20
prcice =3D p; }
< Comparison method =
header =20
> { < Code not shown > }
public String toString() { =
return=20
String.valueOf(price); }
private int=20
price; }
Which of the following could =
replace=20
< Comparison method header > to make =
this class=20
compile with no errors?
I. public int compare(SatleItem =
item1,=20
SaleItem item2)
II. public boolean compareTo(SaleItem =
item1)
III. public int compareTo(Object=20
obj)
I only=20
II only=20
III only=20
I or II=20
II or III =
int money;
public Gambler(int m) { money =
=3D m;=20
}
public int currentMoney() { =
return=20
money; } public void addMoney(int m) =
{ money=20
+=3D m; }
public void work() { money =
+=3D 100;=20
} public void play() { money /=3D 2; =
} public liveAnotherDay() { work(); =
play();=20
}
public String toString() { =
return=20
String.valueOf(money); } }
public class CompulsiveGambler extends=20
Gambler { public =
CompulsiveGambler(int=20
m) =20
{ < =
Missing statements > =
}
public void work() { /* do =
nothing */=20
}
public void=20
play() =20
{ =
while=20
(currentMoney() >=20
1) =20
=
{ &n=
bsp; =20
=
super.play(); =20
} =
} }
39.
Given that
System.out.println(new=20
CompulsiveGambler(300)); =20
displays 300, which of the =
following=20
could replace < Missing statements > in=20
CompulsiveGambler's constructor?
I. addMoney(m);
II. super(m);
III. super(); =20
addMoney(m);
I only=20
II only=20
I or II=20
II or III=20
I, II, or III =
40.
What is the output of the following code segment?
CompulsiveGambler x =3D new=20
=
CompulsiveGambler(300); x.liveAnotherDay(); System.out.println(x);<=
/CODE>
200=20
150=20
100=20
1=20
0
41.
Consider the following method:
public int goFigure(int =
x) { if=20
(x < =
100) x=20
=3D goFigure(x + 10); return (x - =
1); }=20
What does goFigure(60) return?
59=20
69=20
95=20
99=20
109 =
42. (AB)
Suppose a List list contains strings =
"A",=20
"*", "B", "*", "C" (in this order). What is the output of =
the=20
following code segment?
ListIterator it =3D list.listIterator(); while=20
(it.hasNext) { if=20
=
("*".equals(it.next())) &nbs=
p; =20
it.add(it.next() + "*"); }
=
System.out.println(list);
No output: the =
program goes into=20
an infinite loop=20
[A, *, **, B, *, =
**, C]=20
[A, **, *, B, **, =
*, C]=20
[A, *, B, B*, *, =
C, C*]=20
[A, *, **, ***, =
****, B, *, **,=20
***, ****, C]
43. (AB)
A linked list pointed to by ListNode=20
head contains Comparable objects. Which =
sorting=20
algorithm is implemented by the following method sort and its =
helper=20
method doSomething?
public ListNode sort(ListNode=20
head) { if (head !=3D=20
null) =
head =3D=20
doSomething(sort(head.getNext())), head);
return head; }
private ListNode doSomething(ListNode head, ListNode=20
node) { if (head =3D=3D null || =
((Comparable)=20
node.getValue()).CompareTo(head.getValue()) <=20
0) =20
{ =20
=
node.setNext(head); &n=
bsp;=20
head =3D node; =
} =20
else =20
head.setNext(doSomething(head.getNext(), node));
return=20
head; }
Selection sort=20
Insertion sort=20
Mergesort=20
Quicksort=20
Heapsort =
44. (AB)
A Map thingsToDo associates a=20
Resort object with a Set of activities =
available=20
at that resort. The following code segment is intended to =
remove=20
"Golf" from the activity sets in all resorts:
Iterator iter =3D =
thingsToDo.keySet().iterator(); while=20
(inter.hasNext()) < Missing=20
statement >
Which of the following should replace < Missing=20
statement >?
(Set)=20
iter.next().remove("Golf");=20
((Set)=20
iter.next()).remove("Golf");=20
=
thingsTodo.remove((Set)=20
iter.next(), "Golf");=20
=
thingsTodo.remove((Resort)=20
iter.next(), "Golf");=20
((Set)=20
thingsToDo.get(iter.next())).remove("Golf"); =
45. (AB)
What is the output of the following code segment?
TreeNode node6 =3D new TreeNode("6", null,=20
null);TreeNode node5 =3D new TreeNode("5", =
null,=20
null); TreeNode node4 =3D new TreeNode("4", null, =
null); TreeNode=20
node3 =3D new TreeNode("3", node5, node6); TreeNode node2 =3D =
new=20
TreeNode("2", null, node4); TreeNode node1 =3D new =
TreeNode("1", node2,=20
node3); TreeNode root =3D node1;
Object[] arr =3D new Object[8]; toArray(root, 1,=20
arr);
for (int i =3D 0; i < arr.length;=20
i++) System.out.println(arr[i] + "=20
");
The method toArray is defined as follows:
private void toArray(TreeNode root, itn i, Object[]=20
arr) { if (root !=3D=20
null) =20
{ =
arr[i] =3D=20
=
root.getValue();  =
;=20
toArray(root.getLeft(), 2*i,=20
arr); =20
toArray(root.getRight(), 2*i + 1, =
arr); =20
} }
null 1 2 =
null 3 4 5=20
6=20
null 1 2 3 =
null 3 4=20
5=20
null 1 2 3 4 =
null 3=20
4=20
null 1 2 3 4 =
5 null=20
3=20
null 1 2 3 4 =
5 6=20
null
46. (AB)
Suppose all valid five-digit zip codes are =
represented as=20
Integer objects and stored in a set containing about =
4000 zip=20
codes. Compare two implementations of this set: one is a=20
HashSet with 400 buckets; another is a =
TreeSet.=20
Assuming that various zip codes are matched against the set with =
roughly=20
the same frequency, which of the following statements about the =
average=20
performance of these implementations is true?
=
HashSet works more=20
than 100 times faster than TreeSet=20
=
HashSet works about=20
20 times faster than TreeSet=20
=
HashSet works 2-4=20
times faster than TreeSet=20
=
HashSet works=20
slower than TreeSet=20
=
HashSet works=20
roughly as fast as TreeSet, but takes more than =
twice as=20
much space
47. (AB)
What is the output of the following code segment?
String[] letters =3D { "A", "B", "C" }; Queue =
qLetters =3D new=20
ListQueue(); String sLetters =3D ""; Stack stk =3D new=20
ArrayStack();
for (int i =3D 0; i < letters.length;=20
i++) { =20
qLetters.enqueue(letters[i]); =20
stk.push(qLetters); sLetters +=3D=20
letters[i]; } while=20
(!stk.isEmpty()) { =20
System.out.print(stk.pop() + " "); =
Queue q =3D=20
(Queue) stk.pop(); =20
System.out.print("["); while=20
=
(!q.isEmpty()) =
System.out.print(q.dequeue()); =20
System.out.print("] "); }
ABC [ABC] AB =
[] A=20
[]=20
ABC [] ABC =
[] ABC=20
[]=20
ABC [ABC] AB =
[AB] A=20
[A]=20
A [A] AB =
[AB] ABC=20
[ABC]=20
=
ClassCastException=20
48. (AB)
An n -by-n two-dimensional array =
contains=20
Comparable values. The values in each row are =
increasing. The=20
columns alternate: in the first, third, and other odd columns the =
values=20
are increasing and in the second, fourth, and other even columns =
the=20
values are decreasing. What is the average big-O for an optimal =
algorithm=20
that finds a given value in such an array?
O (log =
n )=20
O ((log=20
n )2 )=20
O (n ) =
O (n =
log n )=20
=
O (n 2 )=20
49.
Consider the following classes:
public abstract class =
PointXY { =20
private int x, y;
public PointXY(int x, int y) { =
this.x=20
=3D x; this.y =3D y; } public int =
getX() {=20
return x; } public int getY() { =
return y;=20
} public String toString() { return =
"(" +=20
getX + "," + getY() + ")"; }
public abstract PointXY =
moveBy(int dx,=20
int dy); public abstract double=20
distanceFrom(PointXY p); }
public abstract class CartesianPoint extends=20
PointXY { public =
CartesianPoint(int x,=20
int y) { < Code not shown > =
} =20
public double distanceFrom(PointXY p) { < Code not =
shown >=20
} }
public class BoardPosition extends=20
CartesianPoint { < Code not =
shown > }
Which of the following is the minimal set of public =
constructors and/or=20
methods required in the BoardPosition class, so that the =
statements
BoardPosition pos =3D new BoardPosition(0,=20
0); System.out.println(pos.moveBy(10,=20
10).distanceFrom(pos));
compile with no errors?
public =
PointXY moveBy(int=20
dx, int dy)=20
public =
boardPosition(int=20
x, int y) and public PointXY moveBy(int dx, int =
dy)=20
public =
double=20
distanceFrom(PointXY pos)and public PointXY =
moveBy(int dx,=20
int dy)=20
public =
double=20
distanceFrom(BoardPosition pos)and public =
BoardPosition=20
moveBy(int dx, int dy)=20
public=20
BoardPosition()and public BoardPosition(int x, int=20
y)and public PointXY moveBy(int dx, int dy)=20
50.
A multiple-choice question deals with the scores =
that four=20
students received in a contest. The question offers the following =
answer=20
choices:
Tim got more points than Jenny.=20
Tim is the contest winner.=20
Jenny is in last place.=20
Tim's score is above average, and Jenny's score is below =
average.=20
While Nina did better than Phil, the boys' combined score =
is=20
higher than the girls' combined score.
The question assumes that one option is true and all the rest =
are=20
false. But the question is badly designed, making it possible to =
guess the=20
correct answer from the given choices without even looking at the=20
question. What is the correct answer?
a=20
b=20
c=20
d=20
e
2003
51.
fun is defined as follows:
public int fun(int[] =
v) { =20
v[0]--; return v[0] +=20
2; }
What is the value of v[0] after the =
following code segment is executed?
int [] v =3D { 3, 4, 5 }; v[0] =3D fun(v);=20
1=20
2=20
3=20
4=20
5
52.
The method xProperty is defined as =
follows:
public boolean xProperty(int=20
a) { return a =3D=3D 2 * (a / 10 =
+ a %=20
10); }
For which of the following =
values of=20
a does xProperty(a) return=20
true?
2=20
8=20
18=20
28=20
128 =
53.
What are the values of m and n after the following =
code=20
runs?
int m =3D 20, n =3D 2, temp;
for (int count =3D 1; count <=3D 20;=20
count++) { temp =3D=20
m; m =3D n +=20
count; n =3D temp -=20
count; }
m =3D =
230 n =3D -208=20
m =3D =
30 n =3D -8=20
m =3D =
12 n =3D -10=20
m =3D =
-12 n =3D 8=20
m =3D =
-190 n =3D 212 =
54.
Consider the method
public int[] copyX(int[] =
arr) { =20
int[] result =3D new int[arr.length];
for (int i =3D 0; i < =
arr.length;=20
i++) =20
{ if =
(arr[i]=20
<=3D=20
=
0) &=
nbsp; =20
break; =
if=20
(arr[i] >=20
=
10) =
=20
break; =
result[i] =3D arr[i]; =20
} return result; }=20
Suppose it is rewritten as follows:
public int[] copyX(int[]=20
arr) { int[] result =3D new=20
int[arr.length]; int i =3D =
0;
while (< condition =
>) =20
{ =
result[i]=20
=3D =
arr[i]; =20
i++; } =
return=20
result; }
Which of the following expressions can replace <=20
condition > so that the second version is =
equivalent to=20
the first one (i.e., for any int[] parameter=20
arr, it returns the same result as the first=20
version)?
i < =
arr.length=20
&& (arr[i] <=3D 0 || arr[i] > 10)=20
=
(arr[i] <=3D 0 ||=20
arr[i] > 10) || i >=3D arr.length=20
=
(arr[i] <=3D 0 ||=20
arr[i] > 10) && i < arr.length=20
i < =
arr.length=20
&& !(arr[i] <=3D 0 || arr[i] > 10)=20
i < =
arr.length=20
&& arr[i] > 0 && arr[i] <=3D 10) =
55.
Given
double x =3D < any positive value less than =
2003 >;=20
which of the following code fragments set int y to the smallest =
integer=20
that is NOT less than three quarters of x?
I. int y =3D (int)(3 * x /=20
4); if (y < 3 * x / 4) =
y++;
II. int y =3D =
1; =20
while (y < 3 * x / 4) y++;
III. int y =3D 2010 - (int)(2010 - x * 3 =
/=20
4);
I only=20
II only=20
I and II=20
I and III=20
I, II, and III =
56.
Alicia is five years older than her brother Ben. =
Three years=20
from now Alicia's age will be twice Ben's age. How old are Alicia =
and Ben=20
now? Ha wrote the following program to solve this puzzle:
public class =
AliciaAndBen { =20
public static void main(String[] =
args) =20
{ for =
(int a =3D=20
1; a <=3D 100;=20
=
a++)  =
; =20
for (int b =3D 1; b <=3D 100;=20
=
b++)  =
; =20
if (< condition =20
=
>) &nbs=
p;  =
; =20
System.out.println("Alicia is " + a + " and Ben is " +=20
b); } } =
Which of=20
the following expressions should replace < =
condition =20
> in Hal's program so that it displays the correct =
solution to=20
the puzzle?
(a =3D=3D b =
- 5) && (a=20
- 3 =3D=3D 2 * (b - 3))=20
a =3D=3D b + =
5 && a +=20
3 =3D=3D 2 * b + 6=20
(a =3D=3D (b =
+ 5)) &&=20
((a + 3) ++ (2 * b + 3))=20
a =3D=3D (b =
- 5) && (2=20
* a - 3) =3D=3D (b - 3)=20
None of the above =
works=20
57. (AB)
Suppose a two-dimensional array of =
chars=20
m has 4 rows and 5 columns and holds the values =
represented=20
in the picture below:
=
x.xxx xx..x .x.xx xx...
What =
are=20
the values in m after the following code segment is =
executed?
int r, c; for (r =3D 1; r < m.length;=20
r++) for (c =3D 1; c < =
m[0].length - 1;=20
c++) =
if=20
(m[r-1][c-1] =3D=3D 'x' && m[r-1][c+1] =3D=3D=20
=
'x')  =
; =20
m[r][c] =3D 'x';
58.
What is the output of the following code?
String s =3D =
"ONION"; System.out.println(s.substring(1,=20
5).substring(1, 4).substring(0, =
3));
I=20
IO=20
ION=20
ONI=20
NION =
59.
What is the smallest required number of three =
comparisons in=20
an optimal algorithm (based on comparing values) that puts any =
THREE=20
distinct values into a list in ascending order? What is the answer =
for=20
FOUR distinct values?
2 and 3=20
3 and 4=20
3 and 5=20
3 and 6=20
6 and 12 =
60.
Given
int counts[] =3D {7, 2, 9, 0, 1, 5, 5, 3,=20
9};
What does find3(counts, 9) =
return?=20
find3 is defined as follows:
public int find3(int a[], int=20
targetsum) { int i =3D 0, sum =3D =
0;
while (i <=20
3) =20
{ sum =
+=3D=20
a[i]; =20
i++; }
if (sum =3D=3D=20
=
targetsum) =20
return 1;
while (i <=20
a.length) =20
{ sum =
+=3D a[i]=20
- =
a[i-3]; if=20
(sum =3D=3D=20
=
targetsum)  =
; =20
return i - =
1; =20
i++; =
} }
-1=20
1=20
2=20
3=20
8
61. (AB)
Let us call an array "oscillating" if its values =
alternate=20
going up and down, as follows: a[i-1] < a[i] and=20
a[i] > a[i+1] for any odd i, 0 =
< i=20
< n-1, where n is the number of elements in =
a. What is the "big-O" for an optimal algorithm that=20
determines the minimum value for an oscillating array of length=20
n? The median value?
 =
; =20
=
Minimum =20
Median
=20
=
O(1) &nb=
sp; =20
O(1)=20
=20
=
O(n /2) =
=20
O(1)=20
=20
=
O(n ) &n=
bsp; =20
O(n )=20
=20
=
O(n ) &n=
bsp; =20
O(n log n )=20
=20
=
O(n ) &n=
bsp; =20
O(n 2 )
62. (AB)
Suppose an n by n matrix of "black" and "white" =
pixels=20
(e.g.,1s and 0s) represents a picture of a black blob that fills =
the=20
southeastern corner of the picture. The blob's boundary extends in =
a=20
generally southwest-to-northeast direction. All the pixels below =
and to=20
the right of any black pixel are black. For example:
=
0000001 0000001 0000011 0001111 0111111 11111111111111
What=20
is the worst case "big-O," in terms of n, for the total number of =
integer=20
additions plus pixel comparisons in an optimal algorithm that =
determines=20
the area of a blob (the number of black pixels in the =
blob)?
O (log =
n )=20
O ((log=20
n )2 )=20
O (n ) =
O (n log =
n )=20
=
O (n 2 )=20
Questions 63-67 refer to the following interface =
and=20
classes, written by Kim, a novice programmer, for modelling a =
"Caller ID"=20
device:
public interface Call { =
String=20
getSource(); }
public class IncomingCall implements=20
Call { private String=20
telephoneNumber;
public IncomingCall() {=20
telephoneNumber =3D ""; } public=20
IncomingCall(String tel) { telephoneNumber =3D tel; }
public void =
setTelephoneNumber(String=20
tel) { telephoneNumber =3D tel; =
}
public String getSource() { =
return=20
telephoneNumber; } public String =
toString()=20
{ return getSource(); } }
public class IncomingCallWithName extends=20
IncomingCall { private String=20
callerName;
public =
IncomingCallWithName(String=20
tel, String name) =20
{ < =
missing statement =20
> =20
callerName =3D name; }
public String getName() { =
return=20
callerName; }
public String=20
getSource() { return =
super.getSource() + " "=20
+ getName(); }
public String=20
toString() { return =
super.getSource() + " "=20
+ getName(); } }
63.
Kim's teacher specified that
System.out.println(new =
IncomingCallWithName("800-749-2000",=20
"Pizza Palace"));
should display
800-749-2000 Pizza Palace
Which of the following statements can replace < =
missing=20
statement > in the=20
IncomingCallWithName constructor to achieve =
that?
I. =20
=
super(tel);II. setTelephoneNumber(tel);=
III. telephoneNumber=20
=3D tel;
I only=20
II only=20
I and II=20
II and III=20
I, II, and III =
64.
What is the result of the following code segment?
Call[] calls =3D new Call[3]; calls[0] =3D new=20
=
IncomingCall("888-888-8888"); &n=
bsp; &nb=
sp;=20
// Line 1 calls[1] =3D (IncomingCallWithName)=20
=
calls[0]; &nbs=
p; =20
// Line 2 System.out.println(calls[0] + " " + calls[1] + " " =
+=20
calls[2]); // Line 3
Syntax error on =
line 1=20
Syntax error on =
line 2=20
=
ClassCastException=20
on Line 2=20
=20
NullPointerException on Line 3=20
Compiles and runs =
with no=20
errors; the output is: =20
888-888-8888 888-888-8888 null
65.
After correctly completing the=20
IncomingCallWithName constructor, as requested in =
Question=20
63, Kim wrote the following test code:
IncomingCall call =3D new =
IncomingCallWithName("800-749-2000",=20
=
"PizzaPalace"); System.out.println(call);
 =
;The=20
output was
800-749-2000 PizzaPalace
Kim's teacher suggested that Kim try to compile and run the =
program=20
again with IncomingCallWithName's =
toString=20
method commented out. What would be the result of this=20
experiment?
Syntax error=20
"IncomingCallWithName shoul dbe declared abstract"=20
Infinite =
recursion, stack=20
overflow=20
The program =
compiles with no=20
errors and displays 800-749-2000
The program =
compiles with no=20
errors and displays the same output as before, =
800-749-2000=20
PizzaPalace=20
The program =
compiles with no=20
errors and displays IncomingCallWithName@47e553 =
66.
Suppose calls and name are initialized as follows:
Call[] calls =3D { new=20
IncomingCallWithName("800-749-2000", "Pizza=20
Palace"), new=20
IncomingCall("888-237-3757"), new=20
IncomingCallWithName("800-555-1234", "Burger =
Heaven") };
String name =3D "Pizza =
Palace";
The=20
following code segment is intended to count the number of=20
Call objects in the calls array that came from a =
given=20
source:
String name =3D IO.readline(); // Read the name of the=20
source int count =3D 0; for (int i =3D 0; i < =
calls.length;=20
i++) if ( < condition > =
) =20
count++;
Which of the following =
replacements=20
for ( < condition > ) will compile with =
no=20
errors and correctly set count to 1?
=20
calls[i].getSource().indexOf(name) >=3D 0=20
=20
calls[i].toString().indexOf(name) >=3D 0=20
=20
calls[i].getName().equals(name)=20
=
((IncomingCallWithName)=20
calls[i]).getName().equals(name)=20
None of the above =
67. (AB)
Consider the following class:
public class CallerID { =
public=20
List calls;
public=20
CallerID() =20
{ =
calls =3D new=20
LinkedList(); }
// precondition: calls hold=20
IncomingCall objects // =
postcondition: all=20
calls that came from target are removed from the calls=20
list public void removeAll(String =
target) {=20
< code not shown > }
< other methods not =
shown =20
> }
If removeAll works as specified in its pre- and=20
postconditions, which of the following code segments can serve as=20
removeAll's body?
I. for (int i =3D 0; i < =
calls.size();=20
t++) =20
{ if =
(((IncomingCall)=20
=
calls.get{i}).getSource().equals(target)) &nbs=
p; =20
calls.remove(i); =20
}II. int i =3D=20
0; while (i < calls.size();=20
t++) =20
{ if =
(((IncomingCall)=20
=
calls.get{i}).getSource().equals(target)) &nbs=
p; =20
=
calls.remove(i);  =
;=20
=
else  =
; =20
i++; =20
}III. Iterator iter =3D=20
calls.iterator(); while=20
(iter.hasNext()) =20
{ if =
(((IncomingCall)=20
=
calls.get{i}).getSource().equals(target)) &nbs=
p; =20
iter.remove(i); =20
}
I only=20
II only=20
I and II=20
II and III=20
I, II, and III =
68. (AB)
What is the output of the following code segment?
Map m =3D new TreeMap(); m.put("La", =
"La"); m.put("La-La",=20
"La"); m.put("La-La-La", "Ye-Ye"); Iterator it =3D=20
m.keySet().iterator(); while=20
(it.hasNext()) =20
System.out.println(m.get(it.next()) + " =
");
La =
Ye-Ye=20
La La =
Ye-Ye=20
La =
La-La-La=20
La La =
La-La-La=20
Ye-Ye=20
La La La-La =
La La-La-La=20
Ye-Ye
69. (AB)
Suppose ListNode head refers to the =
first node=20
of a linked list. Consider the following code fragment:
ListNode node, temp;
for (node =3D head; node !=3D null;=20
node.getNext()) { while =
(node.getNext()=20
!=3D null &&=20
=
node.getNext().getValue().equals(node.getValue())) &=
nbsp;=20
{ =20
=
node.setNext((node.getNext().getNext()))l =20
} }
If head refers to a =
linked list=20
with 11 nodes that hold letters
"M", "I", "S", "S", "I", "S", "S", "I", "P", "P",=20
"I"
(in that order), what letters are =
stored in the=20
nodes of this list after the above code is executed?
"M"=20
"M", "I", =
"S"=20
"M", "I", =
"I", "I"=20
"M", "I", =
"S", "P"=20
"M", "I", =
"S", "I", "S",=20
"I", "P", "I"
70. (AB)
Consider the following method:
public int mysteryCount(TreeNode=20
root) { int count =3D =
0;
if (root !=3D=20
null) =20
{ =
count =3D=20
mysteryCount(root.getLeft()) +=20
=
mysteryCount(root.getRight()); &nb=
sp; =20
if ((root.getLeft() =3D=3D null && root.getRight() =
=3D=3D null)=20
=
|| &=
nbsp; =20
(root.getLeft() !=3D null && root.getRight() !=3D null =
&&=20
=
root.getLeft().getValue().equals(root.getRight().getValue()))) &=
nbsp; &n=
bsp;=20
count++; =20
} }
If root refers to the =
tree
=
&nbs=
p; =20
=
A &n=
bsp; =20
/ =20
=
\ &n=
bsp; =20
/ =20
=
\ &n=
bsp; =20
B =20
=
C &n=
bsp;=20
/ \ / =20
=
\ =20
D D D =20
=
D &n=
bsp; =20
/ \ /=20
=
\ &n=
bsp; =20
E E E =
E
which value is=20
returned by mysteryCount(root)?
1=20
3=20
4=20
5=20
10
71. (AB)
Consider the following method that creates a binary =
tree=20
from a linked list:
public TreeNode listToTree(ListNode=20
head) { if (head =3D=3D null ||=20
head.getNext() =3D=3D=20
null) =
return=20
null; =20
else =
return=20
new=20
=
TreeNode(head.getValue(), &n=
bsp; &nb=
sp; =20
=
lisToTree(head.getNext()), &=
nbsp; &n=
bsp; =20
=
listToTree(head.getNext().getNext())); }
If=20
head refers to a linked list with five nodes=97
"A", "B", "C", "D", "E"
=97how =
many nodes in=20
the tree returned by listToTree(head) hold =
"E"?
0=20
3=20
4=20
5=20
8
72. (AB)
Consider the following method that builds a linked =
list from=20
a binary tree:
public ListNode treeToList(TreeNode=20
root) { if (root =3D=3D=20
null) =
return=20
null; else if (Math.random() <=20
0.5) =
return=20
new ListNode(root.getValue(),=20
treeToList(root.getLeft())); =20
else =
return=20
new ListNode(root.getValue(),=20
=
treeToList(root.getRight())); }
If root=20
refers to the tree
=
&nbs=
p; =20
=
A &n=
bsp; =20
/ =20
=
\ &n=
bsp; =20
/ =20
=
\ &n=
bsp; =20
B =20
=
C &n=
bsp;=20
/ \ =20
=
/ =20
D E F
which =
of the=20
following lists could possibly result from =
treeToList(root)?
I. A, B,=20
DII. A, B, C,=20
DIII. A, C,=20
F
I only=20
II only=20
I and II=20
I and III=20
I, II, and III =
73. (AB)
Consider the following method that builds a linked =
list from=20
a queue:
// precondition: q holds Comparable objects public =
TreeNode=20
queueToTree(ListQueue q) { if=20
=
(q.isEmpty()) =20
return null;
ListQueue q1 =3D new=20
ListQueue(); ListQueue q2 =3D new=20
ListQueue(); Comparable x, =
y;
x =3D (Comparable)=20
q.dequeue(); while=20
(!q.isEmpty()) =20
{ y =
=3D=20
(Comparable)=20
=
q.dequeue(); =20
if (y.compareTo(x) <=20
=
0) &=
nbsp; =20
=
q1.enqueue(y); =
=
else  =
; =20
q2.enqueue(y); }
return new TreeNode(x,=20
queueToTree(q1), =
queueToTree(q2)); }
If q=20
contains letters A, P,=20
R, I, C, O, =
T=20
(represented by Character or String =
objects, in=20
this order, from front to rear), what is the result of inorder =
transversal=20
(left-to-right) of the tree returned by=20
queueToTree(q)?
=
A,=20
C, I, O, P,=20
R, T=20
=
A,=20
P, R, T, I,=20
C, O=20
=
A,=20
P, R, C, I,=20
O, T=20
=
A,=20
C, O, I, T,=20
R, P=20
=
A,=20
P, I, C, O,=20
R, T
74. (AB)
Consider the following method eval:
String eval(Queue q) { =
Stack stk=20
=3D new ArrayStack(); String s =3D=20
"";
while=20
(!q.isEmpty()) =20
{ s =
=3D (String)=20
q.dequeue();
=
if=20
=
(!s.equals("+"))  =
; =20
=
stk.push(s); =20
else =20
=
{ &n=
bsp; =20
String s1 =3D "", s2 =3D=20
=
""; =
=20
if=20
=
(!stk.isEmpty())  =
; =20
s2 =3D (String)=20
=
stk.pop();  =
; =20
if=20
=
(!stk.isEmpty())  =
; =20
s1 =3D (String)=20
=
stk.pop();  =
; =20
stk.push(s1 +=20
s2); =20
} } if=20
=
(!stk.isEmpty())  =
;=20
s =3D (String) stk.pop(); return=20
s; }
The program reads strings (which =
may hold=20
single characters), separated by spaces, one by one from the input =
line=20
and puts them into a queue q. Then it displays the =
string=20
returned by eval(q). For which of the following input =
lines=20
is the output HELLO?
 =
; =20
=
front &=
nbsp; =20
↓ =20
H + E + L + =
L + O=20
H E L + + L =
O + +=20
H E + + L L =
+ O=20
+ + + + O L =
L E H=20
None of the above =
75.
A multiple-choice test contains 25 questions. The =
correct=20
answers include five A's, five B's, five C's, five D's, and five =
E's. Both=20
Constance and Randy solve the first 15 questions correctly but =
then run=20
out of time. Constance picks the letter (or one of the letters) =
that is=20
least frequent among the first 15 answers and fills the remaining =
10=20
answers with this letter. Randy picks random answers for the =
remaining 10=20
questions, but he makes sure that at the end each letter A-E =
appears=20
exactly five times among all 25 answers. Which of the following =
statements=20
is FALSE?
Randy and =
Constance can get the=20
same score=20
Randy's score can =
be any number=20
from 15 to 25=20
Constance scores =
not more than=20
17=20
Constance scores =
not more than=20
20=20
Neither Randy nor =
Constance can=20
score exactly 24
2002
76.
If a is and int array of =
length 2=20
and a[0] and a[1] holds values 7 and 13=20
respectively, what are their values after fun(a) is =
called?=20
The method fun is defined as follows:
public void fun(int[] =
x) { x[0]=20
=3D (int)(100.0 * x[0] / (x[0] + =
x[1])); x[1]=20
=3D (int)(100.0 * x[1] / (x[0] + x[1])); } =
7 and 13=20
35 and 27=20
34 and 64=20
35 and 65=20
34 and 66 =
77.
Consider a method
public boolean isProcessedX(int n, int[]=20
v) { if (n >=3D 2 &&=20
isProcessedX(n-1, v)) =20
{ =
v[n-1] =3D=20
=
v[n-2]; return =
true; } =20
else =
return=20
false; }
What happens if an int array s holds =
values=20
1, 2, 3, 4, 5 and isProcessedX(5, s) is =
called?
s =
holds 1, 2, 3, 4,=20
5 and isProcessedX returns false=20
s =
holds 1, 2, 3, 4,=20
4 and isProcessedX returns true=20
s =
holds 1, 1, 1, 1,=20
1 and isProcessedX returns true=20
=20
indexOutOfBoundsException=20
Stack overflow =
error
78.
An array mix holds seven elements. For =
which=20
seven values in mix will the value of the variable=20
property be true after the following =
code=20
segments is executed?
int i; boolean property =3D true;
for (i =3D 1; i < 6; =
i++) { if=20
(mix[i] >=3D mix[i-1] || mix[i] >=3D=20
=
mix[i+1]) =20
property =3D false; }
1, 2, 3, 4, 5, 6, =
7=20
7, 6, 5, 4, 3, 2, =
1=20
7, 1, 6, 2, 5, 3, =
4=20
1, 2, 3, 4, 3, 2, =
1=20
1, 5, 2, 6, 3, 7, =
4
79. (AB)
The class Game has a data member =
char[][]=20
board and a constructor defined as follows:
public Game() { board =
=3D new=20
char[4][4]; int r, c;
for (r =3D 0; r < 4;=20
r++) =
for (c =3D=20
0; c < 4;=20
=
c++)  =
; =20
board[r][c] =3D '.';
for (r =3D 0; r < 4;=20
r++) =20
board[r][(r + 1) % 4] =3D 'x'; }
What =
values are=20
stored in board when a Game object is=20
constructed with the above constructor?
80.
Consider the following method prepare:
public String prepare(String=20
s) { int k =3D s.length() /=20
2; if (k <=3D=20
1) =
return=20
s; return s.charAt(k - 1) +=20
prepare(s.substring(0, k - 1) + s.substring(k + 1, 2*k)) +=20
s.charAt(k); }
what does prepare("LEMONADE") =
return?
=
ONMAEDLE=20
=
OLEMADEN=20
=
OMELEDAN=20
=
OOOONNNN=20
=
LEMONADE=20
81.
What are the values of a and =
b=20
after the following code fragment is executed?
int a =3D 3, b =3D 5, s; for (int i =3D 0; i < =
10;=20
i++) { s =3D a +=20
b; b =3D a - =
b; a=20
=3D s; }
0 and 96=20
96 and 0=20
96 and 96=20
96 and 160=20
160 and 96 =
82.
If int weekDay contains =
the code=20
for the day of the week on November 1 (0 for Sunday, 1 for Monday, =
..., 6=20
for Saturday), which of the following expressions gives the date =
for=20
Thanksgiving (the fourth Thursday in November)?
weekDay + =
26=20
26 - =
weekDay=20
(4 - =
weekDay) % 7 +=20
22=20
(11 - =
weekDay) % 7 +=20
22=20
(weekDay + =
3) % 7 +=20
22
83.
The method average3 below "simultaneously" replaces =
all=20
elements in an array. Each element is replaced with the average of =
that=20
element's current value and its left and right neighbors. If a =
"neighbor"=20
is outside the array, its value is assumed to be 0:
public void average3 (double=20
a[]) { int i, len =3D=20
a.length; double tempSaved[] =3D new =
double[len]; for (i =3D 0; i < =
len;=20
i++) =20
tempSaved[i] =3D a[i];
double leftNeighbor,=20
rightNeighbor;
for (i =3D 0; i < len;=20
i++) =20
{ if =
(i >=20
=
0) &=
nbsp; =20
leftNeighbor =3D=20
=
tempSaved[i-1]; =
=20
=
else  =
; =20
leftNeighbor =3D=20
0; if =
(i <=20
len -=20
=
1) &=
nbsp; =20
rightNeighbor =3D=20
=
tempSaved[i+1]; =
=20
=
else  =
; =20
rightNeighbor =3D=20
0; =
a[i] =3D=20
(leftNeighbor =3D tempSaved[i] =3D rightNeighbor) /=20
3; } }
Suppose the first and lest elements in v are =
zeroes. Which=20
of the following statements is FALSE after =
average3(v) is=20
called? (Disregard all inaccuracies that may be introduced due to=20
floating-point arithmetic.)
If v =
holds the=20
values 0, 3, 6, 9, 12, 9, 6, 3, 0, the resulting array will hold =
1, 3,=20
6, 9, 10, 9, 6, 3, 1.=20
The sum of all the =
elements in=20
v remain the same.=20
If in the original =
array the sum=20
of all the values in even positions, v[0] + v[2] + =
..., is=20
the same as the sum of all the values in the odd positions, =
v[1] +=20
v[3] + ..., then the same will remain true for the =
resulting=20
array.=20
The maximum value =
in the=20
resulting array does not exceed the maximum value in the =
original array.=20
The position of =
the maximum=20
element in the resulting array remains the same as in the =
original array=20
or shifts to one of the two neighboring positions. =
84. (AB)
An array arr contains n =
integer=20
elements whose values form an arithmetic sequence (i.e., =
arr[i+1] -=20
arr[i] =3D=3D arr[i] - arr[i-1] for any 0 < i =
<=20
n-1). Whatr is the "big-O" for an optimal algorithm =
that=20
determines whether such an array contains two given =
values?
O (1)=20
O (log =
n )=20
O (2 log =
n )=20
O (n ) =
=
O (n 2 )=20
85.
Given
Random generator =3D new Random(); int bigNum =3D =
10000; int=20
r =3D generator.nextInt(bignum);
which of the following expressions is the best way to =
initialize=20
x to the value of a randomly chosen element from an =
array=20
arr of 3 values? (The odds for choosing any element =
must be=20
the same or almost the same.)
x =3D arr[r =
/ bignum *=20
3];=20
x =3D =
arr[(int)(3.0 * r /=20
bignum)];=20
x =3D =
arr[(int)(2.9 * r /=20
bignum)];=20
x =3D =
arr[(int)(3.0 * (r -=20
1) / (bignum -1)];=20
x =3D arr[3 =
* (int)((double)=20
r / bigNum)];
86.
Given three integer variables, a,=20
b, and c, with small non-negative =
values, which=20
of the following code fragments tests the condition that any two =
of the=20
values are zeroes while the third one is positive? The variable=20
ok should be set to true if and only if the above =
condition=20
is true.
I. boolean ok=20
=3D a =
=3D=3D 0=20
&& b =3D=3D 0 && c > 0=20
|| b =
=3D=3D 0=20
&& c =3D=3D 0 && a > 0=20
|| c =
=3D=3D 0=20
&& a =3D=3D 0 && b >=20
0;II. boolean ok =3D =
a + b + c=20
> 0 && a*b + b*c + c*a =3D=3D=20
0;III. boolean ok =3D a =
> 0 || b=20
> 0 || c > 0; if (ok) =
ok =3D a +=20
b =3D=3D 0 || b + c =3D=3D 0 || c + a =3D=3D=20
0;
I only=20
II only=20
I and II=20
I and III=20
I, II, and III =
Questions 87-91 are based on the following =
classes:
public class Person implements=20
Comparable { private String=20
name;
public Person(String name) { =
this.name=20
=3D name; } public String getName() =
{ return=20
name; }
public boolean equals(Object=20
other) =20
{ =
return other=20
!=3D null && name.equals(((Person)=20
other).name); }
public int compareTo(Object=20
other) =20
{ =
return=20
name.compareTo((Person other).name); =
}
public int hashCode() { return =
name.hashCode(); } }
public class SoccerPlayer extends=20
person { private int =
numGoals;
public SoccerPlayer(String =
name, int=20
n) =20
{ =20
=
super(name); =20
numGoals =3D n; }
public int getNumGoals() { =
return=20
numGoals; } public void score() [=20
numGoals++; }
public int =
compareTo(SoccerPlayer=20
other) =20
=
{ return =
getNumGoals() - other.getNumGoals(); =
}
public String=20
toString() =20
{ =
return=20
gatName() + "/" + getNumGoals(); =20
} }
87.
Which of the following declarations is =
invalid?
Person p =3D =
new Person("Mia=20
Hamm");=20
SoccerPlayer =
p =3D new=20
Person("Mia Hamm");=20
Comparable p =
=3D new=20
Person("Mia Hamm");=20
Person p =3D =
new=20
SoccerPlayer("Kristine Lilly", 0);=20
Comparable p =
=3D new=20
SoccerPlayer("Kristine Lilly", 0);
88.
What is the result of the following code?
Person players[] =3D { new SoccerPlayer("Mia Hamm", 7), =
new=20
SoccerPlayer("Kristine Lilly", 6)=20
}; System.out.println(players[0].compareTo((SoccerPlayer)=20
players[1])); // Line=20
***
Syntax error in =
the class=20
Person: other.name is not accessible=20
Syntax error in =
the class=20
SoccerPlayer: compareTo is redefined=20
=
ClassCaseException=20
on Line ***=20
Compiles with no =
errors,=20
displays 1=20
Compiles with no =
errors,=20
displays 2
89. (AB)
Suppose the Person and=20
SoccerPlayer classes are changed as follows:
other.name is replaced with =
other.getame()=20
and compareTo in SoccerPlayer is renamed =
into=20
compareGoals. What will be the result of running the=20
following code segment?
SoccerPlayer mia =3D new SoccerPlayer("Mia Hamm",=20
6); SoccerPlayer kristine =3D new SoccerPlayer("Kristine =
Lilly",=20
5); Set team =3D new=20
=
HashSet(); team.add(mia); team.add(kristine); kristine.score();<=
BR>team.add(kristine); Iterator=20
iter =3D team.Iterator(); // Line=20
*** while(iter.hasNext()) =20
System.out.print(iter.next() + " =
");
Kristine =
Lilly/5 Mia=20
Hamm/6=20
Kristine =
Lilly/6 Mia=20
Hamm/6=20
Mia Hamm/6 =
Kristine=20
Lilly/5 Kristine Lilly/6=20
Kristine =
Lilly/5 Kristine=20
Lilly/6 Mia Hamm/6=20
Syntax error on =
Line ***=20
Questions 90-91 are concerned with a class =
SoccerTeam=20
that represents a team of soccer players:
public class =
SoccerTeam { =20
private SoccerPlayer[] players; =
private=20
ArrayList mvps; //holds all the players =
who=20
scored the same highest number of goals
public void score(int=20
k) =20
{ =20
players[k].score(); // Line=20
*1* =
int goals=20
=3D=20
=
players[k].getNumGoals(); &n=
bsp; =20
int maxGoals =3D ((SoccerPlayer)=20
mvps.get(0)).getNumGoals(); // Line=20
*2* if =
(goals=20
>=3D=20
=
maxGoals) =20
=
{ &n=
bsp; =20
if (goals =3D=3D maxGoals) // Line=20
=
*3* =
=20
=
mvps.add(players[k]); =
=20
=
else  =
; =20
=
{ &n=
bsp; =20
// mvps is left with only one player in it,=20
=
players[k]: &nbs=
p; =20
< missing statements =20
=
>  =
; =20
} =20
} }
< constructors and other =
methods=20
not shown > }
90.
SoccerTeam's score method =
is=20
intended to update the number of scored goals for a given player =
on the=20
team and update the list of "most valuable players" (all of whom =
have the=20
same score, the highest on the team). If the player's new score is =
higher=20
than the old best, the mvps list is updated to =
contain only=20
that one player. However, the score method has an =
error.=20
Which of the following actions would correct that error?
I. Move Line *2* before Line=20
*1* II. Replace Line *3*=20
with =
if (goals=20
=3D=3D maxGoals && players[k] !=3D=20
mvps.get(0)) III. Replace Line =
*3*=20
with =
if=20
(goals =3D=3D maxGoals && !=3D=20
mvps.contains(players[k]))
I only=20
II only=20
I and II=20
II and III=20
I, II, and III =
91.
Which of the following would be an appropriate =
replacement=20
for < missing statements > in=20
SoccerTeam's score method?
mvps.set(0,=20
players[k]);=20
=20
mvps.reize(0); =
mvps.add(players[k]);=20
mvps =3D new =
ArrayList(); mvps.add(players[k]);=20
mvps =3D new =
ArrayList(1); mvps.set(0, players[k]);=20
delete=20
mvps; mvps =3D new ArrayList(): =20
mvps.add(players[k]);
92. (AB)
The elements in an array of size n are first =
increasing=20
(a[i] < a[i+1]), until they reach a maximum value, then =
decreasing=20
(a[i] > a[i+1]). What are the respective "big-O" estimates for =
the=20
number of comparisons in two optimal algorithms, one that finds a =
maximum=20
value in such an array and the other that sorts the array in =
ascending=20
order?
O (log=20
2 ) and O (n )=20
O (log =
n ) and=20
O (n log n )=20
O (n ) =
and=20
O (n )=20
O (n ) =
and=20
O (n log n )=20
O (n ) =
and=20
O (n 2 )
93. (AB)
What is the output from the following code segment?
Set set =3D new TreeSet(); String str =3D=20
"A"; set.add(str); str +=3D "B"; set.add(str); str =
+=3D=20
"C"; set.add(str); Iterator iter =3D =
set.Iterator(); while=20
(iter.hasNext()) =20
System.out.print(iter.next() + =
"-");
A-=20
ABC-=20
A-B-C-=20
A-AB-ABC-=20
None of the above =
94. (AB)
Suppse ListNode p1, p2 =
initially=20
refer to two nodes in the same circular linked list. Under what =
conditions=20
does the following loop terminate?
do { p1 =3D=20
p1.getNext(); p2 =3D=20
ps.getNext().getNext(); } while (p1 !=3D=20
p2);
Always=20
If and only if =
p1 =3D=3D=20
p2.getNext()=20
If and only if the =
total number=20
of nodes in the list is even=20
If and only if the =
number of=20
nodes from p2 to p1 (excluding both =
ends of=20
this segment of the list) is even=20
If and only if the =
list contains=20
two nodes with the same info
95. (AB)
What does the following code display?
String expr =3D "(a + b) / (2 * (a - b)"; Stack stk =
=3D new=20
ArrayStack(); int i, k;
for (k =3D 0; k < expr.length();=20
k++) { if (expr.charAt(k) =3D=3D=20
'(') =20
{ =
stk.push(new=20
Integer(k + 1)); =20
} else =20
{ i =
=3D=20
((Integer)=20
=
stk.pop()).intValue();  =
; =20
System.out.println(expr.substring(i, k)); =20
} }
(2 * (a -=20
b)) (a - b) (a + b)=20
(a - =
b) (2=20
* (a - b)) (a + b)=20
a -=20
b a + b (2 * (a - =
b))=20
a +=20
b a - b 2 * a - =
b=20
a +=20
b) a - b) 2 * (a -=20
b))
96.
The following method packed analyzes a string passed =
to it=20
and returns a new string:
String packed(String =
msg) { =20
String packedMsg =3D "";
for (int i =3D 0; i < =
msg.length();=20
i+) =20
{ if=20
(msg.charAt(i) !-=20
'.') =20
=
{ &n=
bsp; =20
int len =3D=20
=
packedMsg.length(); &n=
bsp; =20
if (len =3D=3D 0 || msg.charAt(i) !=3D=20
=
packedMsg.charAt(len-1)) &nb=
sp; =20
packedMsg +=3D msg.substring(i,=20
i+1); =20
} } =
return=20
packedMsg; }
For which of the following values of msg, is =
packed(msg) NOT equal to=20
packed("xxo.ooo.xx.x")?
xxo..ooo..xx..x=20
..xxooooooxxxx..=20
xxoooxxx=20
xxooooooxxox=20
xox =
97. (AB)
What is the value of sum after the =
following=20
code is executed?
int sum =3D 0; int r =3D 0, c =3D 0; int temp, d1 =
=3D 1; d2 =3D=20
2; int table[][] =3D new int[8][8];
for (int k =3D 0; k < 64;=20
k++) { table [r][c] =3D=20
1; r =3D (r + d1) %=20
8; c =3D (c + d2) %=20
8; temp =3D d1; d1 =3D d2; d2 =3D=20
temp; }
for (r =3D 0; r < 8; =
r++) for (c =3D=20
0; c < 8;=20
c++) =
sum +=3D=20
table[r][c];
8=20
15=20
16=20
22=20
64
Questions 98-99 assume that TreeNode root points =
to the=20
following binary tree:
=
&nbs=
p; =20
=
A &n=
bsp; =20
/=20
=
\ &n=
bsp; =20
B =20
=
C &n=
bsp;=20
/ /=20
=
\ =20
D E =20
=
F &n=
bsp; =20
=
\ &n=
bsp; =20
G
98. (AB)
Consider the following method traverse:
void traverse(TreeNode =
root) { =20
if (root !=3D null) =20
{ =20
=
traverse(root.getLeft()); &n=
bsp; =20
=
traverse(root.getRight()); &=
nbsp; =20
=
System.out.print(root.getValue());  =
; =20
=
traverse(root(getRight()); &=
nbsp; =20
traverse(root.getLeft()); =20
} }
How many letters will be displayed =
when=20
traverse(root) is called?
1=20
7=20
13=20
14=20
25
99. (AB)
Consider the following method:
public void grow(TreeNode=20
root) { if (root !-=20
null) =20
{ if=20
(root.getLeft() !=3D null && root.getRight !=3D=20
=
null) &nbs=
p; =20
root.setRight(new TreeNode("X", null,=20
=
null)); else=20
if (root.getLeft() =3D=3D null && root.getRight !=3D=20
=
null) &nbs=
p; =20
root.setLeft("X", null,=20
=
null)); =20
=
grow(root.getLeft()); =
=20
grow(root.getRight()); =20
} }
Which of the following represents the resulting tree after=20
grow(root) is called?
100.
An exam contains 24 questions for 40 minutes. Some =
questions=20
are easy, while other questions are really hard. Constance and =
Skip always=20
solve any easy question in 20 seconds, but a hard question always =
takes=20
each of them 3 minutes. Constance's strategy is to take each =
question in=20
turn and take whatever time it takes to solve it. Skip tries a =
question=20
for 20 seconds and if he can't solve it, he moves on to the next =
one. If=20
he has looked at all the questions, Skip returns to the unsolved =
hard=20
questions, but he has to start solving them from scratch because =
by then=20
he has forgotten what they were about. If the first half of the =
test=20
contains at least 8 easy questions and the second half contains at =
least 8=20
hard questions, which of the following statements is =
FALSE?
Skip will solve =
at least 18=20
questions=20
Constance will =
solve at least=20
20 questions=20
If at most 10 of =
the questions=20
are hard, both Skip and Constance will solve all 24 questions=20
Skip will solve =
at most 10 hard=20
questions=20
Constance will =
always solve at=20
least as many questions as Skip
2001
101.
What is the output of the following code?
int a =3D 1, b =3D 2, c =3D 3; a +=3D b + c; b =
+=3D a + c; c +=3D=20
a + b; System.out.println(a + " " + b + " " +=20
c);
3 3 =
4=20
3 5 =
6=20
5 4 =
3=20
5 8 =
13=20
6 11 =
20=20
102.
Consider two methods:
public int f(int x) { =
return x +=20
2; }
and
public int g(int x) { =
return x *=20
2; }
What is the value of x after the following code=20
executes?
int x =3D 1; x +=3D f(g(x)) - g(f(x));=20
-9=20
-2=20
-1=20
3=20
7
103.
What is the value of y after the =
following code=20
is executed?
int x =3D 123; while (x >=20
0) { y *=3D =
10; =20
y +=3D x % 10; x /=3D=20
10; }
1=20
3=20
6=20
12=20
321 =
104. (AB)
The method xWon below is supposed to =
return=20
true is a tic-tac-toe board (represented by a 3-by-3 =
array of=20
characters) has three x's in any line, =
false=20
otherwise:
public boolean xWon (char[][]=20
b) { if (b[1][1] =3D=3D=20
'x') =20
{ if =
(b[0][0]=20
=3D=3D 'x' && b[2][2] =3D=3D 'x') return=20
true; =
if=20
(b[0][1] =3D=3D 'x' && b[2][1] =3D=3D 'x') return=20
true; =
if=20
(b[1][0] =3D=3D 'x' && b[1][2] =3D=3D 'x') return=20
true; =
if=20
(b[0][2] =3D=3D 'x' && b[2][0] =3D=3D 'x') return=20
true; } =
else if=20
(b[0][0] =3D=3D 'x') =20
{ if =
(b[0][1]=20
=3D=3D 'x' && b[0][2] =3D=3D 'x') return=20
true; =
if=20
(b[1][0] =3D=3D 'x' && b[2][0] =3D=3D 'x') return=20
true; } =
else if=20
(b[2][2] =3D=3D 'x') =20
{ if =
(b[2][0]=20
=3D=3D 'x' && b[2][1] =3D=3D 'x') return=20
true; =
if=20
(b[0][2] =3D=3D 'x' && b[1][2] =3D=3D 'x') return=20
true; } =
return=20
false; }
105.
Consider the following method:
public void divide5 (int[] a, int[] q, int[]=20
r) { q[0] =3D a[0] /=20
5; r[0] =3D a[0] % 5; }=20
What is the value of y[0] after the following =
statements=20
are executed?
int[] x =3D {21, 22, 23], y =3D new =
int[3]; divide5(x, y, z);=20
0=20
1=20
2=20
4=20
21 =
106. (AB)
Suppose the method fun is defined as =
follows:
public void fun (int m[][], String=20
s) { for (int i =3D 1; i < =
s.length();=20
i++) =20
{ int =
r =3D=20
Character.digit(s.charAt(i),=20
10); =
int c =3D=20
Character.digit(s.charAt(i - 1),=20
10); =20
m[r][c]++; } } =
What is the output when the following code fragment is =
executed?
int m[][] =3D new int[10][10]; String s =3D=20
"20012002";
fun(m, s); int sum =3D 0; for (int k =3D 0; k =
< 10;=20
k++) sum +=3D=20
m[k][k]; System.out.println(sum); =
1=20
2=20
4=20
7=20
8
107.
Which of the following conditions is always true =
after the=20
while loop in the following code fragment has =
finished=20
(assuming it executes without errors)?
int k =3D 0; n =3D 10; while (k < n && =
a[k] >=3D=20
0) k++; =
k >=3D n =
&& a[k]=20
< 0=20
k < n =
&& a[k]=20
< 0=20
k < n || =
a[k] <=20
0=20
k >=3D n =
|| a[k] <=20
0=20
None of the above =
108.
The method
public boolean xyz (String =
s) { =20
return s.length() >=3D 3=20
=
&& =20
((s.charAt(0) =3D=3D s.charAt(1)=20
=
&&  =
; =20
s.charAt(1) =3D=3D s.charAt(2)) || xyz(s.substring(1))); }=20
returns true if and only if
s =
contains three=20
or more of the same characters in a row=20
s =
starts with=20
three or more of the same characters=20
s =
ends with three=20
or more of the same characters=20
s =
contains three=20
or more of the same characters=20
s[0] =
is the same=20
as two other characters in s
109.
The following version of Selection Sort is supposed =
to sort=20
an array in ascending order. For better performance it tries to =
tackle the=20
array from both ends simultaneously:
public void sort (int =
a[]) { int=20
left =3D 0, right =3D a.length() - =
1; int=20
k;
while (left <=20
right) =20
{ for =
(k =3D=20
left + 1; k < right;=20
k+) =20
=
{ &n=
bsp; =20
if (a[k] <=20
=
a[left]) &=
nbsp; =20
swap(a, k,=20
=
left); &nb=
sp; =20
else if (a[k] >=20
=
a[right]) =
=20
swap(a, k,=20
=
right); =20
} =20
=
left++; =20
right--; } } =
swap(a, i, j) correctly swaps a[i] =
and=20
a[j]. This code has a bug, though. Which one of the =
following=20
changes would assure that the method sorts the array =
correctly?
I. Remove else=20
in =
else if=20
(a[k] > a[right]) =
...II. =20
=
Replace =20
for (k =3D left + 1; k < right;=20
k+) =20
=
with for =
(k =3D left; k <=3D right; =
k+)III. =20
Add =20
if (a[left] >=20
=
a[right]) =
=20
swap(a, left,=20
=
right); at=20
the beginning of the while loop (before the=20
for loop).
I only=20
II only=20
III only=20
I or II=20
II or III =
110. (AB)
Insertion Sort is a sorting algorithm that works as =
follows:=20
keep the first k elements of the array sorted; find the right =
place and=20
insert the next element among the first k. These steps are =
repeated for k=20
- 1, ..., n. Sequential Search is usually used to find the right =
spot in=20
which to insert the next element. Suppose we use Binary Search =
instead of=20
Sequential Search in this algorithm. How would Big-O for the =
number of=20
comparisons among the elements (not counting the number of moves =
or swaps)=20
change?
No change=20
From =
O (n ) to=20
O (log n)=20
From=20
O (n 2 ) to =
O (n 2 /2)=20
From=20
O (n 2 ) to O (n log n)=20
From=20
O (n 2 ) to O (n ) =
Questions 111-112 are based on the following =
class that=20
represents a moving ball on a rectangular pool table:
public class =
MovingBall { =20
private int mLength, mWidth; private =
int=20
mPosX, mPosY; private int mDirX,=20
mDirY;
public MovingBall(int length, =
int=20
width, int dx, int dy) =20
{ =
mLength =3D=20
=
length; mWidth =
=3D =
width; mPosX=20
=3D length / =
2; =20
mPosY =3D width /=20
2; =
mDirX =3D=20
dx; =
mDirY =3D=20
dy; }
public void=20
move() =20
{ =
mPosX +=3D=20
mDirX; =
mPosY=20
+=3D =
mDirY; if=20
(mPosX =3D=3D 0 || mPosX =3D=3D mLength) mDirX =3D=20
=
-mDirX; id=20
(mPosY =3D=3D 0 || mPosY =3D=3D mWidth) mDirY =3D=20
-mDirY; =
} }
111.
Giiven
MovingBall b =3D new MovingBall(8, 4, 1,=20
-1);
what are the values of mPosX and =
mPosY after=20
70 moves (i.e., 70 calls to b.move())?
74 and -68=20
6 and 4=20
4 and 2=20
3 and 1=20
1 and -1 =
112.
If
MovingBall b =3D new MovingBall(9, 4, 1,=20
1);
is defined, how many moves (i.e., calls to =
b.move()) will=20
ball b hot position mPosX =3D 6, mPosY =3D=20
1?
Never=20
2=20
3=20
7=20
30 =
113.
Suppose all the elements in an array have different =
values.=20
Let us say the array is "nearly sorted" (in ascending order) if =
each=20
element's position differs from its appropriate position in the =
sorted=20
arrangement of the same array by at most 2 (in either direction). =
The=20
following method takes an array where the first n elements =
are=20
"nearly sorted" and properly sorts the array.
public void sortNearlySorted(int[] arr, int=20
n) { int i =3D 0;
while (i < n =3D=20
1) =20
{ if =
(arr[i+1]=20
<=20
=
arr[i]) &n=
bsp; =20
swap(arr, i, i +=20
1); if =
(i+2=20
< n && arr[i+2] <=20
=
arr[i]) &n=
bsp; =20
swap(arr, i,=20
i+2); =20
i++; } }
Which of the following is a loop invariant for the =
while=20
loop in the above method?
arr[0] ... =
arr[n-1] are sorted=20
and 0 <=3D i < n=20
arr[0] ... =
arr[n-1] are nearly=20
sorted and 0< n-1=20
arr[0] ... =
arr[i-1] are placed=20
where they belong in the sorted array and arr[i] ... arr[n-1] =
are=20
"nearly sorted"=20
arr[i] < =
arr[i+1] and arr[i]=20
< arr[i+2]=20
arr[0] ... =
arr[n-3] are sorted=20
114.
In the ABBAB language the alphabet has only two =
letters. A=20
string of letters (including and one-letter strings) is a valid =
word, if=20
and only if the isValid method returns =
true for=20
that string. isValid is defined as follows:
public boolean isValid(String=20
word) { int n =3D =
word.length();
return n < 1=20
|| =20
(isValid(word.substring(0, n-1) &&=20
=
&nb=
sp; =20
word.charAt(n-1) =3D=3D 'B')=20
|| =20
(isValid(word.substring(0, n-2)=20
=
&&  =
; =20
"BA".equals(word.substring(n-2))); }
How many valid words of length 7 are there in the ABBABA=20
language?
2=20
3=20
15=20
23=20
34 =
115. (AB)
The method below takes a linked list pointed to by=20
head, removes the first node, appends it at the end =
of the=20
list, and returns a reference to the head of the new list.
public ListNode firstToLast (ListNode=20
head) { if (head =3D=3D null) ||=20
head.getNext() =3D=3D=20
null) =
return=20
head;
ListNode p =3D=20
head; while (p.getNext() !=3D=20
null) =
p =3D=20
p.getNext();
< missing statements =
>
return head; }=20
Which of the following code fragments correctly completes this=20
method?
I. =
ListNode=20
temp =3D head; =
head =3D=20
head.getNext(); =20
p.setNext(temp); =20
temp.setNext(null);II. =20
ListNode temp =3D=20
head.getNext(); =20
=
head.setNext(null); =20
p.setNext(head); =
head =3D=20
temp;III. =
=20
p.setNext(head); =
head =3D=20
head.getNext(); =20
=20
p.getNext().setNext(null);
I only=20
II only=20
I and II=20
II and III=20
I, II, and III =
116. (AB)
The following method goodHand checks =
whether s=20
stack of Integers (that represent ranks of playing =
cards in a=20
small deck) has a certain property:
public boolean goodHand (Stack=20
hand) { int[] count =3D new=20
int[2]; Object x, y;
for (int k =3D 0; k <=3D 1; =
k++) =20
{ if=20
=
(hand.isEmpty())  =
; =20
return =
false; =20
x =3D=20
=
hand.pop(); =20
Stack stk =3D new=20
=
ArrayStack(); =20
while=20
=
(!hand.isEmpty()) &nbs=
p;=20
=
{ &n=
bsp; =20
y =3D=20
=
hand.pop(); &nbs=
p; =20
if=20
=
(x.equals(y)) &n=
bsp; =20
=
count[k]++; &nbs=
p; =20
=
else  =
; =20
=
stk.push(y); =20
} hand =
=3D=20
stk; } =
return=20
Math.abs(count[0] - count[1]) <=3D 1 && =
hand.isEmpty(); }=20
For which of the following stacks does goodHand =
return=20
true?
top =
=3D=3D>
117. (AB)
An n by n square image contains black =
and=20
white pixels. The first several columns contain one or more black =
pixels=20
at the top with only white pixels below; the remaining columns are =
all=20
white. For example:
=
xxxx.. xxxx.. x.xX.. x.x... ..x... ......<=
/P>
A program is allowed to examine individual pixels in the image; =
its=20
task is to find the position of the lowest black pixel in the =
rightmost=20
column that has at least one black pixel (the uppercase pixel in =
the above=20
example). What is the worst case big-O for the number of examined =
pixels=20
in the best possible algorithm.
O (log n)=20
O ((log=20
n) 2 )=20
=
O (n )=20
O (n log =
n )=20
=
O (n 2 )=20
Questions 118-122 use the following classes:
public class Point { =
private int=20
x; private int y;
public Point(int x, int y) { =
this.x =3D=20
x; this.y =3D y; }
public int getX() { return x;=20
} public int getY() { return y; =
}
public boolean equals(Point=20
other) =20
{ =
return=20
getX() =3D=3D other.getX() && getY() =3D=3D=20
other.getY(); }
public int=20
hashCode() =20
{ =
return (new=20
Integer(getX() + =
getY())).getHashCode(); =20
}
public void setX(int x) { =
this.x =3D x;=20
} public void setY(int Y) { this.y =
=3D y;=20
}
public String=20
toString() =20
{ =
return "(" +=20
getX() + ", " + getY() + ")"; =20
} }
public class MovingPoint extends=20
Point { private Point =
myPoint;
public MovingPoint(Point=20
p) =20
{ < =
missing statements =20
> =
myPoint =3D=20
p; }
public int getX() { return=20
myPoint.getX(); } public int getY() =
{ return=20
myPoint.getY(); }
public void move(int x, int=20
y) =20
{ =20
=
myPoint(setX(x)); &nbs=
p;=20
myPoint(setY(y)); }
public String=20
toString() =20
{ =
return=20
myPoint.toString(); =20
} }
118.
Which of the following can replace < missing=20
statements > in MovingPoint's =
constructor?
=
super();=20
super(0, =
0);=20
setX(0); =
setY(0);=20
super(); =
setX(p.getX());=20
seyY(p.getY());=20
// set =
myPoint to=20
p:
119.
Which line in the following code segment causes =
a =20
syntax error?
Point p1 =3D new Point(100,=20
100); // =
Line=20
1 MovingPoint p2 =3D new MovingPoint(p1); // Line =
2 Point p3 =3D=20
new MovingPoint(p2); =
// Line=20
3 MovingPoint p4 =3D new MovingPoint(p2); // Line 4=20
No syntax errors =
on these lines=20
Line 1=20
Line 2=20
Line 3=20
Line 4 =
120.
What is the output of the following code?
Point p =3D new Point(0, 0); MovingPoint mp =3D new=20
MovingPoint(p); mp.move(1, 1); System.out.println(p + " " =
+=20
p.equals(mp));
=20
NullPointerException=20
=
ClassCastException=20
(0, 0) =
true=20
(0, 0) =
false=20
(1, 1) =
true=20
121. (AB)
Consider the following two code segments:
Set points =3D new HashSet(); MovingPoint =
p;
p =3D new MovingPoint(new Point(0,=20
0)); points.add(p);
p =3D new MovingPoint(new Point(0,=20
2)); points.add(p);
p =3D new MovingPoint(new Point(2,=20
2)); points.add(p);
p =3D new MovingPoint(new Point(2,=20
0)); points.add(p);
p =3D new MovingPoint(new Point(1,=20
1)); points.add(p);
=
System.out.println(points.size());
Set points =3D new HashSet(); MovingPoint =
p;
p =3D new MovingPoint(new Point(0,=20
0)); points.add(p);
p.move(0, 2); points.add(p);
p.move(2, 2); points.add(p);
p.move(2, 0); points.add(p);
p.move(1, 1); points.add(p);
=
System.out.println(points.size());
<=
/TR>
What are their respective outputs?
3 =
and=20
1=20
3 =
and=20
3=20
5 =
and 1=20
5 =
and=20
3=20
5 =
and=20
5
122. (AB)
What is the output of the following code segment?
Point p =3D new Point(0, 0); MovingPoint p1 =3D new=20
MovingPoint(p); movingPoint p2 =3D new =
MovingPoint(p); Queue q =3D new=20
ListQueue(); p1.move(1, 0); q.enqueue(p1); p2.move(0,=20
1); q.enqueue(p2); System.out.println(q.dequeue() + " " +=20
q.dequeue());
(0, 0) (0, =
0)=20
(1, 0) (1, =
0)=20
(0, 1) (0, =
1)=20
(1, 0) (0, =
1)=20
(0, 1) (1, =
0)=20
123. (AB)
Consider the following method:
public int magic (TreeNode=20
root) { if (root !=3D=20
null) =20
{ if=20
(root.getLeft() =3D=3D null && root.getRight() =3D=3D=20
=
null) &nbs=
p; =20
return =
0; if=20
(magic(root.getLeft()) + magic(root.getRight()) =3D=3D=20
0) =20
=
{ &n=
bsp; =20
TreeNode temp =3D=20
=
root.getLeft(); =
=20
=
root.setLeft(root.getRight()); &nb=
sp; =20
=
root.setRight(temp); &=
nbsp;=20
} } =
return=20
-1; }
What does this method do to a =
tree=20
referred to by root?
Nothing, leaves =
the tree=20
unchanged=20
Swaps the left =
and right=20
branches of the tree at the root=20
Replaces the tree =
with its=20
mirror image=20
Swaps any two =
leaves that have=20
the same parent=20
Swaps the =
leftmost and=20
rightmost leaves
124. (AB)
Suppose traversePreOrder and=20
traversePostOrder are defined as follows:
public void traversePreOrder(TreeNode root, Stack=20
s) { if (root !=3D=20
null) =20
{ =20
=
s.push()(root.getValue()); &=
nbsp; =20
traversePreOrder(root.getLeft(),=20
s); =20
traversePreOrder(root.getRight(), =
s); } }=20
public void traversePostOrder(TreeNode root, Stack=20
s) { if (root !=3D=20
null) =20
{ =20
traversePostOrder(root.getLeft(),=20
s); =20
traversePostOrder(root.getRight(),=20
s); =20
root.setValue(s.pop()); } }=20
If root initially refers to
=
&nbs=
p; =20
=
A &n=
bsp; =20
/=20
=
\ &n=
bsp; =20
/ =20
=
\ &n=
bsp; =20
B =20
=
C &n=
bsp;=20
/ \ /=20
=
\ =20
D =
E F G
what is=20
the resulting tree after the following statements are executed?
Stack s =3D new ArrayStack =
(); traversePreOrder(root,=20
s); traversePostOrder(root, =
s);
125.
A multiple-choice question offers three options, I, =
II, and=20
III, and asks which ones fit into a given situation. The offered =
answers=20
are a) I only; b) II only; c) III only; d) I and II; and e) II and =
III.=20
Assuming that it takes the same amount of time to examine any one =
of the=20
three options, that the odds that any option fits are 50-50, and =
that s=20
student always gets all answers right and doesn't waste time =
considering=20
unnecessary options, which option should she consider =
first?
Doesn't matter=20
Either I or II=20
Either I or III=20
Definitely I=20
Definitely II =
2000
126.
Which of the following statements does NOT display=20
2/3?
=20
System.out.println("2/3");=20
=
System.out.println("2" +=20
"/" + "3");=20
=20
System.out.println(2/3);=20
=
System.out.println(2 +=20
"/" + 3);=20
=
System.out.println((int)2=20
+ "/" + (int)3);
127.
If array arr has five elements with =
values=20
0 1 2 3 4, what are the values in arr =
after the=20
following code is executed?
for (int k =3D 0; k < 5;=20
k++) arr[k] =3D=20
arr[arr[k]];
0 1 2 3 =
4=20
1 2 3 4 =
0=20
1 2 2 2 =
2=20
0 0 0 0 =
0=20
1 2 3 4 =
4=20
128.
If c and d are=20
boolean variables, which of the answer choices is NOT =
equivalent to the following expression?
(c && d) !=3D (c || =
d)
(c =
&& !d) || (!c=20
&& d)=20
(c || d) =
&& (!c=20
&& !d)=20
(c || d) =
&& (!c=20
|| !d)=20
(c || d) =
&& !(c=20
&& d)=20
c !=3D =
d
129.
The value of (1 + (1/n))n for large enough n (e.g., =
n >=3D=20
50) approximates the base of the natural logarithm e =3D =
2.71828... A=20
student decided to test this property and wrote the following =
method:
public double =
approxE() { double=20
e =3D 1.0 + 1.0 / 64; for (int count =
=3D 1;=20
count <=3D 6;=20
=
count++) e =
*=3D=20
e; return =
e; }
What value is returned by approxE()? =
No value is =
returned: the=20
method throws an ArithmeticException=20
1.0=20
2.6973449525651=20
1.642359568597906 =
7.275669793128421 =
130.
What is the output from the following code?
int[] counts =3D new int[3]; int i, j; for (int i =
=3D 0; i=20
< 100; i++) for (j =3D 0; j < =
10;=20
j++) =
counts[j=20
% 3]++; System.out.println((counts[1] + counts[2]) /=20
counts[0]);
1=20
1.5=20
2=20
2.5=20
3
131.
Suppose an array A has n=20
elements. Let's call it periodic with a period of =
p if=20
0 < p < n and A[i] =
=3D=3D=20
A[i+p] for all 0 <=3D i < =
n-p=20
and p is the smallest such number. What is the period =
of=20
array v after the following code is executed?
int v[] =3D new=20
int[100]; v[0] =3D 0; v[1] =3D =
1;
for (int i =3D 2; i < 100;=20
i++) =
v[i] =3D=20
v[i-1] - v[i-2];
2=20
3=20
4=20
6=20
No period =
132.
What is the output from the following code?
StringBuffer s =3D new=20
StringBuffer("WYOMING"); int k, i, n =
=3D=20
s.length(); char temp;
for (k =3D 0; k < 3;=20
k++) =20
{ temp =
=3D=20
=
s.charAt(n-1); =
for (i =3D k+1; i < n;=20
=
i++)  =
; =20
s.setCharAt(i,=20
=
s.charAt(i-1)); =
=20
s.setCharAt(k, temp); =20
} =20
System.out.println(s);
=
YOMINGW=20
=
MINGWYO=20
=
GWWWWWW=20
=
MINGOYW=20
=
GNIMOYW=20
133.
A recursive method upNdown is defined =
as=20
follows:
public void upNdown(int =
n) { if=20
(n > 1) =20
{ if =
(n % 2 !=3D=20
0)=20
=
upNdown(n+1); =20
else=20
=
upNdown(n/2); =20
System.out.println("*"); =20
} }
How many stars are displayed when upNdown(5) is=20
called?
1=20
2=20
3=20
4=20
5
134.
Given
String a =3D "a", b =3D "b", zero =3D ""; Integer c =
=3D new=20
Integer(0);
what is the output of
System.out.println( =20
((a+b)+c).equals(a+(b+c)) + " " + (c =
+=20
zero).equals(zero + c) + " " + (a +=20
null).equals(a + "null"));
false false =
false=20
false true =
true=20
true false =
true
true true =
false=20
true true =
true=20
135. (AB)
The method below uses a stack to check whether =
parentheses=20
and brackets match in a string of characters:
public boolean parensAndBracketsMatch(String=20
expr) { Stack s =3D new=20
ArrayStack(); char ch0, =
ch;
for (int k =3D 0; k < =
expr.length();=20
k++) =20
{ ch =
=3D=20
=
expr.charAt(k); =
=20
if (ch =3D=3D '(' || ch =3D=3D=20
=
'[')  =
; =20
s.push(new=20
=
Character(ch)); =
=20
else if (ch =3D=3D ')' || ch =3D=3D=20
']') =20
=
{ &n=
bsp; =20
if=20
=
(s.isEmpty()) &n=
bsp; =20
return=20
=
false; &nb=
sp; =20
ch0 =3D=20
=
((Character)s.pop().charValue(); &=
nbsp; =20
if ((ch0 =3D=3D '(' && ch !=3D ')')=20
=
|| &=
nbsp; =20
(ch =3D=3D '[' && ch !=3D=20
=
']')) &nbs=
p; =20
return =
false; =20
} } =
return=20
true; }
However, it has a bug. For which of the following strings does =
this=20
method return a result that is DIFFERENT from what is =
expected?
 =
; =
&=
nbsp; &n=
bsp; =20
Expected result:
"[(a+b) *=20
=
(c+d)]/2" &nbs=
p; =20
true=20
"[(a+b) *=20
=
(c+d)/2]" &nbs=
p; =20
true=20
"[(a+b) *=20
=
(c+d)]/2[" &nb=
sp; =20
false=20
"](a+b) *=20
=
(c+d)/2[" &nbs=
p; =20
false=20
"[(a+b] *=20
=
[c+d)/2"  =
; =20
false
136.
Consider the following two versions of the method=20
mixUp:
public void mixUp(int[] x, int[]=20
y) { x[0] =3D x[0] - 2 * =
x[0] *=20
y[1]; y[1] =3D y[1] - 2 * x[0] =
*=20
y[0]; }
public void mixUp(int[] x, int[]=20
y) { int d =3D 2 * x[0] *=20
y[1]; x[0] -=3D=20
d; y[1] -=3D=20
=
d; }
Suppose the=20
following arrays are declared and initialized:
int[] a =3D {1, 1}, b =3D {0, 0}, c =3D {1, 1};=20
Which of the following calls to mixUp result in =
the same=20
values in a, b, and c for =
bother=20
versions of the code?
I. mixUp(a,=20
a)II. mixUp(a,=20
b)III. mixUp(a,=20
c)
I only=20
II only=20
I and II=20
II and III=20
I, II, and III =
137.
The Binary Search algorithm normally finds a value =
in an=20
array sorted in ascending order. Suppose that by mistake the =
algorithm is=20
used on an unsorted array with the following seven elements:
1 3 2 5 13 8 21
Which of the following target values will NOT be found? =
1=20
3=20
5=20
13=20
21 =
138.
An array has 4095 =3D 212 -1 elements, =
arranged in=20
ascending order. Binary Search is used to find the position of a =
target=20
value. This Binary Search is implemented iteratively, in such a =
way that=20
in each iteration the target is compared to one of =
the=20
elements of the array. Suppose we know that the =
target is=20
somewhere in the array. What number of iterations guarantees that =
a target=20
value is found.
10=20
11=20
12=20
2047=20
4095 =
139.
Which of the following arithmetic expressions maps =
the=20
values x =3D 0, 1, 2, 3, 4, 5, 6 onto y =
=3D 4, 3, 9,=20
8, 7, 6, 5, respectively?
y =3D 11 - =
x + (x + 4) %=20
7;=20
y =3D (4 - =
x) % 7 + 2 *=20
x;=20
y =3D 3 + =
(8 - x) %=20
7;=20
y =3D 9 - =
(x - 2) %=20
7;=20
y =3D 4 + x =
% 7 - 2 *=20
x;
140.
The method below implements a simplified square =
cipher:
public char[][] encrypt(char[][] key, String=20
msg) { int i, j, n =3D =
key.length, k =3D=20
0; char[][] result =3D new=20
char[n][n]; // fills with spaces
for (i =3D 0; i < n;=20
i++) =
for (j =3D=20
0; j < n;=20
=
j++)  =
; =20
if (key[i][j] =3D=3D=20
=
'x')  =
; =20
result[i][j] =3D msg.charAt(k++);
for (i =3D 0; i < n;=20
i++) =
for (j =3D=20
0; j < n;=20
=
j++)  =
; =20
if (key[i][j] =3D=3D=20
=
'x')  =
; =20
result[n-i-1][n-j-1] =3D msg.charAt(k++);
return=20
result; }
If key is a 4 by 4 matrix
.x.. x..x .xx. xx.x
and msg is "ransom received ", which =
of the=20
following matrices is returned from encrypt(key,=20
msg)?
Questions 141-142 use the following class:
public class Circle { =
private=20
int xCenter, yCenter, radius;
public Circle(int x, int y, =
int=20
r) =20
{ =
xCenter =3D=20
x; =
yCenter =3D=20
y; =
radius =3D=20
r; }
public void moveTo(int x, int=20
y) =20
{ =
xCenter =3D=20
x; =
yCenter =3D=20
y; }
// Draw this circle in the =
graphics=20
context g public void draw(Graphics =
g)=20
{ < code not shown =20
> =
} }
141.
Suppose the following code is added to a method that =
repaints a window within a graphics context g:
for (int x =3D 10; x <=3D 30; x +=3D=20
10) { Circle circle =3D new =
Circle(x + 100,=20
100, x); =20
circle.draw(g); }
The origin of the coordinate system in in the upper left corner =
of the=20
window with the y-axis pointing down. Which of the following =
pictures will=20
be displayed?
142.
The method drawOrnament draws several circles, as =
follows:
public void drawOrnament(Graphics g, int=20
k) { if (k <=20
8) =20
return; Circle c =3D new Circle(100 =
- k, 100 -=20
k, k); =20
c.draw(g); c.moveTo(100 + k, 100 +=20
k); =
c.draw(g); =20
drawOrnament(g, k/2); }
Which of the following pictures is produced by =
drawOrnament(g,=20
32)?
143.
What is displayed by
=
System.out.println(expand(expand("1001")));
where=20
expand is defined as follows?
public String expand(String =
s) { =20
String d =3D "";
for (int i =3D 0; i < =
s.length();=20
i++) =
switch=20
=
(s.charAt(i)) =20
=
{ &n=
bsp; =20
case '0': d +=3D "01";=20
=
break; &nb=
sp; =20
case '1': d +=3D "10";=20
break; =
} return =
d; }
1001 =
=
1*0*0*1=20
=
10010110=20
=
10*01*01*10*=20
=
1001011001101001=20
Questions 144-146 use the following interface and =
class:
public interface =
Toggleable { =20
void toggle(); boolean=20
isOn(); }
public class toggleSwitch =
implements=20
Toggleable =20
{ =
private=20
boolean on;
=
public=20
ToggleSwitch() { on =3D false;=20
} public =
ToggleSwitch(boolean state) { on =3D state; }
=
public=20
void toggle() { on !=3D on;=20
} public =
boolean=20
isOn() { return on; } =
}
144.
Suppose we have found the=20
ToggleSwitch.class file but have no access to its =
source=20
code. Which of the following code segments, if it compiles with no =
errors,=20
will convince us that ToggleSwitch implements=20
Toggleable?
I. Toggleable x =3D new=20
ToggleSwitch();II. =
ToggleSwitch x=20
=3D new ToggleSwitch(); =20
x.toggle();III. ToggleSwitch x =
=3D new=20
ToggleSwitch(); if (!x.isOn())=20
x.toggle();
I only=20
II only=20
III only=20
II and III=20
I, II, and III =
145.
Consider the following class that represents a set =
of=20
checkboxes
public class =
CheckBoxSet { =20
private Togleable[] buttons;
public CheckBoxSet(int=20
nButtons) =20
{ < =
missing statements > =
}
public int numButtons() { =
return=20
buttons.length; } public void =
push(int k) {=20
buttons[k].toggle(); } public =
boolean=20
isOn(int k) { return buttons[k].isOn(); }
public void=20
clear() =20
{ for =
(int k =3D=20
0; k < buttons.length;=20
=
k++)  =
; =20
if=20
=
(buttons[k].isOn()) &n=
bsp; =20
buttons[k].toggle(); =20
} }
Which of the following can replace < missing =
statements >=20
in the CheckBoxSet constructor?
I. buttons =3D new=20
Toggleable[nButtons]; for (int k =3D 0; k =
<=20
buttons.length; =
k++) =20
buttons[k] =3D new =
ToggleSwitch();II. =20
buttons =3D new ToggleSwitch[nButtons]; =20
clear();III. buttons =3D new=20
ToggleSwitch[nButtons]; for (int i =3D 0; =
i <=20
buttons.length; =
i++) =20
if=20
=
(buttons[i].isOn()) &n=
bsp; =20
buttons[i].toggle();
I only=20
II only=20
I and II=20
II and III=20
I, II, and III =
146. (AB)
A set of "radio buttons" is a user interface control =
used to=20
choose one of several options. For example:=20
Consider the =
following class=20
RadioSet that implements a set of radio buttons:
public class RadioSet extends=20
CheckBoxSet { // =
precondition: =20
nButtons > k >=3D 0 // =
postcondition:=20
creates an array of nButtons and =20
=
//  =
; =20
sets the k-th button to "on" public=20
RadioSet(int nButtons, intk) =20
{ =20
=
super(nButtons);  =
;=20
push(k); }
// postcondition: sets the =
k-th button=20
to "on", all other =20
=
//  =
; =20
buttons to "off" public void =
push(int=20
k) =20
{ < =
missing code > =
}
// postcondition: returns the =
number=20
of the button that is "on"; =20
=
//  =
; =20
throws an exception if none are "on" =
{ for =
(int k =3D=20
0; k < numButtons();=20
=
k++)  =
; =20
if=20
=
(isOn(k)) =
=20
return =
k; =20
throw IllegalStateException(); =20
} }
Which of the following can replace < missing code > =
in this=20
class?
I. =
clear(); =20
=
push(k);II. clear();  =
;=20
super.push(k);III. for (int j =
=3D 0; j=20
< numButtons();=20
j++) if=20
=
(buttons[j].isOn()) &n=
bsp; =20
buttons[j].toggle(); =20
buttons[k].toggle();
I only=20
II only=20
I and II=20
II and III=20
I, II, and III =
147. (AB)
char[][] m holds the following picture:
=
........ ..xxxxx. ..xx.... ..xxx... ....xxx. ....=
..x. .x.xxx.. ..xxx... ........=20
What picture is stored in m =
after=20
process(m) is called? The method process =
is=20
defined as follows:
public void process(char[][]=20
m) { int rows =3D m.length, cols =
=3D=20
m[0].length, r, c; char[][] t =3D =
new=20
char[rows][cols];
for (r =3D 0; r < rows;=20
r++) =
for (c =3D=20
0; c < cols;=20
=
c++)  =
; =20
t[r][c] =3D m[r][c];
for (r =3D 1; r < rows;=20
r++) =
for (c =3D=20
1; c < cols;=20
=
c++)  =
; =20
if (t[r][c] =3D=3D 'x' && (t[r-1][c] !=3D=20
=
'x' =
&=
nbsp; &n=
bsp; =20
|| t[r][c-1] !=3D=20
=
'x')) &nbs=
p; =20
m[r][c] =3D=20
=
'x';  =
; =20
=
else  =
; =20
m[r][c] =3D '.'; }
148.
Suppose Mergesort is implemented as follows:
public void sort(int[] a, int n1, int=20
n2) { if (n1 =3D=3D=20
n2) =20
return; else if (n2 =3D=3D n1 +=20
1) =20
{ if =
(a[n2]=20
<=20
=
a[n1]) &nb=
sp; =20
swap(a, n1, n2); // swaps a[n1] and=20
a[n2] =20
return; =
} int m=20
=3D (n1 + n2) / 2; sort(a, n1,=20
m); sort(a, m+1,=20
n2); if (a[m] >=20
=
a[m+1]) =20
merge(a, n1, m, n2); // merges a[n1]...a[m] =
and=20
a[m+1]...a[n2] }
How many times will =
the method=20
merge be called if an array a contains =
the=20
values
2 1 4 3 6 5 8 7
and =
sort(a, 0,=20
7) is called?
7=20
4=20
3=20
1=20
0
149. (AB)
Consider the following method:
public void xQ(Queue =
q) { if=20
=
(q.isEmpty()) =20
return;
Queue q1 =3D new ListQueue(), =
q2 =3D new=20
ListQueue(); Object x =3D=20
q.dequeue();
while=20
(!q.isEmpty()) =20
{ =
Object y =3D=20
=
q.dequeue(); =20
if (((Comparable)y).compareTo(x) <=3D=20
=
0) &=
nbsp; =20
=
q1.enqueue(y); =
=
else  =
; =20
q2.enqueue(y); }
=
xQ(q1); =20
xQ(q2);
while=20
=
(!q1.isEmpty()) =
=20
q.enqueue(q1.dequeue());
q.enqueue(x);
while=20
=
(!q2.isEmpty()) =
=20
q.enqueue(q2.dequeue()); }
What are the values in q after xQ(q) =
is=20
called, if q initially holds Integer =
objects=20
with the values 7, 11, 3,=20
5, 6, 0 (listed starting =
from the=20
front)?
0, =
3,=20
5, 6, 7, 11=20
0, =
6,=20
5, 3, 11, 7=20
3, =
5,=20
6, 0, 7, 11=20
11,=20
3, 5, 6, 0,=20
7=20
11,=20
7, 6, 5, 3,=20
0
150. (AB)
Consider the following method:
public int xSum(TreeNode =
root) { =20
if (root =3D=3D=20
null) =
return=20
0; =20
else =
return=20
((Integer)root.getValue()).intValue()=20
=
+ &n=
bsp; =20
xSum(root.getRight()) - xSum(root.getLeft()); }=20
What value is returned by xSum(root) if =
root=20
points to the following tree?
=
&nbs=
p; =20
=
5 &n=
bsp; =20
/=20
=
\ &n=
bsp; =20
4 =20
=
2 &n=
bsp; =20
/ /=20
=
\ &n=
bsp;=20
3 =20
=
7 1 &=
nbsp; &n=
bsp; =20
=
\ &n=
bsp; =20
12
-9=20
-10=20
5=20
12=20
20 =
1999
151.
What is the output from the following code?
int x =3D 5, y =3D 2; System.out.println(x/y - =
(double)(x/y));=20
0=20
0.5=20
-0.5 =
-2.5 =
None of the above =
152.
What are the values of u and =
v=20
after the following code is executed?
int u =3D 3, v =3D 5;
u +=3D v; v +=3D u; u -=3D v; v -=3D u;=20
0, 0=20
3, 5=20
5, 3=20
-5, -3=20
-5, 18 =
153.
Which of the following does NOT display=20
9.95?
=
System.out.println(9 +=20
0.95);=20
=20
System.out.println(995/100.0);=20
=
System.out.println(9. +=20
95/100);=20
=
System.out.println(9 +=20
95.0/100);=20
=
System.out.println(9 +=20
"." + 95);
154.
What is the output from the following code?
int count1 =3D 0, count2 =3D 0, inc =3D 1; for (int =
i =3D 0; i <=20
11; i++) { count1 +=3D=20
inc; inc =3D =
-inc; =20
count2 +=3D inc; } System.out.println(count1 -=20
count2);
0=20
2=20
-2=20
22=20
-22 =
155.
What is the result of the following code segment?
Integer i =3D new=20
=
Integer(0); &n=
bsp; &nb=
sp; =20
// Line 1 if=20
=
(!i.equals(0))  =
; =
&=
nbsp; =20
// Line 2 System.out.println(i + " =
does not=20
equal to " + 0); // Line =
3 else =20
System.out.println("Done");
Syntax error on =
Line 2=20
Syntax error on =
Line 3=20
=
ClassCastException=20
0 is not =
equal to=20
0=20
Done =
156.
What is the output from the following code?
int a =3D 0, b =3D 0; while (a <=20
3) { switch(a +=20
b) =20
{ case =
0:=20
a++; =
case=20
1: =
case 2:=20
b++; =
break; =20
case 3: a++;=20
break; =
default: b =3D 0; break; =20
} =20
System.out.print(b); }
0123 =
=
122011=20
=
0122011=20
=
0122123=20
=
112300
157.
What is the value of n after the =
following code=20
is executed?
int i, n =3D 0; while (n <=20
90) { for (i =3D 0; i < 10;=20
i++) =20
{ n =
+=3D=20
3; if =
(n >=20
=
50) =
=20
break; } =
n++; }
51=20
61=20
91=20
93=20
104 =
158.
What is the set of all possible outputs of the =
following=20
code when the user correctly follows the input instructions and =
the=20
program terminates without an error?
System.out.print( "Enter an =
integer=20
from -1000 to 1000 (inclusive): ");
int n =3D console.readInt(); // Read an =
integer
while (n > 0 && Math.sqrt(n) <=20
10.0) n++;
=
System.out.println(n);
{0, ..., 99, 100} =
{100, ..., 999, =
1000}=20
{-1000, -999, =
..., 0} union=20
{100, ..., 999, 1000}=20
{-1000, -999, =
..., 0, ..., 999,=20
1000}=20
{-1000, -999, =
..., 0, ..., 99}=20
159.
An integer array a has 19 elements. What is the =
value of the=20
middle element after the following code is executed?
int i, j, n =3D 19;
for (i =3D 0; i < n; =
i++) a[i] =3D=20
i+1;
for (i =3D 0, j =3D n-1; i <=3D j; i++,=20
j--) a[(i+j)/2] -=3D (a[i] + a[j]) / =
2;
0=20
10=20
-41=20
-62=20
-71 =
160.
Consider the following method:
public void mysteryMix(Integer a, Integer=20
b) { a =3D new Integer(2 *=20
a.intValue()); b =3D a; }=20
What is the output of the following code?
Integer a =3D new Integer(1); Integer b =3D new=20
Integer(2); mysteryMix(a, a); mysteryMix(a,=20
b); System.out.println(a + " " + b); =
1 1=20
1 2=20
2 2=20
1 4=20
4 4 =
161.
The following interface Index describes =
the=20
location of a document:
public interface Index { =
String=20
getKey(); Document =
getAddress(); }=20
Document is a class that has a public method=20
getSize(). An ArrayList folder contains=20
Index objects and describes a collection of =
documents. Which=20
of the following expressions refers to size of the=20
k-th document in folder?
=20
folder[k-1].getAddress().getSize()=20
(Index)=20
folder.get(k-1).getAddress().getSize()=20
((Index)=20
folder.get(k-1)).getAddress().getSize()=20
((Document) =
(folder[k-1].getAddress())).getSize()=20
((Document) =
(folder.get(k-1).getAddress())).getSize()
162.
Which of the following could safely appear and make sense in =
place of=20
< condition > in some suitable context?
if ( < condition =
>=20
=
) msg =3D =
new message("o");
I. msg =3D=3D null ||=20
=
msg.getStatus().equals("x")II. msg.=
getStatus().equals("x")=20
|| msg =3D=3D nullIII. =20
"x".equals(msg.getStatus()) || msg =3D=3D=20
null
I only=20
II only=20
I and II=20
II and III=20
I, II, and III =
Questions 163-167 use the following interface and =
classes=20
used in a picture drawing application:
public interface =
Drawable { void=20
draw(Graphics g); }
public class Line implements=20
Drawable =20
{ =
private int=20
xBeg, yBeg, xEnd, yEnd;
=
public=20
Line(int x0, int y0, int x1, int=20
y1) =20
=
{ &n=
bsp; =20
xBeg =3D x0; yBeg =3D y0; xEnd =3D x1; yEnd =3D=20
y1; =
}
=
public=20
void draw(Graphics=20
g) =20
=
{ &n=
bsp; =20
g.drawLine(xBeg, yBeg, xEnd,=20
yEnd); =20
} }
public class Picture implements=20
Drawable =20
{ =
private List=20
pictures;
=
public=20
=
Picture() =20
=
{ &n=
bsp; =20
pictures =3D new=20
=
LinkedList(); =20
}
=
public=20
void add(Drawable=20
obj) =20
=
{ &n=
bsp; =20
=
pictures.add(obj); &nb=
sp;=20
}
=
public=20
void draw(Graphics=20
g) =20
=
{ &n=
bsp; =20
Iterator it =3D=20
=
pictures.iterator(); &=
nbsp; =20
while=20
=
(it.hasNext()) &=
nbsp; =20
((Drawable)=20
=
it.next()).draw(g); &n=
bsp;=20
} }
163.
Which of the following code segments compiles with =
no=20
errors?
Drawable =
picture =3D new=20
Picture(new Line(0, 0, 100, 100)); =20
Drawable =
picture =3D new=20
Picture(); =20
picture.add(new Line(0, 0, 100, 100)); =20
Picture =
picture1 =3D new=20
Picture(); =
Picture=20
picture2 =3D new Picture(picture1); =20
Picture =
picture =3D new=20
Picture(); =20
picture.pictures.add(new Line(0, 0, 100, =
100)); =20
Drawable =
picture =3D new=20
Picture(); ((Picture)=20
picture).add(new Picture());
164.
Consider the following code segment:
Picture picture =3D new Picture(); picture.add(new =
Line(100,=20
100, 200, 50)); picture.add(new Line(200, 50, 300, =
100)); Picture=20
box =3D new Picture(); box.add(new Line(100, 100, 100,=20
300)); box.add(new Line(100, 300, 300, 300)); box.add(new=20
Line(300, 300, 300, 100)); box.add(new Line(300, 100, 100,=20
100));
Which of the following pictures will be displayed when=20
pictures.draw(g) is called from an appropriate=20
paint method within the graphics context=20
g?
165.
Suppose the class Box extends=20
Picture and has the following constructor:
public Box(int x, int y, int width, int=20
height) { =20
super(); add(new Line(x, y, x + =
width,=20
y)); add(new Line(x + width, y, x + =
width, y=20
+ height)); add(new Line(x + width, =
y +=20
height, x, y + height)); add(new =
Line(y, y +=20
height, x, y)); }
Suppose the statements
Box box =3D new Box(100, 100, 300, =
200); box.draw(g);=20
=97when executed from an appropriate paint method =
within the=20
graphics context g=97draw a box with the upper left corner at =
(100, 100),=20
width 300 and height 200. Besides the above constructor, which =
methods=20
must be supplied in the Box class for this to =
happen?
No methods are =
needed=20
=
add(Drawable)=20
=
draw(Graphics)=20
=
add(Drawable) and=20
add(Graphics)=20
=
add(Drawable), add(Line),=20
and draw(Graphics)
166.
Suppose the following statements have been added to the Line =
class:
public String getName() { return "Line"; }
public String =
toString() { =20
return "[" + getName()=20
=
+ =20
/* " " + xBeg + " " + yBeg=20
=
+ &n=
bsp; =20
" " + xEnd + " " + yEnd + =20
=
*/ =
"]"; }
Also, the following methods have been added to the =
Picture=20
class:
public String getName() { return "Picture"; =
}
public String =
toString() { =20
String str =3D "[" + getName() + " =
"; Iterator=20
it =3D pictures.iterator(); while=20
=
(it.hasNext()) =
str +=3D it.next(); return str + =
"]"; }=20
Finally, the following method has been added to the =
Box=20
class mentioned in the previous question:
public String getName() { return "Box"; }=20
What is the output of the following code?
Box box =3D new Box(100, 100, 100, 100); box.add(new =
Line(100,=20
100, 200, 200)); box.add(new Line(100, 200, 200, =
100)); Picture=20
picture =3D new=20
=
Picture(); picture.add(box); System.out.println(picture);
[Picture =
[Line] [Line]=20
[Line] [Line] [Line] [Line]=20
[Picture =
[Box =20
[Line] [Line] [Line] [Line] ] ]=20
[Picture =
[Box =20
[Line] [Line] [Line] [Line] [Line] [Line] ] ]=20
[Picture =
[Picture [Line]=20
[Line] [Line] [Line] [Line] [Line] ]=20
[Picture =
[Picture [Line]=20
[Line] [Line] [Line] [Line] [Line] ] ]
167.
The class DrawingBoard extends=20
JFrame and represents a window on the screen:
public class DrawingBoard extends=20
javax,Swing,JFrame { private =
Drawable=20
picture;
public DrawingBoard(Drawable=20
picture) { =20
this.picture =3D picture; }
public void paint(Graphics g) =
{=20
picture.draw(g); } }
Consider the following code segment:
Picture picture =3D new=20
=
Picture(); picture.add(picture); &n=
bsp; =20
// Line *** DrawingBoard w =3D new=20
DrawingBoard(picture); w.setBounds(50, 50, 400, =
400); w.show();=20
What happens when we try to compile and execute this =
code?
Syntax error on =
Line ***=20
=
ClassCastException=20
The code compiles =
and runs, but=20
goes into an infinite loop=20
=
StackOverflowError=20
The code compiles =
and runs; a=20
blank window is displayed
Questions 168-169 use the following class:
public class PetDog implements=20
Comparable =20
{ =
private String=20
myName, myBreed;
=
public=20
petDog(String name, String=20
breed) { =
myName=20
=3D name; myBreed =3D breed; }
=
public=20
String getName() { return myName;=20
} public =
String=20
getBreed() { return myBreed; }
=
public=20
boolean equals(Object=20
other) { =
return=20
other !=3D null && getName.equals(other.toString()); =
}
=
public int=20
compareTo(Object=20
other) =20
=
{ &n=
bsp; =20
return=20
=
getName().compareTo(other.toSstring()); =
=20
}
=
public int=20
hashCode() { return getName().hashCode(); }
=
public=20
String=20
=
toString() {=20
return getName() + "--" + + getBreed(); =
} =20
}
Suppose the following variables are declared and =
initialized:
PetDog honey =3D new PetDog("Honey", "Cocker =
Spaniel"); PetDog=20
lucie =3D new PetDog("Lucie", "Springer Spaniel"); PetDog =
murray =3D new=20
PetDog("Murray", "Golden =
Retriever");
168. (AB)
The (AB)
a Cocker Spaniel
Map map =3D new=20
HashMap(); =
map.put(honey.getName(),=20
honey); map.put(lucie.getName(),=20
lucie); map.put(murray.getName(),=20
murray);
=
System.out.println(honey.getNamer()=20
+ " is a " =
+ =20
< missing expression >=20
);
Which of the following can replace < missing =
expression =20
>?
I. =20
=
PetDog(map.get(honey.getName())II. =
((PetDog)map.get(honey)).getBreed()III. =
=
((PetDog)map.get(honey.getName())).getBreed()
I only=20
II only=20
I and II=20
II and III=20
I, II, and III =
169. (AB)
Which of the following code segments will compile =
with no=20
errors and produce an alphabetical list of all three pets:
Honey -- Cocker Spaniel Lucie -- Springer Spaniel =
Murray=20
-- Golden Retriever
I. Set set =3D new=20
TreeSet(); =
set.add(honey); =20
set.add(lucie); =20
set.add(murray); Iterator iter =3D=20
set.iterator(); while=20
(iter.hasNext()) =20
=
System.out.println(iter.next());II. =20
Map map =3D new TreeMap(); =
map.put(homey.getName(),=20
honey); map.put(lucie.getName(),=20
lucie); map.put(murray.getName(),=20
murray); Iterator iter =3D=20
map.values().iterator(); while=20
(iter.hasNext()) =20
System.out.println(iter.next());III. =20
Map map =3D new HashMap(); =20
map.put(homey.getName(), honey); =20
map.put(lucie.getName(), lucie); =20
map.put(murray.getName(), murray); =
Iterator iter =3D=20
map.keySet().iterator(); while=20
(iter.hasNext()) =20
=
System.out.println(map.get(iter.next()));
I only=20
II only=20
I and II=20
II and III=20
I, II, and III =
170. (AB)
head1 points to the first node of a =
linked list=20
that has three nodes with Integer values =
0,=20
1, 2; head2 points to the =
first=20
node of a list that has three nodes with Integer =
values=20
3, 4, 5. What is the output =
from=20
the following code?
ListNode node; if (head1 !=3D null && head2 =
!=3D=20
null) { node =3D=20
head1.getNext(); =20
head1.setNext(head2.getNext()); =20
head2.setNext(node); }
for (node =3D head1; node !=3D null; node =3D=20
node.getNext()) =20
System.out.println(node.getValue() + " "); for (node =3D =
head2; node !=3D=20
null; node =3D node.getNext()) =20
System.out.println(node.getValue() + " =
");
0 1 2 3 4 =
5=20
0 4 5 3 1 =
2=20
3 4 5 0 1 =
2=20
3 0 2 0 4 =
5=20
3 0 1 2 4 =
5=20
171. (AB)
What is the contents of the stack s1 =
(its=20
elements listed starting from the top) after the following code is =
executed?
Stack stk =3D new ArrayStack(); Stack stk1 =3D new=20
ArrayStack(); Stack stk2 =3D new ArrayStack();
int n; Integer obj; for (n =3D 1; n <=3D 6;=20
n++) stk.push(new =
Integer(n));
while (!stk.isEmpty()) { =
obj =3D=20
(Integer stk.pop()); n =3D=20
obj.intValue(); if (N % 2 !=3D=20
0) =20
stk1.push(obj); =20
else =20
stk2.push(obj); } while=20
(!stk1.isEmpty()) =20
stk.push(stk1.pop()); while=20
(!stk2.isEmpty()) =20
stk.push(stk2.pop());
1, 2, 3, 4, 5, 6=20
6, 5, 4, 3, 2, 1=20
1, 3, 5, 2, 4, 6=20
2, 4, 6, 1, 3, 5=20
None of the above =
172. (AB)
Consider the following method:
public boolean someProperty(TreeNode=20
root) { return root !=3D null=20
=
&&  =
; =20
(root.getLeft() !=3D null && root.getRight() !=3D null=20
=
|| &=
nbsp; =20
someProperty(root.getLeft())=20
=
|| &=
nbsp; =20
someProperty(root.getRight())); }
This method returns true if and only if the tree =
pointed=20
to by root
is not empty.=20
is not empty and =
the root is=20
not a leaf.=20
is not empty and =
the root is=20
either a leaf or has two children.=20
has at least one =
node with two=20
children.=20
is a full tree. =
173. (AB)
The method max(TreeNode root) assumes a =
precondition that root points to a non-empty binary =
search=20
tree containing Comparable objects. max =
returns=20
the value from the tree's largest node. Which of the following =
three=20
versions of max return the correct answer when the=20
precondition is met?
I. public Object =
max(TreeNOde=20
root) =20
{ =
while=20
(root.getRight() !=3D=20
=
null) &nbs=
p; =20
root =3D=20
=
root.getRight();  =
;=20
return root.getValue(); =20
}II. public Object =
max(TreeNOde=20
root) =20
{ =
Object=20
maxValue =3D=20
=
root.getValue();  =
;=20
if (root.getRight() !=3D=20
null) =20
=
{ &n=
bsp; =20
Object temp =3D=20
=
max(root.getRight()); =
=20
if (((Comparable)temp).compareTo(maxValue) >=20
=
0) &=
nbsp; =20
maxValue =3D=20
temp; =20
} =
return=20
maxValue; =
}III. =20
public Object max(TreeNOde =
root) =20
{ =
Object=20
maxValue =3D root.getValue();
=
if=20
(root.getLeft() !=3D null=20
=
&&  =
; =20
=
((Comparable)max(root.getLeft())).  =
; =
=20
compareTo(maxValue) >=20
=
0) &=
nbsp; =20
maxValue =3D max(root.getLeft());
=
if=20
(root.getRight() !=3D null=20
=
&&  =
; =20
=
((Comparable)max(root.getRight())). &nbs=
p;  =
; =20
compareTo(maxValue) >=20
=
0) &=
nbsp; =20
maxValue =3D max(root.getRight());
=
return=20
maxValue; =
}
I only=20
II only=20
I and II=20
II and III=20
I, II, and III =
174.
Given
int[] a =3D {1, 3, 5, 7, 9, 11, 13}; =
what are the values in a after =
disarray(int[] a, int=20
n) is called? The method disarray is defined =
as=20
follows:
public void disarray(int[] a, int=20
n) { if (n >=20
1) =20
{ =
disarray(a,=20
n-1); =
a[n-1]=20
+=3D a[n-2]; } }=20
1, 4, 9, 16, 25, =
36, 49=20
1, 4, 8, 12, 16, =
20, 24=20
1, 8, 12, 16, 20, =
24, 13=20
1, 24, 20, 16, =
12, 8, 4=20
None of the above =
175.
The method mixup is defined as follows:
String mixup(String =
word) { if=20
(word.length() =3D=3D=20
1) =
return=20
""; =20
else =
return=20
mixup(word.substring(0, word.length() -=20
=
1)) =
&=
nbsp;=20
+ word.charAt(word.length() - 1); }
What is the value of the string returned by=20
mixup("IDEAL")?
IDEAL=20
IDEA=20
LEAD=20
LEDA=20
DEAL =