Section I
Assume that an array of integer values has been declared as follows and has been initialized.
int[] arr = new int[103;
Which of the following code segments correctly interchanges the value of arr[0] and arr[5] ?
(A) arr[0] = 5;
arr[5] = 0;
(B) arr[0] = arr[5];
arr[5] = arr[0];
(C) int k = arr[5];
arr[0] = arr[5];
arr [5] = k;
(D) int k = arr [0];
arr[0] = arr[5];
arr[5] = k;
(E) int k = arr[5];
arr [5] = arr[0];
arr[0] = arr[5];
nauthorized copying or reuse of
y part of this page is illegal.
GO ON TO THE NEXT PAGE. 21
SectionI
4. Consider the following code segment.
ArrayList<String> items = new ArrayList<String>();
[Link]("A");
[Link]{"B");
[Link]("C");
[Link](0, "D");
[Link] (3);
[Link](0, "E");
[Link](items);
What is printed as a result of executing the code segment?
(A) [A, B, C, E]
(B) [A, B D, E
(C) [E D, A, B]
(D) [E, D. A, C
(E) [E, D, C, B]
5. When designing a class hierarchy, which of the following should be true of a superclass?
(A) A superclass should contain the data and functionality that are common to all subclasses that inherit from
the superclass.
(B) A superclass should be the largest, most complex class from which all other subclasses are derived.
(C) A superclass should contain the data and functionality that are only required for the most complex class.
(D) A superclass should have public data in order to provide access for the entire class hierarchy.
(E) A superclass should contain the most specific details of the class hierarchy.
Unauthorized copying or reuse of
22 any part of this page is Illegal.
GO ON TO THE NEXT PAGE.
Section I
8. Consider the following instance variable and incomplete method. The method calcTotal is intended to
return the sum of ali valuesin vals.
private int(] vals;
public int calcTotal()
int total = 0:
/* missing code */
return total;
}
Which of the code segments shown below can be used to replace /* missing code */ so that
calcTotal will work as intended?
I. for (int pos = 0; pos < [Link]; pos++)
total += vals[pos];
II. for (int pos = [Link]; pos > 0; pos--)
{
total += vals [pos];
}
III. int pos = 0;
while (pos < [Link])
{
total += vals[pos];
pos++;
}
(A) I only
(B) II only
(C) III only
(D) I and III
(E) II and III
Unauthorized copylng or reuse of
24 any part of this page is illegal.
GO ON TO THE NEXT PAG
Section I
1. Consider the following method that is intended to modify its parameter nameList by replacing all
occurrences of name with newValue.
public void replace (ArrayList<String> nameList,
String name, String newValue)
{
for (int j = 0; j < [Link](); j++)
{
if ( /* expression */ )
{
[Link](j, newValue);
}
}
}
Which of the following can be used to replace /* expression */ so that replace will work
as intended?
(A) [Link](j).equals (name)
(B) [Link](j) == name
(C) [Link](j)
(D) nameList[j] == name
(E) nameList[j].equals(name)
authorized copying or reuse of
y part of this page is illegal.
GO ON TO THE NEXT PAGE. 2.7
Section I
25. A DancingCritter isa Critter that moves in the following manner. The DancingCritter
makes a left turn if at least one of its neighbors is another DancingCritter. It then moves like a
Critter. If none DancingCritter objects, it moves like a Critter without
of its neighbors are
making a DancingCritter acts like a Critter by eating neighbors that
left turn. In all other respects, a
are not rocks or critters. Consider the following implementations.
1. public class DancingCritter extends Critter
public ArrayList<Actor> getActors()
{
ArrayList<Actor> actors new ArrayList<Actor>(); =
for (Actor a : getGrid().getNeighbors (getLocation()))
{
if (a instanceof DancingCritter)
[Link](a);
}
return actors;
}
public void processActors(ArrayList<Actor> actors)
if ([Link]() > 0)
{
setDirection(getDirection() + [Link]);
}
[Link](actors):
II. public class DancingCritter extends Critter
{
public void processActors (ArrayList<Actor> actors)
boolean turning = false;
for (Actor a : actors)
{
if (a instanceof DancingCritter)
turning = true;
}
if (turning)
{
setDirection(getDirection() + [Link]);
}
Unauthorized copying or reuse of
copying or r
42 any part of this page is illegal.
GO ON TO THE NEXT PA(
Section I
1. public class DancingCritter extends Critter
{
public void makeMove (Location loc)
{
boolean turning = false;
for (Actor a : getGrid().getNeighbors (getLocation()))
{
if (a instanceof DancingCritter)
turning = true;
}
if (turning)
{
setDirection (getDirection() + [Link]);
}
[Link] (loc);
}
}
hich of the proposed implementations will correctly implement the DancingCritter class?
A) I only
3) II only
) III only
>) I and II only
3) I, II, and III
uthorized copying or reuse of
part of this page is illegal.
GO ON TO THF NEXT PAGE 43
Section I
. Consider the following method. Method allEven is intended to return true if all elements inarray arr
are even numbers; otherwise, it should returm false.
public boolean allEven(int[1 arr)
{
boolean isEven = /* expression */ ;
for (int k = 0; k < [Link]; k++)
{
/* loop body *1
}
return isEven:
}
Which of the following replacements for /* expression */ and /* loop body */ should be used
so that method allEven will work as intended?
/* expression * / /* loop body 7
(A) false if {(arr [k] % 2) == 0)
isEven = true;
(B) false if ((arr[k] % 2) ! = 0)
isEven = false;
else
isEven = true;
(C) true if ((arr [k] % 2) != 0)
isEven = false;
(D) true if ((arr[k] % 2) ! = 0)
isEven = false;
else
isEven = true;
(E) true if ((arr[k] % 2) == 0)
isEven = false;
else
isEven = true;
uthorized copying or reuse of
part of this page is illegal.
GO ON TO THE NEXT PAGE. 45
Section I
34. Consider the following incomplete method. Method findNext is intended to return the index of the first
occurrence of the value val beyond the position start in array arr.
// returns index of first occurrence of val in arr
// after position start;
// returns [Link] if val is not found
public int findNext(int[] arr, int val, int start)
{
int pos = start + 1;
while ( /* condition */ )
pos++;
return pos;
}
For example, consider the following code segment.
int[] arr = (11, 22, 100, 33, 100, 11, 44, 100};
[Link] (findNext(arr, 100, 2));
The execution of the code segment should result in the value 4 being printed.
Which of the following expressions could be used to replace /* condition */ sothat findNext will
work as intended?
(A) (pos < [Link]) && (arr[pos] != val)
(B) (arr[pos] != val) && (pos < [Link])
(C) (pos < [Link]) || (arr[pos] != val)
(D) (arr[pos] == val) && (pos < [Link])
|1
(E) (pos < [Link]) (arr[pos] == val)
Unauthorized copying or reuse of
Lany nard ofen copying of reuse
any part of this page is illegal.
50 GO ON TO THE NEXT PAGI
Section I
36. Consider the following two methods that appear within a single class.
public void changeIt(int [] list, int num)
{
list = new int [5];
num = 0;
for (int x = 0; x < [Link]; x++)
list[x] = 0;
}
public void start()
{
int[] nums = {1, 2, 3, 4, 5};
int value = 6;
changeIt(nums, value);
for (int k = 0; k < [Link]; k++)
[Link](nums [k] + " ");
[Link](value);
}
What is printed as a result of the call start() ?
(A) 0 00000
(B) 00000 6
(C) 1 2 3 4 5 6
(D) 1 2 3 4 5 0
(E) changeIt will throw an exception.
Unauthorized copying or reuse of
any part of this page is illegal.
52 GO ON TO THE NEXT PAGE
Section I
7. Consider the following declaration of the class NumSequence, which has a constructor that is intended to
initialize the instance variable seq to an ArrayList of numberOfValues random floating-point
values in the range [0.0, 1.0).
public class NumSequence
{
private ArrayList<Double> seq;
// precondition: numberOfValues > 0
// postcondition: seq has been initialized to an ArrayList of
// length numberOfValues; each element of seq
// contains a random Double in the range [0.0, 1.0)
public NumSequence(int numberofValues)
{
/* missing code */
}
}
Which of the following code segments could be used to replace /* missing code */ so that the
constructor will work as intended?
I. ArrayList<Double> seq = new ArrayList<Double>();
for (int k = 0; k< numberOfValues; k++)
[Link](new Double ([Link]()));
II. seq = new ArrayList<Double>() ;
for (int k = 0; k < numberOfValues; k++)
[Link] (new Double ([Link]()));
III. ArrayList<Double> temp = new ArrayList<Double>();
for (int k = 0; k < numberOfValues; k++)
[Link](new Double ([Link]() ));
seq = temp;
(A) II only
(B) III only
(C) I and II
(D) I and III
(E) II and III
authorized copying or reuse of
/ part of this page is illegal.
GO ON TO THE NEXT PAGE. 53