COMPUTER SCIENCE A
oO eee
Number of questions—40
Percent of total score—50
Directions: Determine the answer to each of the following questions or incomplete statements, using the available
space for any necessary scratch work, Then decide which isthe best of the choices given and fill in the corresponding
circle on the answer sheet. No credit will be given for anything written in the examination booklet. Do not spend too
much time on any one problem.
Notes:
© Assume that the classes listed in the Java Quick Reference have been imported where appropriate,
‘* Assume that declarations of variables and methods appear within the context of an enclosing class
‘+ Assume that method calls that are not prefixed with an object or class name and are not shown within a complete
class definition appear within the context of an enclosing class.
+ Unless otherwise noted in the question, assume that parameters in method calls are not nu11_ and that
‘methods are called only when their preconditions are satisfied.
1. Consider the following incomplete method, which is intended to retuen the number of integers that evenly divide
the integer imputVal. Assume that Val is grealer than 0,
public static int numDivisors(int inputval)
¢
int count = 0
for (int k = 1; k <= inputval; k++)
{
if ( /* condition */ }
{
count++;
y
urn count;
i
Which of the following can be used to replace /* condition */ so that numDivisors will work as
intended?
(A) inputvel &
B) ki
(©) inputval % k 1=
(D) inputval /
©) k / inputval > 0
putVal
[Gravina copying ac rou a]
[any part ofthis eae toga
GO ON TO THE NEXT PAGE.2. Consider the following code segment.
for! (int r= 3; ¥> 0; r.
om z
int ¢;
for (c = 1; ¢ $a; cH)
{
System. out.peint ("
d
for (ec
Vi
ri €.<= 3; ote)
4
System. out.print("*")
d
System. out .printin();
}
‘What is printed as a result of executing the code segment?
(A)
B)
©
©)
®
rautoraed spying oe reuse i]
Sry art of tue page Megs
GO ON TO THE NEXT PAGE.3. Consider the following two classes.
public class a
rene void show()
‘ System. out.print(*A");
, d
public class B extends A
d
public yoia show()
a
system.or
print (*BY);
3
?
‘Whats printed as a result of executing te following code segment?
peer
ce} naan?
we
@e
© a8
ea
(E) The code results in a runtime error.
(Ghauthorzed sping ox rae of
any bat ofthis page tings
GO ON TO THE NEXT PAGE,private int[] arr;
/** Precondition: arr. length >0
* Greturn the largest value in array arr
+
public int finaMax()
c
int maxval
oF
for (int val : arr)
{
(val > maxval)
maxval = val;
)
d
return maxval;
)
4. Consider the following instance variable and method.
Method €£ndttaic js intended to etur the largest value in the array arr. Which of the following best
describes the concitions under which the method findtiaxc will not work ss intended’?
(A) The largest value in arr occurs only once and isin arr [0).
(B) Phe largest value in ary. occurs only once andiis in axr[arr-length - 1}.
(C) The largest value in axr is negative
(D) The largest value in. arr is zero.
(E) The largest value in arr occurs more than once.
e
any part ofthis page fs itopat
GO ON TO THE NEXT PAGE.5. Assume that x and y are boolean variables and have been properly initialized.
(x [| y) sex
‘Which of the following always evaluates to the same value as the expression above?
(A) x
By
© x ee y
@ x Il y
®xitzy
6. Consider the following method, which is intended to return true if at least one of che three strings s1, 82,
or 3 contains the substring "art". Otherwise, the method should return false
public static boolean containsAr
(string sl, string s2, String s3)
return (all.indexof(tart") 1= -1);
i
Which of the following method calls demonstrates that the method does not work as intended?
(A) containsart ("rattrap", "similar", ‘today")
(B) containsart("start", "article", "Bart*)
(C) containsart (*harm", "chortle", "crowbar")
(D) containsArt ("matriculate*, “carat', "arbitrary")
(B) containsArt ("darkroon', ‘cartoon", *articulate")
Fonauoried sapyn
Sry par of rs page,
GO ON TO THE NEXT PAGE.7. Consider the following code segment.
for (int ‘outer = 1) outer <= 6; outer++)
4 for (int inner = outer; inner <= 6; inner++)
i if (inner & 2 oO)
: System.out.print(inner +" ");
; +
System. out .printin();
)
‘What will be printed as a result of executing the code segment”,
A 2 46
46
6
®) 2 46
a 416
24 6
O24 6
a tee.
46
4 6
6
6
@) 2 4 6
Beas
24 6
ards:
24 6
Ble
®24
er
ry
4
[Ueawtorasd coping
any prt ote ee
GO ON TO THE NEXT PAGE.8. Consider the following method.
public static int[] operation(int{][] matrix, int r, int c)
c
int{] result = new int{matrix. length];
for (int j = 0; j < matrix.length ; j++) -
4
result(j] = matrix[x] [J] * matrix[J][¢l:
5)
return result;
)
The following code segment appears in another method in the same class,
imt{][] mat = ({3, 2, 1, 4},
oka Sth
enaeehe2h
fay pce i
int [] arr = operation(mat, 1, 2);
Which of the following represents the contents of arr as a result of executing the code segment?
(A) (6, 4, 2, 43
(B) (1, 6, 3, 43
(CO) nea cea
@) (4, 4, 2, 23
(E}) ae 2 dy
[Unauthorised spying wr rane oT
‘sry part of mie page haga
GO ON TO THE NEXT PAGE.
-10-9. A pair of number cubes is used in a game of chance. Each number cube has six sides, numbered from 1 t0 6,
inclusive, and there is an equal probability for each of the nambers to appear on the top side (indicating the
Cube’s value) when the number cube is rolled, The following incomplete statement appears in a program that
computes the sum of the values produced by rolling two number cubes.
int sum = /* missing code */ ;
Which of the following replacements for /* missing code */ would best simulate the value produced as a
result of rolling two number cubes?
(A) 2 * (dnt) (Qtath.random() * 6)
(8) 2 * (int) (Math.zandom() * 7)
(©) (int) (ath.random() * 6) + (int) (wath.random() * 6)
@) (int) (Math. ranéom() * 13)
©) 2+ (int). (Math.random() * 6) + (int) (Math.random(} * 6)
any pat of ts poe
GO ON TO THE NEXT PAGE,
Ate10. Consider the following interface and class declarations.
public interface student
(© /* implementation not shown */ —}
public class Athlete
( /* implementation not shown */° 3
public class TennisPlayer extends Athlete implements student
(© /* implementation not shown */ —}
‘Assume that each class has a zero-parameter constructor. Which of the following is NOT a valid declaration?
(A) Student a = new TennisPlayer(};
(B) TennisPlayer b = new TennisPlayer();
(© athlete ¢
(D) student 4
© Athlete e
1
any ar ts page toga
new TennisPlayer();
new Athlete (};
new Athlete (};
126
GO ON TO THE NEXT PAGE,11, Consider the following method,
public static boolean mystery(String str)
€
String temp = "";
for (int k-= str.length(); k > 0; k--)
4
temp = temp + str.cubstring(k - 1, k);
i
return temp.equals(str);
d
Which of the following calls to mystery willretum true?
(A) mystery ("not)
(8) mystery ("on")
(©) mystery ("nnoo")
©) mystery ("nono")
©) mystery("noon")
GO ON TO THE NEXT PAGE.12. Assume that x and y are boolean variables and have been properly initialized.
(x e& y) && I(x | y)
‘Which of the following best describes the result of evaluating the expression above?
(a)
(B)
©*
©)
©
any part
area copra
true always
false always
true only when x is true and y is true
true only when x and y have the same value
true only when x and y have different values
aa
nega
Aa
GO ON TO THE NEXT PAGE.13. Consider the following instance variable and method,
private int{] numbers;
public void mystery(int x)
{
for (int k
{
numbers[k] = numbers{k - 1] + x;
7
a}
k < numbers.length; k = k + x)
Assume that numbers has been initialized with the following values.
417, 34,21, 42, 15, 69, 48, 25, 39)
‘Which of the following represents the ordcr of the values in numbers as‘aresult of the call mystery(3) ?
(A) 7, 20, 21, 42, 45, 69, 48, 51, 397
@) (27, 20,.23,, 26, 29, 32, 35, 38, 41)
(©) (17, 37, 21, 42, 18, 68, 48, 28, 39)
(D) (20, 23, 21, 42, 45, 68, 51, 54, 39)
©) (20, 34, 21, 45, 15, 69, 51, 25, 39)
GO ON TO THE NEXT PAGE.
3 15.14. Consider the following method, biggest, which is intended to return the greatest of three integers: Tt does
not always work as intended,
public static int biggest (ii
{
if ((a > b) && (a > c))
4
veturn a;
d
else if ((b > a) && (b > ¢))
4
return b;
a, int b,
else
{
return o;
3
}
Which of the following best describes the error in the method?
(A) biggest always retums the value of a.
int c)
(B) biggest may not work correctly when c has the greatest value.
(©) biggest may not work correctly when a and b have equal values,
(D) biggest may not work correctly when @ and c have equal values,
(©) biggest may not work comectly when » and c have equal values.
[Uamstorsed sop
shy par ots ge!
16.
GO ON TO THE NEXT PAGE.15. Consider the following method.
public static void showMe(int arg)
i
if (arg < 10)
{
showMe(arg.+ 1);
}
else
t
System.out.print(arg + * *);
}
‘What will be printed as a result of the call showte (0) ?
(a) 10
(B) 11
©a123456789
93876543210
012345678910
[Grautiorised coping or rou of
[sy ar of his page Moga
a7
GO ON TO THE NEXT PAGE.16, Consider the following method.
1 Precondition: values has at least one row */
public static int calculate(int(](] values]
{
int found = values{0) {0};
int result = 0;
for (int{] row : values}
{
for (int y = 0; y < row.length; y++)
C
if (rowly] > found)
t
found = rowly];
result = y;
z
)
y
return result;
)
Which of the following best describes what is retuned by the calculate method?
(A) The largest value in the two-dimensional array
(B) The smallest value in the two-dimensional array
(©) The row index of an element with the largest value in the two-dimensional array
(D) The row inidex of an element with the smallest value in the two-dimensional array
(E) The column index of an element with the largest value in the two-dimensional array
[Unsutioaed copying ot rue ol
18 a
any bar of tus peoe te logal : GO ON TO THE NEXT PAGE.17. Consider the following method,
/)* Precondition: num > 0 */
public static int doWhat(int num)
‘
int var =
for (int loop =
al
loop <= num; loop = loop + 2)
var += loop:
y
return var;
}
‘Which of the following best describes the value retumed from a call to dowhat ?
(A) sum
(B) The sum of all integers between 1 and num, inclusive
(©) The sum of all even integers between 1 and num, inclusive
(D) The sum of all odd integers between 1 and num, inclusive
(B) No value is returned because of an infinite loop.
18, What is printed as a result of executing the following statement?
System. out.printin(404 / 10 * 10 + 1);
ay 4
B) 5.
oa
(D) 401
(& 405
GO ON TO THE NEXT PAGE,
19619, Consider the following cade segment.
* condition */ )
System.out.print(x +" *);
d
kau +2;
‘The following conditions have been proposed to replace /* condition */ in the code segment,
For which of the conditions will noth
(a) Tony
! (B) Monty
(©) Land only
(D) [and Il only
© 1.0, and tt
sy par of te page toga
-20-
GO ON TO THE NEXT PAGE.20. Consider the following method.
/** Precondition: arr.length > 0 */
public static int mystery(int[] arr)
¢
int'index = 0;
int count
int m
for (int outer = 0; outer < arr.length; outer++]
¢
inner = cuter + 1; inner < arr.length; inner++)
if (arr[outer]
i
arr[inner])
count++;
ee
if (count > m)
C
index = outer;
m = count;
}
>
return index;
)
Assume that mums has been declared and initialized as an array of integer values. Which of the following
best describes the value returned by the call mystery (nums) ?
(A) The maximurh value that occurs in nums
(B) An index of the maximum value that occurs in. mums,
(©) The number of times that the maximum value occurs in nums
(D) A value that occurs most often in mums
(B) An index of a value that occurs most often in nums
any profs page
GO ON TO THE NEXT PAGE,
2121. Consider the following recursive method.
public static void whatsTtDo(string str)
t
int len = sté.length();
J 4 Gen > 1)
‘
String temp = str.substring(0, len - 1);
Systen.out.printin(temp) ;
whatsTtDo (temp) ;
)
)
What is printed asa result ofthe call whatsTtDo("WATCH") ?
aa
(@) ware
(©) ance
ATC
AT
) ware
wae
Wa
w
© warcH
WAC
WAT
WA
[Unavihoize copying arouse
sry part of is og Hepa
GO ON TO THE NEXT PAGE.22. Consider the following definition,
int{][] numbers = ((1, 2, 3),
(4, 5, 643;
Which of the following code segments produces the output 123456 ?
(A) for (int{] row : numbers)
c
for {int n.: row)
‘
System. out.print (n) ;
)
y
(B) for (int{] row : numbers)
t
for (int n : row)
«
system. out print (rowin]
)
)
(©) for (int re = 0; ro < numbers.length; xct+)
System. out print (numbers [rc})
}
(D) for (int x
{
for (int
4
System. out. print (numbers[r] [c]);
)
)
0; x < numbers[0].length; r++)
© < numbers.length; c++)
{E) for (int ¢ = 0; ¢.< numbers[0].length; c++)
f for (int r = 0; r < numbers.length; r++)
© system.out print (munbers [x] [¢) ;
F 1
GO ON TO THE NEXT PAGE.
23.23. Consider the following code segment from an insertion sort prégram
for (int j = 1; j < arr.length; j++)
t
int insertItem = arr(];
intk=j- 1;
while (k >= 0 && insertItem < arr{k))
{
arr(k + 1] =arr[k];
keow;
>
arr{k + 1] = insertItem;
(* end of for loop */
}
Assume that array ax has been defined and initialized with the values (5, 4, 3,.2, 1), What are
the values in array ary after two passes of the for loop (ie., when 5 = 2 atthe point indicated by
1* end of for loop */) ?
A) (2, 3, 4,5, 1)
®B) (3, 2, 4, 4, 5)
Cael eerie etal
() 22h ye en
CE) are
Gnstorised spying oy rove ol
secretes rape eee GO ON TO THE NEXT PAGE.
-24-24. Consider the following class,
public class SomeMethods
€
public void one(int first
{ /* implementation not shown */ 3
public void one(int first, int second)
{ /* implementation not shown */ }
public void one(int first, String second)
{ /* implementation not shown */ }
)
Which of the following methods can be added to the SomeMetheds class without causing a
compile-time error?
I. public void one(int valu
(/* implementation not shown */ — }
I public void one(string first, int second)
{ /* implementation not shown */ )
I. public void one(int first, int second, int thira)
(- /* implementation not shown */
(A) Fonly
(B) Vand Il only
(© Land only
(D) Hand I only
© U0, and
[Unauthorised coping o reuse a
= any par cts pon egal
GO ON TO THE NEXT PAGE.
-25-25. Consider the following code segment.
int count = 07
for (int x = 0; x < 4; xt)
c
for (int y = x; ¥ < 4; y++)
4
count++;
d
System. out :printin{eount) ;
What is printed as a result of executing the code segment?
(a4
B) 8
© 10
) 16
©) 20
choraed spying or aaseo
ly prt of page Hoge
-26-
GO ON TO THE NEXT PAGE,26. Consider the following two methods, which appear within a single class.
public static void changert(int[] arr, int val, String word)
‘
arr = new int(5];
val = 0;
word = word.substring(0, 5);
for
£
y
7
0;
(int, k
arr(k]
= 0; k < arr.length; k++)
°
public static void start ()
iE)
{
Ant{] nums = (1, 2, 3, 4, 53;
int value
String name = "blackboard";
changelt (nums, value, name);
for (int k = 0; k < nums.length; kr+)
‘
system.out.print (nums[k] +"
}
system. out.print (value +" ");
System. out .print (name) ;
y
What is printed as a result of the call start () ?
(A) 0000.0 0 black
(B) 0 0 0:0 0 & blackboard
(123456 black
(D) 123 450 black
(B) 123 45 6 blackboard
[any part of nis ge ihegal
27.
GO ON TO THE NEXT PAGE.Questions 27-28 refer to the following information.
Consider the following sort method, This method correctly sorts the elements of aay ci
increasing order.
public static void sort (int[] data)
(
for (int j = 0; 4 < data.length - 1; j++)
{
int m
for (int k = j +1; k < data.length; k++)
{
if (datalk] < datatm)) — /* Compare values */
q
mek;
2
y
int temp = data{nl; /* Assign to temp */
datalm) = dataljl;
data(j] = temp;
1 End of outer loop */
)
)
27. Assume that soxt is called with the array (6, 3, 2, 5, 4, 1). Whatwill the value of data be
after three passes of the outer loop (ie. when j = 2 atthe point indicated by /* End of ower loop */) ?
@) G, 2,3, 4, 5, 6
iG eae hess a eo)
(© {1, 2, 3, 6, 5, 4}
ot. a, a: A; 6)
KEN reaeez eS (ave 6)
28. Assumie that sort is called with the aay (1, 2, 3, 4, 5, 6}. How many times will the expression
indicated by /* Compare values */ and the statement indicated by /*- Assign to temp */ execute?
Compare values Assign 10 temp
@ 15 0
®) Is 5
©: 6
© 4 5
® 2 6
oriaed yng 3
yee te pl
GO ON TO THE NEXT PAGE,
-28-29. Consider the following recursive method.
/>* Precondition: nun > 0 */
public static int what (int num)
{
if (num < 10)
4
return 1;
else
«
return 1 +, what (num / 10);
d
}
Assume that int val has been declared and initialized with a value that satisfies the precondition of the
‘method. Which of the following best describes the value retumed by the call. what (val) ?
(A) The number of digits in the decimal representation of val. is retuned.
(B) The sum of the digits in the devimal representation of val. is returned.
(©) Nothing is returned. A runtime error occurs because of infinite recursion.
(D) The value 1 is retumed.
(©) The value val/10 is retumed.
shy par ofthis peae soo
GO ON TO THE NEXT PAGE.
-29-30. The price per box of ink pens advertised in an office supply catalog is based on the number of boxes ordered.
The following table shows the pricing,
Number of Boxes Price per Box
1 up to but not including $ $5.00
5 up to but not including 10 $3.00
10 or more $1.50
The following incomplete method is intended to return the total cost of an order based on the value of the
Parameter numBoxes
(** Precondition: namBexes > 0 */
public static double cetCost (int numBoxes]
c
double totalcost = 0.0;
/* missing code */
return totalCost;
}
GO ON TO THE NEXT PAGE,Wich of the following code segments can be used to replace /* missing code */ so that method getCost
will work as intended?
ei eee crea)
: totalCost = numBoxes * 1.50;
ie (mnzones > 5)
| eoEalcost = muntones 3.00%
if (numBoxes > 0)
_ totalCost = numBoxes * 5.00;
)
TL if (numBokes >= 10)
ere eee 50;
elec lrenear ae)
ee cee eer ta
2
else
€
| totalCost = numBoxes * 5.00;
3}
UL if (numBoxes > 0)
{
totalCost = numBoxes * 5.00;
}
else if (numBoxes >= 5)
‘
totalCost = numBoxes * 3.00;
y
else if (numBoxes >= 10)
. t
}
totalCost = numBoxes * 1.50;
(A) Loaly
®) Tonly
(©) Monly
(D) Land
©) Wand OT
ony pa ofthis ge egal
GO ON TO THE NEXT PAGE,
-31-31. Consider the following code segment.
String(][] board = new string[5] [5];
for (int row = 0; row < 5; rows+)
ce
for (int col = 0; col < 5; colts)
{
board[row] [col] = "0";
)
)
for (int val = 0; val < 5; val++)
¢
if (val 4 2 )
4
int row = val;
int col = 0;
while (col < 5 && row >= 0)
(
board{row] [col] = "x";
col++;
)
)
[Ursuthoreed oop
lanypartott
-32-
GO ON TO THE NEXT PAGE.Which of the following represents board after this code segment is executed?
125.4
0 [xTo[xfoyrx
0
GO ON TO THE NEXT PAGE,
33.
A
1
o[xlo[xfo
[Ox [olx
o[x|[olx|o
O[x|olzlo
o
Ofx[ololx
‘ofo[x|olo
0
x[o[x|olo
o|x[ofolo
@
2 (xo |xlo[x
3 [olxlo}x[o
4 [zfo[xfolx
1
0 [e[x]o[x]o
B)
1
2[ol|zfolx[o
3 [elolxlorx
4 [ofe}o[xfo
0 [x[efolo,x
©
2[olelxfolo
1
4 [xfoyololx
1
0 foTx[oloqto
()
2[x[o[olx[o
4 [o[o[x [oto
1
3
0 [oTXToTXTo
®
3 [x[efololo
4 [e[ofotefo
[Ussutrsed Spying of Tous
lay er of hs poas a Wage32. Consider the following class declaration.
public class studentInfo
¢
private String major;
private int age;
public String getMajor()
{ return major;
public int getage()
{ return ace; }
47 There may be instance variables, constructors, and methods that are not shown,
d
The following instance variable and method appear in another class,
private List
students;
/** @return the average age of students with the given major;
* -1.0 ifno such students exist
public double averageAgeInMasor (st:
{
double sum = 0.0;
mt count = 0;
ox (StudentInfo k : student,
ring theMajor)
/* missing code */
if (count > 0)
‘
return sum / count;
else
‘
return -1.0;
)
)
any partot tne ge lope
‘GO ON TO THE NEXT PAGE.
-34-‘Which of the following could be used to replace /* missing code */. so that averageAgeTnMajor will
‘compile without error?
(A) if (theMajor.equals(k.major) )
{
sum += k.age;
count++;
y
(B) if (theNiajor equals (k.getMajor(}))
t
sum += k.getage(};
count++;
?
(thetfajor. equals (k.major))
‘
sum += k.getage();
count++;
(D) it
heMajor .equals (students [k] .getMajor()))
students [k] .getage();
count++;
@) if (themajor. equals (getMajor(k)))
eum += getage(k);
count++;
}
33,
Which of the following statements regarding interfaces is FALSE?
(A) All methods in an interface are public.
(B) An interface cannot be instantiated
(C) An interface can declare an instance variable.
(D) A non-abstract class can implement an interface.
(©) An abstract class can implement an interface.
-35-
GO ON TO THE NEXT PAGE,34. Consider the problem of finding the maximum value in an array of integers: The folloiving code segments are
proposed solutions to the problem. Assume that the variable axx has been defined as an array of int values
and has been initialized with one or more values.
Lint max = Integer .MIN_VALUE;
for (int value : arr}
{
if (max < value)
€
max = value;
+
d
IL int max ="0;
boolean first = true;
for (int value : arr]
4
if (first)
i 4
max = value;
first = false;
?
else if (max < value}
c
max = value;
by
z
WL int max = arr{0l;
for (int k = 1; k < arr.length; k++)
£
if (max < arr[k])
‘
max
}
arrik];
3
Which of the code segments will always correctly assign the maximum element of the array to the
variable max?
(A) only
B) Honly
(C) Wonly
(D) Wand It only
© LU, nam
asainoraed opving or
sny ar oft ego a teas
GO ON TO THE NEXT PAGE.
-38-35. Consider the following instance variable and method. Method wordsvtithCommas is intended to return a
string containing all the words in 1istofWords separated by commas and enclosed in braces,
For example, if 1istofWords contains ["one", "two", “three"),
wordsWithCommas() shouldbe "{one, two, three)",
private List listofWords;
public String wordsWithCommas ()
{
String result
int sizeofList = /* expression */ ;
for (int k
£
result = result + listofWords. get (k);
0; k < sizeofbist; k++)
if ( /* condition */ )
{
result = result +", *;
d
?
result = result +
return result;
)
the string returned by the call
Which of the following can be used to replace /* expression */ and /* condition */ so that
wordsiithCommas will work as intended?
» expression */,
(A) Listofwords.size() - 1 kK I=0
(B) Listofwords.size() k I= 0
(©) listofiords.size() - 1 k != sizeofList
(D) listofwords. size (jf k != sizeofnist
(©) result.1ength() ei 8
Unauthodeed copying or rea
ny ba of he page hol
-37-
end)
{
int mid = (start + end) / 2; /* Caleulate midpoint */
(target < datalmid])
end = mid - 1
)
else if (target > data[mia))
start = mid + 1;
)
else
{
return mid;
)
)
return -1;
GO ON TO THE NEXT PAGE.36, Consider the following code segment.
int[] values
int target =
ear oe,
‘What value is retuned by the call binarySearch(values, target) ?
(A) -l
B) 3
os
©) 6
® 8
37, Suppose the binarySearch method is called with an array containing 2,000 elements sorted in increasing
order. What is the maximum number of times that the statemeat indicated by /* Calculate midpoint * /
could execute?
(A) 2,000
(B) 1,000
© .20
© 0
® 1
[Unauthorised copying oc roura
Shy par ots page sepa
GO ON TO THE NEXT PAGE,
-39-38. Consider the following incomplete method that is intended to retum a string formed by concatenating elements
from the parameter words. The elements to be concatenated start with start Index and continuc through
the last element of words -and should appear in reverse order in the resulting string.
/** Precondition: words. length > 0;
2 startindex >= 0
+
public static String concatwords(string{] words, int startindex)
‘
String result =
/* missing code. */
return result;
For example, the following code segment uses a call to the concatiforas method.
String[] things = {"Bear", "Apple", "Gorilla", "House", "Car"};
System. out.println{concatWords(things, 2));
‘When the code segment is executed, the string "CarHouseGorilla” is printed,
‘The following three code Segments have been proposed as replacements for /* missing cade ~ /
L for (int k = startindex; k < words.length; k++)
(
result
?
= words (k] + words[words.length - - 1];
I. int _k = words.length - 1;
, while (k >= startIndex)
{
result += words[k];
a
)
MI. ~ stringi] temp = new String[words.length]
for (int * = 0; k <= words.length / 2; k++)
£
temp[k] = words [words.length - k - 1];
temp[words.length - k - 1] = words[k];
k < temp.length - startindex;
k++)
result += temp(k];
>
‘Which of these code segments can be used to replace /* missing code */ sothat concatWiords will
work as intended?
(A) Tonly
(B) Tonly
(©) only
(D) Tana
© Wand
any par ofthis ge otowa GO ON TO THE NEXT PAGE.
-40-39. Consider the following method.
/** Precondition: 0 < numVale <= nums.length */
public static int mystery(int[] mums, int v, int numVals}
{
int k = 0;
if nums[numvals - 1])
£
k= 4;
d
if (numvals
i
return k;
>
Felse
{
return k + mystery(nums, v, numVals - 1);
>
)
‘Which of the following best describes what the call mystery (numbers, val, numbers.1
does? You may assume that variables numbers and val have been declared and initialized.
(A) Returns 1 ifthe last element in numbers is equal to val; otherwise, returns 0
(B) Retums the index of the last element in numbers that is equal to val
(©) Retums the number of elements in numbers that are equal to val
(D) Retums the number of elements in numbers that are not equal to val
(E) Retums the maximum number of adjacent elements that are not equal to val
Unntoriad saping rouse]
any parc his pope aga
GO ON TO THE NEXT PAGE.
a140. Consider the following code segment.
List students = new ArrayList();
students. add("Alex");
students. add ("Bob") ;
students. add("Carl");
for (int-k
{
System. out.print (students. set (k,
)
System.out printn();
for (String str :
i
System.out.print(str +"
‘What is printed as a result of executing the code segment?
d
(A) Alex
Alex
(B) Alex
Alex
(© Alex
Alex
() Alex
Alex
Alex © Alex
Alex Alex
Alex Alex
Bob. Carl
Bob Carl
Alex Alex
Bob Carl
Bob Carl
k < students.size(); k++)
lex") +");
(©) Nothing is printed because the frst print statement will cause a runtime exception to be thrown,
[onaumans copying srr
‘Shy parol iis ope egal
42-
GO ON TO THE NEXT PAGE.