Sunteți pe pagina 1din 23

A1 (A1.3). Consider the following data field and method.

private double[] list;


public int getIt(double value)
{
int k = 0;
while(k < list.length && list[k] != value)
k++;

if(k < list.length)


return k;
else
return -1;

Which of the following best describes what is returned by this method?


A.
B.
C.
D.
E.

The number of times that value occurs in list, or -1 if all items in the list equal value.
The number of items in list that are not equal to value, or -1 if value is not in list.
The index of the first occurrence of value in list, or -1 if value is not in list
The index of the last occurrence of value in list, or -1 if value is not in list
The index of the first item in list that is not equal to value, or -1 if value is not in list

A2 (A2.3). Consider the following declaration


public interface Landmark
{
// returns the distance from home
double distanceFromHome();
}

Assume the following data field and method are declared in another class.
private Landmark[] map;
public Landmark getIt()
{
int index = 0;

for(k = 1; k < map.length; k++)


{
if(map[k].distanceFromHome() > map[index].distanceFromHome())
index = k;
}
return map[index];

Which of the following best describes what is returned from a call to getIt?
A.
B.
C.
D.
E.

The item in map with the smallest distance from home.


The index of the item in map with the smallest distance from home.
The item in map with the greatest distance from home.
The index of the item in map with the greatest distance from home.
The first item in map.

A3 (A1.31). Consider the following declaration


public interface Landmark
{
// returns the distance from home
double distanceFromHome()
}

Assume the following data field and method are declared in another class.
private ArrayList<Landmark> map;
public Landmark getNextClosest(Landmark marker)
{
Landmark item = marker;

for(Landmark current: map)


{
if(current.distanceFromHome() > marker.distanceFromHome() &&
current.distanceFromHome() < item.distanceFromHome())
item = current;
}
return item;

Assume that tower is a Landmark. The call to the method, getNextClosest(tower), is intended to return the item
from ArrayList, map that has the smallest distance from home, among those items that have a distance from home that
is greater than the distance from home for tower, or tower itself if there is no such item. What does call
getNextClosest(tower) actually return?
A.
B.
C.
D.
E.

The call always works as intended.


The call always returns the item in map that has the smallest distance from home.
The call always returns the item in map that has the largest distance from home.
The call always returns the last item in map.
The call always returns tower.

A4 (none). Consider the following declaration


public interface Landmark
{
// returns the distance from home
double distanceFromHome();
}

Assume the following data field and method are declared in another class, with line numbers given for reference.
private Landmark[] map;
public Landmark getNextClosest(Landmark marker)
{
1 Landmark item = marker;
2 index = 0;
while(index < map.length)
{
3
if(map[index].distanceFromHome() > marker.distanceFromHome() &&
4
map[index].distanceFromHome() < item.distanceFromHome())
item = map[index];
index++;
}
return item;
}

The method getNextClosest is intended to return the item from map that has the smallest distance from home among
those items that have a distance from home that is greater than the distance from home for marker, or marker itself if
there is no such item. However, getNextClosest may not always work as intended. Which of the following best
describes the error, if any, in the definition of this method?
A.
B.

The method always works as intended.


Line 1 should be changed to the following:
Landmark item = map[0];

C.

The line 2 should be replaced with the following code segment:

D.
E.

The "&&" in line 3 should be replaced with "||"


The "<" in line 4 should be replaced by "<="

int index = 0;
while(index < map.length &&
map[index].distanceFromHome() <= marker.distanceFromHome())
index++;
if(index < map.length)
item = map[index];

A5 (A1.36). Consider the following declaration


public interface HasHeight
{
// returns the height of an item
double height();
}

Assume the following data field and method are declared in another class.
private HasHeight[] list;
public void inSort()
{
for(int start = 1; start < list.length; start++)
{
HasHeight temp = list[start];
int index = start;
while(/* condition */)
{
/* statement */
index--;
}
list[index] = temp;
}
}

The method inSort is intended to sort list into increasing order by height using an insertion sort. Which of the following
replacements for /* condition */ and /* statement */ will make inSort work as intended?
condition

statement

A.

index >= 0 && temp.height() < list[index].height()


list[index+1] = list[index]

B.

index >= 0 && temp.height() < list[index].height()


list[index] = list[index-1]

C.

index >= 0 && temp.height() < list[index-1].height()


list[index] = list[index-1]

D.

index > 0 && temp.height() < list[index-1].height()


list[index+1] = list[index]

E. index > 0 && temp.height() < list[index-1].height()


list[index] = list[index-1]

A6 (A2.31). The following method sorts the Comparable array parameter from largest to smallest.
public static void sort(Comparable[] list)
{
for(int start = 0; start < list.length-1; start++)
{
int index = start;
for(int k = start + 1; k < list.length; k++)
{
if(list[k].compareTo(list[index]) > 0)
index = k;
}
Comparable temp = list[start];
list[start] = list[index];
list[index] = temp;
}
}

Assume the String array words is initialized as shown below.


word

Which of the following best represents the array word after the third pass through the outer loop in the call
sort(word)?
A.
B.
C.
D.
E.

A7 (A1.29). Consider the following data field and method.


private ArrayList<String> list;
public void change()
{
for(int k = 0; k < list.size(); k++)
{
if(list.get(k).equals(list.get(k+1)))
list.remove(k+1);
}
}

Assume that list is initialized as shown below.


list

Which of the following shows list after a call to method change?


A.
B.
C.
D.
E.

A8 (A2.35). Consider the following data field and method.


private String[] list;
public void change()
{
for(int k = 0; k < list.length; k++)
{
if(list[k].equals(list[k+1]))
list[k+1] = "";
}
}

Assume that list is initialized as shown below.


list

Which of the following shows list after a call to method change?


A.
B.
C.
D.
E.

A9 (A1.39). Consider the partial method definition.


// len > 3
public void printTriangle(int len)
{
/* method body */
}

This method is intended to print a hollow triangle as diagrammed below, where the number of stars along each side is
given by the parameter len, where len > 3. The result of the call printTriangle(6) would be this diagram.
*
**
* *
* *
*
*
******

Which of the following replacements for /* method body */ would make the method printBox work as intended?
A.

for(row = 0; row < len; row++)


{
for(k = 0; k < row+1; k++)
System.out.print("*");
System.out.println();
}

B.

System.out.print("*");
for(row = 0; row < len; row++)
{
System.out.print("*");
for(k = 0; k < row; k++)
System.out.print(" ");
System.out.println("*");
}
for(star = 0; star < len; star++)
System.out.print("*");

C.

System.out.print("*");
for(row = 1; row < len-1; row++)
{
System.out.print("*");
for(k = 0; k < row; k++)
System.out.print(" ");
System.out.println("*");
}
for(star = 0; star < len; star++)
System.out.print("*");

D.

System.out.print("*");
for(row = 1; row < len-1; row++)
{
System.out.print("*");
for(k = 0; k < row-1; k++)
System.out.print(" ");
System.out.println("*");
}
for(star = 0; star < len; star++)

System.out.print("*");
E.

System.out.print("*");
for(row = 1; row < len-1; row++)
{
System.out.print("*");
for(k = 0; k < row-2; k++)
System.out.print(" ");
System.out.println("*");
}
for(star = 0; star < len; star++)
System.out.print("*");

A10 (). Consider the partial method definition.


// len > 2
public void printBox(int len)
{
/* method body */
}

This method is intended to print a hollow box as diagrammed below, where the number of stars along each side is given
by the parameter len, where len > 2. The result of the call printBox(6) would be this diagram.
******
*
*
*
*
*
*
*
*
******

Which of the following replacements for /* method body */ would make the method printBox work as intended?
A.

for(row = 0; row < len; row++)


{
for(k = 0; k < len; k++)
System.out.print("*");
System.out.println();
}

B.

for(star = 0; star < len; star++)


System.out.print("*");
for(row = 0; row < len; row++)
{
for(k = 0; k < len; k++)
System.out.print(" ");
System.out.println();
}
for(star = 0; star < len; star++)
System.out.print("*");

C.

for(star = 0; star < len; star++)


System.out.print("*");
for(row = 0; row < len; row++)
{
System.out.print("*");
for(k = 0; k < len; k++)
System.out.print(" ");
System.out.println("*");
}
for(star = 0; star < len; star++)
System.out.print("*");

D.

for(star = 0; star < len; star++)


System.out.print("*");
for(row = 1; row < len-1; row++)
{
System.out.print("*");
for(k = 0; k < len; k++)
System.out.print(" ");
System.out.println("*");
}
for(star = 0; star < len; star++)

System.out.print("*");
E.

for(star = 0; star < len; star++)


System.out.print("*");
for(row = 1; row < len-1; row++)
{
System.out.print("*");
for(k = 1; k < len-1; k++)
System.out.print(" ");
System.out.println("*");
}
for(star = 0; star < len; star++)
System.out.print("*");

A11 (A1.40). Consider the following data field and method.


private int[] valList;
public void changeList()
{
int currentLoc = 0;
int nonZeroLoc = 0;
while(nonZeroLoc < valList.length && valList[nonZeroLoc] == 0)
nonZeroLoc++;
while(nonZeroLoc < valList.length)
{
valList[currentLoc] = valList[nonZeroLoc];
valList[nonZeroLoc] = 0;
currentLoc++;
nonZeroLoc++;

while(nonZeroLoc < valList.length && valList[nonZeroLoc] == 0)


nonZeroLoc++;

Assume that valList is initially the following array.

Which of the following best represents valList after the call changeList()?
A.
B.
C.
D.
E.

The call will result in an ArrayIndexOutOfBoundsException.

A12 (A2.40). Consider the following data field and method.


private ArrayList<String> list;

// contains Strings or nulls

public void changeList()


{
int currentLoc = 0;
int nonNullLoc = 0;
while(currentLoc < list.size() && list.get(currentLoc) != null)
{
currentLoc ++;
nonNullLoc++;
}
while(nonNullLoc < list.size() && list.get(nonNullLoc) == null)
nonNullLoc++;

while(nonNullLoc < list.size())


{
list.set(currentLoc, list.get(nonNullLoc));
list.set(nonNullLoc, null);
currentLoc++;
while(nonNullLoc < list.size() && list.get(nonNullLoc) == null)
nonNullLoc ++;
}

Assume that list is initially the following array.

Which of the following best represents list after the call changeList()?
A.
B.
C.
D.
E.

A13 (A3.3) Consider the following instance variable and method.


private String[] nameList;
public int getIt(String name)
{
int val = 0;

for(String fromList: nameList)


{
if(fromList.equals(name))
val++;
}
return val;

Assume that nameList has been appropriately initialized. Which of the following best describes what, if anything, is
returned by the call getIt("Dennis")?
A. The index of the first occurrence of "Dennis" in nameList.
B. The index of the last occurrence of "Dennis" in nameList.
C. The number of times "Dennis" occurs in nameList.
D. The number of items not equal to "Dennis" in nameList.
E. Nothing because the code is incorrect and will not compile.

A14 (A3.31). Consider the following declaration


public interface HasHeight
{
// returns the height of an item
double height()
}

Assume the following data field and method are declared in another class.
private HasHeight[] list;
public HasHeight getIt(HasHeight hh)
{
HasHeight current;
int k = 0;
while(k < list.length && list[k].height() >= hh.height())
k++;
if(k == list.length)
return hh;
else
current = list[k];

while(k < list.length)


{
if(list[k].height() < hh.height() && list[k].height() > current.height())
current = list[k];
k++;
}
return current;

Assume that bobby is an instance of a class that implements HasHeight. Which of the following best describes the item
returned by the call getIt(bobby)?
A. bobby is always returned.
B. The tallest item in list among those that are taller than bobby is returned; if there is no such item, then bobby is

returned.
C. The shortest item in list among those that are taller than bobby is returned; if there is no such item, then bobby
is returned.
D. The tallest item in list among those that are shorter than bobby is returned; if there is no such item, then bobby
is returned.
E. The shortest item in list among those that are shorter than bobby is returned; if there is no such item, then
bobby is returned.

A15 (A3.36). The following method sorts an array of doubles.


public void sort(double[] list)
{
for(int start = 1; start < list.length; start++)
{
double temp = list[start];
int k = start;
while(k > 0 && list[k-1] > temp)
{
list[k] = list[k-1];
k--;
}
list[k] = temp;
}
}

Assume the String array values is initialized as shown below.


values

Which of the following best represents the array values after the fourth pass through the outer loop in the call
sort(values)?
A.
B.
C.
D.
E.

A16 (A3.29) Consider the following data field and method.


private ArrayList<String> list;
public void change()
{
String str = list.get(list.size() 1);
for(int k = list.size() - 1; k > 0; k--)
list.set(k, list.get(k-1));
}

list.set(0, str);

Consider following replacements for the entire body of method change.


I.

list.add(list.remove(0));

II.

list.add(0, list.remove(list.size() 1));

III.

String str = list.get(0);


for(int k = 1; k < list.size() 1; k++)
list.set(k, list.get(k+1));
list.set(list.size() -1, str);

Which of these replacements would result in an equivalent method?


A.
B.
C.
D.
E.

I only
II only
III only
I and III
II and III

A17 (A3.39) Consider the following method.


// precondition: length > 3
public void printSomething(int length)
{
for(int k = 0; k < length-1; k++)
System.out.print(" ");
System.out.println("*");
for(int row = 1; row < length 1; row++)
{
for(int k = 0; k < length 1 - row; k++)
System.out.print(" ");
System.out.println("*");

for(int k = 0; k < row 1; k++)


System.out.print(" ");
System.out.println("*");

for(int star = 0; star < length; star++)


System.out.print("*");

Which of the following best represents what is printed by the call printSomething(6)?
A. *
**
* *
* *
*
*
******

B.

*
**
* *
* *
*
*
******

C. *
**
***
****
*****
******

D.

*
**
***
****
*****
******

E. ******
* *
**
**
* *
******

A18 (A3.40). Consider the following data field and partially defined method.
private ArrayList<String> people;
public void removeDups(String target)
{ // code not shown }

The method removeDups is intended to remove all instances of target that occur in people except the first, leaving the
first instance in the same relative position. Which of the following correctly implements the method removeDups?
A.

for(int k = 0; k < people.size(); k++)


{
if(people.get(k).equals(target))
people.remove(k);
}

B.

int k = 0;
while(k < people.size())
{
if(people.get(k).equals(target))
people.remove(k);
else
k++;
}

C.

int k = 0;
while(k < people.size() && !people.get(k).equals(target))
k++;
for(int j = k+1; j < people.size(); j++)
{
if(people.get(k).equals(target))
people.remove(k);
}

D.

int k = 0;
while(k < people.size() && !people.get(k).equals(target))
k++;
while(k < people.size())
{
if(people.get(k).equals(target))
people.remove(k);
else
k++;
}

E.

int k = 0;
while(k < people.size() && !people.get(k).equals(target))
k++;
k++;
while(k < people.size())
{
if(people.get(k).equals(target))
people.remove(k);
else
k++;
}

A19 (A3.16). Consider the following declarations.


public class Book
{
private String myTitle;
private int myNumPages;

public int numPages()


{
return myNumPages;
}
// constructors and other methods not shown

ArrayList<Book> readingList;

Assume that readingList has had several books added to it. Consider the following code segments.
I.

int sum = 0;
for(Book bk: readingList)
sum += bk.numPages();

II.

int sum = 0;
for(int k = 0; k < readingList.size(); k++)
sum += readingList.get(k).numPages();

III.

int sum = 0;
int k = 0;
while(k < readingList.size())
{
sum += readingList.get(k).numPages();
k++;
}

Which of these code segments correctly calculates sum to be the total number of pages of books in readingList?
A. II only
B. I and II only
C. I and III only
D. II and III only
E. I, II and III

A20 (A3.32). Consider the incomplete method ChoiceOfThree.


public int choiceOfThree()
{
// missing code
}

Method choiceOfThree is intended to return one of the values 1, 2, 3, each with probability 1/3. Consider the following
replacements for //missing code.
I.
II.
III.

return (int)(Math.random() * 3);


return (int)(Math.random() * 3) + 1;
if(Math.random() < 1.0/3.0)
return 1;
else if (Math.random() < 2.0/3.0)
return 2;
else
return 3;

Which if these replacements for //missing code will make choiceOfThree work as intended?
A. I only

B. II only

C. III only

D. I and III

E. II and III

A21 (A1.16). Consider the incomplete method aceCount(int numRolls).


public int aceCount (int numFlips)
{
int count = 0;
// missing code
return count;
}

Method aceCount(int numRolls) is intended to return the number of "ones" from numRolls rolls of a six-sided die,
with each value 1, 2, 3, 4, 5, 6 equally likely. Consider the following replacements for // missing code.
I

while(count <= numRolls && Math.random() < 1/6)


count++;

II

for(int roll = 0; roll < numRolls; roll++)


{
if(Math.random() < 1/6)
count++;
}

III

for(int roll = 0; roll < numRolls; roll++)


{
if((int)(6 * Math.random() + 1) == 1)
count++;
}

Which of these replacements for // missing code will work as intended?


A. I only

B. II only

C. I and II only

D. II and III only

E. I, II and III

A22 (A2.34) Consider the following incomplete method.


// precondition: 0.0 <= prob <= 1.0
public boolean chance(double prob)
{
// stetement
}

The method chance is intended to return true with a probability prob and false with a probability 1.0 prob. Which
of the following replacements for // statement will make the method chance work as intended?
A.
B.
C.
D.
E.

return
return
return
return
return

prob < Math.random();


Math.random() < prob;
prob < (1 Math.random());
(1 / prob) < Math.random();
Math.random() < (1 / prob);

A23. (A2.17)
Consider the following method.
public List<String> doSomething(List<String> names)
{
List<String> result = new ArrayList<String>();
for(String name: names)
{
if(name.substring(0,1).equals(B)
result.add(name);
}
return result;
}

Assume List<String> nameList has been declared and initialized with a list of names. Which of the following best
describes what happens when this method is compiled and the call doSomething(nameList) is made?
A. The compile fails
B. A copy of nameList is returned.
C. A List<String> containing all the names from nameList that begin with B is returned.
D. A List<String> containing all the names from nameList that contain a B is returned.
E. An empty List<String> is returned.

A24. (A1.9) Consider the following data field and method.


private List<String> list; // contains String objects or null
// precondition: list.size() > 0
public int mystery()
{
int k = 0;
while(k < list.size() && list.get(k) != null)
k++;
return k;
}

Assume that the List<String> list may contain null references as well as String objects. Which of the following
best describes the value returned by the call mystery()?
A. The index of the first occurrence of null in list, or list.size() if null does not occur.
B. The index of the last occurrence of null in list, or list.size() if null does not occur.
C. The number of occurrences of null in list.
D. The value list.size() is always returned.
E. No value is returned because an exception is thrown when list.get(k) != null is evaluated.
A25 (A2.29)
Consider the following partial method.
public int findMin(List<Integer> values)
{
int min = /* expression */;

for(Integer val: values)


{
if( /* condition */ )
min = val.intValue();
}
return min;

The method findMin is intended to return the minimum value stored in values. Which of the following replacements
for /* expression */ and /* condition */ will make findMin work as intended?

A.
B.
C.
D.
E.

/* expression */

/* condition */

Integer.MIN_VALUE
Integer.MIN_VALUE
Integer.MAX_VALUE
Integer.MAX_VALUE
values.get(0).intValue()

val.intValue()
val.intValue()
val.intValue()
val.intValue()
val.intValue()

>
<
>
<
>

min
min
min
min
min

S-ar putea să vă placă și