0% found this document useful (0 votes)
2K views57 pages

AP Computer Science A Multiple Choice Questions Packet

The document contains a series of multiple-choice questions related to AP Computer Science A concepts, including Java programming, data structures, and algorithms. Each question presents a code snippet or scenario, asking for the expected output or identifying errors in the code. The questions cover a wide range of topics, such as loops, method overloading, and array manipulation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2K views57 pages

AP Computer Science A Multiple Choice Questions Packet

The document contains a series of multiple-choice questions related to AP Computer Science A concepts, including Java programming, data structures, and algorithms. Each question presents a code snippet or scenario, asking for the expected output or identifying errors in the code. The questions cover a wide range of topics, such as loops, method overloading, and array manipulation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

AP Computer Science A Multiple Choice Questions Packet

Directions: Determine the answer to each of the following questions or in complete statements, using the
available space for any necessary scratch work. Then decide which the best of the choices given is. Do not
spend too much time on any one problem.

Notes:
• Assume that the classes in the Quick Reference have been imported where needed.
• Assume that variables and methods are declared within the context of an enclosing class.
• Assume that method calls that have no object or class name prefixed, and that are not shown within a
complete class definition, appear within the context of an enclosing class.
• Assume that parameters in method calls are not null unless otherwise stated.

1) Determine the output after the following code is run

int num = 5;
num++;
num /= 3;
double p = 2.5;
p *= num;
System.out.println(p);

(A) Nothing is printed due to an error in the program.


(B) 2.5
(C) 5
(D) 5.0
(E) 2

2) What does the following method return when mystery(4) is called?

public static int mystery(int n){


if (n == 1)
return 1;
else
return n * mystery(n - 1);
}

(A) 0
(B) 1
(C) 6
(D) 12
(E) 24

1
3) What will be printed by this code?

int x = 0;
int y = 0;
while(x < 2) {
y = 2;
while(x*y <= 2) {
System.out.print(x + " " + y + " ");
y++;
}

System.out.println();
x++;
}

(A) 1 2
(B) 0 2
(C) 0 2 1 3
(D) Nothing; the loop never begins.
(E) Nothing; this is an infinite loop.

4) Which declaration will cause an error?

I. ArrayList<String> stringList = new ArrayList<String>();


II. ArrayList<int> intList = new ArrayList<int>();
III. List<String> stringLists = new ArrayList<String>();

(A) I only (B) II only (C) III only (D) I and III only (E) II and III only

5) A certain class, SomeClass, contains a method with the following header:

public int getValue(int n)

Suppose that methods with the following headers are now added to SomeClass:

I . public int getValue()


II. public double getValue(int n)
III. public int getValue(double n)

Which of the above headers will cause an error?

(A) None
(B) I only
(C) II only
(D) III only
(E) I and III only

2
6) Which of the following pieces of code will correctly calculate the area of a circle (𝐴 = 𝜋𝑟 2 )

I. int radius = 5;
final double area = Math.PI * Math.pow(radius, 2);

II. final int radius = 5;


final double PI = 3.14;
double area = PI * Math.pow(radius, 2);

III. int radius = 5;


double area = Math.PI * Math.pow(radius, 2);

(A) I only
(B) II and III only
(C) I and III only
(D) II only
(E) I, II, and III

7) What are the values of a and b after the following code executes?

int a = 10, b = 20;


a += b;
b += a;
b -= a;
a -= b;

(A) a = 10 and b = 50
(B) a = 30 and b = 20
(C) a = 20 and b = 10
(D) a = 30 and b = 50
(E) a = 10 and b = 20

8) Consider these declarations:

ArrayList<String> stringList = new ArrayList<String>();


String space = " ";
int myInt = 5;

Which statement will cause an error?

(A) stringList.add(space);
(B) stringList.add("Computer");
(C) stringList.add(“myInt”);
(D) stringList.add(space + 8);
(E) stringList.add(myInt + 8);

3
9) Consider the following statement:

int num = /*expression*/;

Which of the following replacements for /*expression*/ creates in num a random integer from 2 to 50,
including 2 and 50?

(A) (int)(Math.random() * 50) — 2


(B) (int)(Math.random() * 49) — 2
(C) (int)(Math.random() * 49) + 2
(D) (int)(Math.random() * 50) + 2
(E) (int)(Math.random() * 48) + 2

10) Which of the following pairs of statements will cause an error message?

I. double x = 14.7;
int y = x;

II. double x = 14.7;


int y = (int) x;

III. int x = 14;


double y = x;

(A) None
(B) I only
(C) II only
(D) III only
(E) I and III only

11) What value is stored in number after the following initialization?

int number = 200 % 3;

(A) 2
(B) 3
(C) 66
(D) 66.66666666
(E) 203

4
12) What output is produced by the following line of code?

int myNumber = 4;
System.out.println("myNumber/3 = 1");
System.out.println(myNumber/3);

(A) Complier Error (B) (C)


1.333333333333 = 1 1=1
1.333333333333 1

(D) (E)
myNumber/3 = 1 myNumber/3 = 1
1.333333333333 1

13) Which of the following statements does NOT display 2/3?

(A) System.out.println("2/3");
(B) System.out.println("2" + "/" + "3");
(C) System.out.println(2/3);
(D) System.out.println(2 + "/" + 3);
(E) System.out.println((int)2 + "/" + (int)3);

14) Consider the following code segment.

int num = 0;
int score = 10;
if (num != 0 && score / num > SOME_CONSTANT)
statement1;
else
statement2;

What is the result of executing this statement?

(A) An ArithmeticException will be thrown.


(B) A syntax error will occur.
(C) statement1, but not statement2, will be executed.
(D) statement2, but not statement1, will be executed.
(E) Neither statement1 nor statement2 will be executed.

5
15) What is the output from the following code?

int x = 5, y = 2;
System.out.println(x/y - (double)(x/y));

(A) 0.0
(B) 0.5
(C) −0.5
(D) −2.5
(E) None of the above.

16) Consider the following code segment.

ArrayList <String> myKids = new ArrayList<String>();

myKids.add(“Emmie”);
myKids.add(“Gus”);
myKids.add(“Diana”);

for(int i = 0; i < mykids.size(); i++)


{
System.out.print(myKids.set(i, “Emmie”) + “ “);
}

System.out.println();

for(String kid : myKids)


{
System.out.print(kid + “ “);
}

What is printed as a result of executing the code segment?

(A) Emmie Emmie Emmie (B) Emmie Emmie Emmie (C) Emmie Gus Diana
Emmie Emmie Emmie Emmie Gus Diana Emmie Emmie Emmie

(D) Emmie Gus Diana (E) Nothing is printed because the first print statement will cause an error.
Emmie Gus Diana

6
17) What are the values of u and v after the following code is executed?

int u = 3, v = 5;
u += v;
v += u;
u -= v;
v -= u;

(A) v is 0 and u is 0
(B) v is −5 and u is −3
(C) v is 3 and u is 5
(D) v is −5 and u is 18
(E) v is 5 and u is 3

18) What is the output of the program segment below?

int a = 100;
int b = 200;
int c = a + b;
String d = "100";
String e = "200";
String f = d + e;
System.out.println(c);
System.out.println(f);

(A) 300
300

(B) 300
100200

(C) 200
100100

(D) 100200
100200

(E) Error message

7
19) Consider the code below.

public class IntOps


{
public static void main(String[] args)
{
int a = 20
int b = 3
int sum = a + b;
int prod = a * b;
int quot = a / b;
int mod = a % b;
System.out.println(sum + prod + quot + mod);
}
}

What is printed off to the screen as the program executes?

(A) Nothing; there is a compiler error


(B) 236062
(C) 20
(D) 91.6666666667
(E) 91

20) Consider the code below.

int x = 14;
while(!(x%3 == 0 && x%5 == 0))
{
System.out.println(x);
x += 2;
}

What is the last number printed by this code?

(A) 30
(B) 28
(C) 14
(D) 16
(E) 18

8
21) Consider the following code segment.

for(int r = 3; r > 0; r--)


{
int c;
for(c = 1; c < r; c++)
{
System.out.print(“-”);
}
for(c = r; c <= 3; c++)
{
System.out.print(“*”);
}

System.out.println();
}

What is printed as a result of executing the code segment?

(A) (B) (C) (D) (E)

--* *-- *** *** --*


-** **- -** **- ***
*** *** --* *-- --*

22) What is printed as a result of the following code segment?

int count = 0;

for(int x = 0; x < 4; x++)


{
for(int y = x; y < 4; y++)
{
count ++;
}
}
System.out.println(count);

(A) 4
(B) 8
(C) 10
(D) 16
(E) 20

9
23) Refer to the static method removeNegs shown below.

//Precondition: list is an ArrayList<Integer>.


//Postcondition: All negative values have been removed from list.

public static void removeNegs(List<Integer> list)


{
int index = 0;
while (index < list.size())
{
if (list.get(index).intValue() < 0)
{
list.remove(index);
}
index++;
}
}

For which of the following lists will the method not work as intended?

(A) 6 -1 -2 5
(B) -1 2 -3 4
(C) 2 4 6 8
(D) -3
(E) 1 2 3 —8

Use the following code to answer questions 24 and 25.

public class SomeClass {


private int myA;
private int myB;
private int myC;

// Constructor(s) not shown here.

public int getA(){


return myA;
}

public void setB(int value){


myB = value;
}
}

The following declaration appears in another class.

SomeClass obj = new SomeClass();

10
24) Which of the following code segments will compile with no error?

(A) int x = obj.myA;


(B) int x;
obj.getA(x);
(C) int x = obj.getA();
(D) int x = SomeClass.getA();
(E) int x = getA(Obj);

25) Which of the following changes to SomeClass will allow other classes to access but not modify the value
of myC?

(A) Make myC public (B) Include the method: (C) Include the method:
private int getC(){ public void getC(int x){
return myC; x = myC;
} }

(D) Include the method: (E) Include the method:


public int getC(){ private void getC(int x){
return myC; x = myC;
} }

26) What does the following loop do?

int num = scan.nextInt();


int sum = 0;

while(num > 0)
{
sum += num % 10;
num /= 10;
}

System.out.print(sum);

(A) Find the lowest common denominator of the number the user enters.
(B) Find the remainders of the number the user enters when divided by ten.
(C) Sums each digit of the number the user enters.
(D) Find the quotients of the number the user enters when divided by ten.
(E) Find the factorial of the number the user enters.

11
27) Consider the following code.

int x = 9;
if(x/2 <= 4)
{
x++;
}
if(x/2 >= 5)
{
x += 2;
}
System.out.println(x);

What is the output of the code?

(A) 9 (B) 10 (C) 11 (D) 12 (E) 13

28) Consider a sorted array arr of n elements, where n is large and n is even. Under which conditions will a
sequential search of arr be faster than a binary search?

I. The target is not in the list.


II. The target is in the first position of the list.
III. The target is in arr [l + n/2]

(A) I only
(B) II only
(C) III only
(D) I and III only
(E) II and III only

29) Consider the following segment of code.

String word = “Computer Science”;


int value = word.indexOf(“puter”);
String piece = word.substring(0, value) + word.substring(value + 2);

What will be stored in the variable piece after the code segment executes?

(A) Compter Science


(B) Comuter Science
(C) Comter Science
(D) Comter
(E) Science

12
30) Consider the following code segment.

int myNumber = 5;
boolean myFlag = true;
if(myFlag)
{
myNumber++;
}
if(myFlag || false)
{
myNumber++;
myFlag = false;
}
if(myFlag && false)
{
myNumber++;
}

What is the value of myNumber after the above code executes?

A) 5 B) 6 C) 7 D) 8 E) Nothing, there is a compiler error.

31) Consider writing a program that produces statistics for long lists of numerical data. Which of the following
is the best reason to implement each list with an array of int (or double), rather than an ArrayList of
Integer (or Double) objects?

(A) An array of primitive number types is more efficient to manipulate than an ArrayList of wrapper objects
that contain numbers.
(B) Insertion of new elements into a list is easier to code for an array than for an ArrayList.
(C) Removal of new elements into a list is easier to code for an array than for an ArrayList.
(D) Accessing individual elements in the middle of a list is easier for an array than for an ArrayList.
(E) Accessing all the elements is more efficient in an array than in an ArrayList.

32) Refer to the following program segment.

for(int n = 50; n > 0; n = n/2)


System.out.println(n);

How many lines of output will this segment produce?

(A) 50 (B) 49 (C) 7 (D) 6 (E) 5


33) Consider the following code segment.
13
int x = 1;
while( /* condition */ )
{
if(x % 2 == 0)
{
System.out.print(x + “ “);
}
x = x + 2;
}

The following conditions have been proposed to replace /* condition*/ in the code segment.

I. x < 0
II. x <= 1
III. x < 10

Which of the above conditions will allow nothing to be printed?

A) I only
B) II only
C) I and II only
D) I and III only
E) I, II, and III

34) Which features can you use to recognize constructor methods in a class declaration?

I. The constructor identifier is the same as the class identifier.


II. Constructors use both the public and the static keywords.
III. Constructors are neither void methods nor return methods.

(A) I only
(B) II only
(C) I & III only
(D) II & III only
(E) I, II & III

35) What is the value of n after the following code is executed?


14
int i = 0;
int n = 0;
while(n < 20)
{
for(i = 0; i < 3; i++)
{
n += 3;
}
n++;
}

(A) 10
(B) 9
(C) 13
(D) 20
(E) 30

36) Let list be an ArrayList<String> containing only these elements:

“Alex”, “Emma”, “John”, “Luis”

Which of the following statements will cause an error to occur?

I. list.set(2, “6”);
II. list.add(4, “Steven”);
III. String s = list.get(4);

(A) I only
(B) II only
(C) III only
(D) II and III only
(E) I, II, and III

37) Consider the following static method.


15
public static int compute(int n)
{
for (int i = 1; i < 4; i++)
n *= n;
return n;
}

Which of the following could replace the body of compute, so that the new version returns the identical result
as the original for all n?

(A) return 4 * n;
(B) return 8 * n;
(C) return 64 * n;
(D) return (int) Math.pow(n, 4);
(E) return (int) Math.pow(n, 8);

38) The Boolean expression A <= B is equivalent to which of the following expressions?

(A) !(B >= A)


(B) !(A <= B)
(C) !(A != B)
(D) B > A
(E) B >= A

39) Examine the following code:

int count = 1;
while ( /*condition goes here */ ) {
System.out.print( count + " " );
count = count + 1;
}
System.out.println();

What condition should be used so that the code writes out:

12345678

(A) count < 8


(B) count < 9
(C) count +1 <= 8
(D) count != 8
(E) count <= 9
40) Consider the code segment
16
if(n == 1)
k++;
else if(n == 4)
k += 4;

Suppose that the given statement is rewritten in the form

if(/* condition */)


/*assignment statement*/;

Given that n and k are integers, and that the rewritten code performs the same task as the original code, which
of the following could be used as

(1) /* condition */ (2) /*assignment statement*/

(A) (1) n == 1 && n == 4 (2) k += n


(B) (1) n == 1 && n == 4 (2) k += 4
(C) (1) n == 1 || n == 4 (2) k += 4
(D) (1) n == 1 || n == 4 (2) k += n
(E) (1) n == 1 || n == 4 (2) k = n – k

41) What does the following Boolean expression evaluate to?

p = true;
q = false;
System.out.println((!p&&!q)||!(p||q));

(A) true
(B) null
(C) false
(D) Compiler Error
(E) Not enough information to answer the problem

17
42) Consider the following method.

public int mystery(int n)


{
if (n == 0)
return 0;
else if (n % 2 == 1)
return n;
else
return n + mystery(n - 1);
}

What will be returned by a call to mystery(6)?

(A) 6
(B) 11
(C) 12
(D) 27
(E) 30

43) Consider the following method

private ArrayList<String> animals;

public void manipulate()


{
for(int k = animals.size()-1; k > 0; k--)
{
String animal = animals.get(k);
if(animal.substring(animal.length() – 1 ).equals("n"))
{
animals.add(animals.size()-k,animals.remove(k));
}
}
}

Assume that animals has been instantiated and initialized with the following contents.

["dolphin", "giraffe", "tiger", "penguin", "elephant", "lion"]

What will the contents of animals be as a result of calling manipulate?

(A) ["lion", "giraffe", "tiger", "penguin", "dolphin", "elephant"]


(B) ["dolphin", "giraffe", "tiger", "penguin", "elephant", "lion"]
(C) ["giraffe", "tiger", "elephant", "dolphin", "penguin", "lion"]
(D) ["dolphin", "lion", "penguin", "giraffe", "tiger", "elephant"]
(E) ["giraffe", "elephant", "tiger", "penguin", "lion", "dolphin"]
44) Consider the following output
18
11111
2222
333
44
5

Which of the following code segments will produce this output?

(A) for( int j = 1; j <= 5; j++) {


for(int k = 1; k <= 5; k++) {
System.out.print(j + " ");
}
System.out.println();
}

(B) for( int j = 1; j <= 5; j++) {


for(int k = 1; k <= j; k++) {
System.out.print(j + " ");
}
System.out.println();
}

(C) for( int j = 1; j <= 5; j++) {


for(int k = 5; k >= 5; k--) {
System.out.print(j + " ");
}
System.out.println();
}

(D) for( int j = 1; j <= 5; j++) {


for(int k = 5; k >= j; k--) {
System.out.print(j + " ");
}
System.out.println();
}

(E) for( int j = 1; j <= 5; j++) {


for(int k = j; k <= 5; k++) {
System.out.print(k + " ");
}
System.out.println();
}

45) What does the following print?


19
int i = 1;
int k = 1;
while(i < 5)
{
k *= i;
k++;
}
System.out.print(k);

(A) 6 (B) 10 (C) 24 (D) 120 (E) Nothing is printed.

46) The Boolean expression (A||B)&&B is true

(A) only when A is true.


(B) only when B is true.
(C) whenever either A is true or B is true.
(D) only whenever both A is true and B is true.
(E) for all values of A and B.

47) Consider the following code segment.

int num1 = value1, num2 = value2, num3 = value3;


while (num1 > num2 || num1 > num3)
{
/* body of loop */
}

You may assume that value1, value2, and value3 are int values. Which of the following is sufficient to
guarantee that /* body of loop */ will never be executed?

(A) There is no statement in /* body of loop */ that leads to termination


(B) num1 < num2
(C) num1 < num3
(D) num1 > num2 && num1 > num3
(E) num1 < num2 && num1 < num3

48) Consider the following recursive method

20
public static void whatsItDo(String str)
{
int len = str.length();
if(len > 1)
{
String temp = str.substring(0, len-1);
whatsItDo(temp);
System.out.println(temp);
}
}

What is printed as a result of the call whatsItDo("WATCH")?

(A) WATC
WAT
WA
W

(B) WATCH
WATC
WAT
WA

(C) W
WA
WAT
WATC

(D) W
WA
WAT
WATC
WATCH

(E) WATCH
WATC
WAT
WA
W
WA
WAT
WATC
WATCH

49) Which is true of the following Boolean expression, given that x is a variable of type double?
21
3.0 == x * (3.0 / x)

(A) It will always evaluate to false.


(B) It may evaluate to false for some values of x.
(C) It will evaluate to false only when x is zero.
(D) It will evaluate to false only when x is very large or very close to zero.
(E) It will always evaluate to true.

50) What is the output of the following code?

String myString = "APCOMSCI";


int k = myString.indexOf(“O”);
int p = myString.indexOf(“J”);
k += p;
System.out.println(myString.substring(k));

(A) COMSCI
(B) MSCI
(C) OMSCI
(D) O
(E) M

51) Consider the following segment of code.

String word = “conflagration”;


int x = word.indexOf(”flag”);
String s = word.substring(0, x);

What will be the result of executing the above segment?

(A) A syntax error will occur


(B) String s will be the empty string.
(C) String s will contain “flag”.
(D) String s will contain “conf”.
(E) String s will contain “con”.

52) Consider the following code segment.


22
String s = “VOLLEYBALL”;
String w = s.substring(s.indexOf(“L”) + s.indexOf(“O”)) + s.substring(5);
System.out.println(w);

What is printed as a result of executing the code segment?

(A) EYBALL
(B) YBALL
(C) LEYBALL
(D) BALL
(E) LEYBALLYBALL

53) What does the following method return if a call to mystery(2,5) is made?

public static int mystery(int x, int y) {


double average = ((double) y)/x;
average += 3.5;
return average;
}

(A) 6
(B) 6.0
(C) 5.5
(D) 5
(E) Nothing. There is an error in the program.

54) What is the output of the following code?

String s = "ONION";
String first = s.substring(1,5);
String other = first.substring(1,4);
String last = other.substring(0,2);
System.out.println(last);

A) I
B) IO
C) ION
D) ONI
E) NION

55) Assume that an array of integer values has been declared as follows and has been initialized.
23
int list[] = new int[15];

Which of the following code correctly swaps the values of list[2] and list[3]?

(A) list[2] = list[3];


list[3] = list[2];

(B) int n = list[2];


list[2] = list[3];
list[3] = list[2];

(C) int n = 3;
list[3] = list[2];
list[2] = list[n];

(D) int n = list[2];


list[2] = list[3];
list[3] = n;

(D) int n = list[2];


list[2] = list[3];
list[3] = list[n];

56) Consider the following code segment.

String s = “goodbye”;
System.out.println(s.substring(s.substring(s.length() - s.indexOf(“d”)).length()));

What is printed as a result of executing the code segment?

(A) bye
(B) 3
(C) dbye
(D) 2
(E) odbye

57) What is the output of the following code segment:


24
int t = 0;
for (int x = 3; x < 4; x++)
{
for (int y = 1; y <= 4; y++)
{
t++;
System.out.print(t + " ");
}
}

(A) 1234
(B) 123
(C) 12
(D) 4
(E) 31234

58) A class of 30 students rated their computer science teacher on a scale of 1 to 10 (1 means awful and 10
means outstanding). The responses array is a 30-element integer array of the student responses. An 11-element
array freq will count the number of occurrences of each response. For example, freq[6] will count the
number of students who responded 6. The quantity freq[0] will not be used.

Here is a program that counts the students’ responses and outputs the results.

public class StudentEvaluations


{
public static void main(String args[])
{
int[] responses =
{6,6,7,8,10,1,5,4,6,7,5,4,3,4,4,9,8,6,7,10,6,7,8,8,9,6,7,8,9,2};
int[] freq = new int[11];
for(int i = 0; i < responses.length; i++)
freq[responses[i]]++;
//output results
System.out.print(”rating” + “ “ + “frequency\n”);
for(int rating = 1; rating < freq.length; rating++)
System.out.print (rating + “ “ + freq[rating] + “\n”);
}
}

Suppose the last entry in the initializer list for the responses array was incorrectly typed as 12 instead of 2. What
would be the result of running the program?

(A) A rating of 12 would be listed with a frequency of 1 in the output table.


(B) A rating of 1 would be listed with a frequency of 12 in the output table.
(C) An ArrayIndexOutOfBoundsException would be thrown.
(D) A StringIndexOutOfBoundsException would be thrown.
(E) A NullPointerException would be thrown.
59) Consider the following code segment.
25
String word = //assume word is pointing to a valid String and not null

Which of the following segments of code will NOT reverse the String that word is pointing to?

(A) String reversed = “”;


for(int i = 0; i<word.length(); i++)
{
reversed = word.substring(i, i+1) + reversed;
}
word = reversed;

(B) String reversed = “”;


for(int i = word.length()-1; i >= 0; i--)
{
reversed = word.substring(word.length()-i-1, word.length()-i) + reversed;
}
word = reversed;

(C) String reversed = “”;


for(int i = word.length()-1; i >= 0; i--)
{
reversed = word.substring(word.length()-i-1, word.length()-i);
}
word = reversed;

(D) String reversed = “”;


for(int i = 0; i<word.length(); i++)
{
reversed += word.substring(word.length()-i-1, word.length()-i);
}
word = reversed;

(E) String reversed = “”;


for(int i = word.length(); i > 0; i--)
{
reversed += word.substring(i-1, i);
}
word = reversed;

60) Consider the following method:


26
public static String midString(String s) {
String output = “”;
int b = 0;
int t = s.length() - 1;
while(b <= t) {
output += s.substring(b, b+1);
int m = (b+t)/2;
b = m + 1;
}
return output;
}

How many times would the body of the while loop execute when the following statement is executed:

midString(“responsibility”);

(A) 3 (B) 4 (C) 5 (D) 6 (E) 7

61) Given the following method declaration, what value is returned as the result of the call mystery(5)?

public static int mystery(int n)


{
if (n == 0)
return 1;
else
return 3 * mystery (n - 1);
}

(A) 243
(B) 0
(C) 3
(D) 81
(E) 27

27
62) Consider a Monster class that has a default constructor. Suppose a list ArrayList<Monster>
minions is initialized. Which of the following will not cause an IndexOutOfBoundsException to be
thrown?

(A) for(int i = 0; i <= minions.size(); i++)


minions.set(i, new Monster());
(B) minions.add(minions.size(), new Monster());
(C) Monster m = minions.get(minions.size());
(D) Monster m = minions.remove(minions.size());
(E) minions.add(-1, new Monster());

63) Consider the following method.

//*Precondition: num > 0 *//


public static int doWhat(int num)
{
int var = 0;
for(int loop = 1; loop <= num; loop = loop + 2)
{
var += loop;
}
return var;
}

Which of the following best describes the value returned from a call to doWhat?

(A) num
(B) The sum of all integers between 1 and num inclusive.
(C) The sum of all even integers between 1 and num inclusive.
(D) The sum of all odd integers between 1 and num, inclusive.
(E) No value is returned because of an infinite loop.

28
64) Consider the following method. What is the output from conditionTest(-3,2)?

public static void conditionTest(int num1, int num2)


{
if ((num1 > 0) && (num2 > 0))
{
if (num1 > num2)
System.out.println("A");
else
System.out.println("B");
}
else if ((num2 < 0) && (num1 < 0))
{
System.out.println("C");
}
else if (num2 < 0)
{
System.out.println("D");
}
else
{
System.out.println("E");
}
}

(A) A (B) B (C) C (D) D (E) E

29
65) Consider the following method

public ArrayList<Integer> mystery(int n)


{
ArrayList<Integer> myList = new ArrayList<Integer>();
int num = 0;
for(int i = 0; i <= n; i++)
{
num = i * i + 3;
myList.add(num);
}

return myList;
}

Which of the following is printed as a result of executing the following statement?

System.out.println(mystery(6));

(A) [3, 4, 7, 12, 19, 28]


(B) [3, 4, 7, 12, 19, 28, 39]
(C) [4, 7, 12, 19, 28, 39]
(D) [39, 28, 19, 12, 7, 4]
(E) [39, 28, 19, 12, 7, 4, 3]

66) Consider an array defined as

String colors[] = {“red”, “blue”, “green”, “yellow”};

Which code segment would print:


red blue green yellow

I. for(String c: colors)
System.out.print(c + “ “);

II. for(int j = 0; j < colors.length; j++)


System.out.print(colors[j] + “ “);

III. System.out.print(colors);

IV. for(String c: colors)


System.out.print(colors[c] + “ “);

(A) I only
(B) II only
(C) I and II only
(D) II and IV only
(E) III and IV only

30
67) Which of the following creates an array correctly?

(A) String array = new String[10];


(B) int[10] array = new int[10];
(C) int array = new array[10];
(D) String[] array = new String[];
(E) double[] array = new double[10];

68) What is the output of the following code fragment?

int[] numbers = {2, 4, 6, 8, 10, 1, 3, 5, 7, 9};

for (int index= 0 ; index < 5 ; index++)


System.out.print(numbers[index] + " ");

(A) 2 4 6 8
(B) 2 4 6 8 10
(C) 2 4 6 8 10 1
(D) 2 4 6 8 10 1 3 5 7 9
(E) Nothing - there is a compiler error.

69) The following shuffle algorithm is used to shuffle an array of int values, nums.

public void shuffle() {


for(int k = nums.length -1; k > 0; k--) {
int rand = (int) (Math.random() * (k+1));
int temp = nums[k];
nums[k] = nums[rand];
nums[rand] = temp;
}
}

Suppose the initial state of nums is 8, 7, 6, 5, 4, and when the method is executed, the values generated for
rand are 3, 2, 0, 0 in that order. What element will be contained in nums[2] after execution?

(A) 8
(B) 7
(C) 6
(D) 5
(E) 4

31
70) Refer to the following code segment.

int arr[] = {1, 2, 3, 4, 5};


int arr2[] = new int[arr.length/2];
int k = 0;
for(int i = 0; i<arr2.length; i++) {
arr2[i] = arr[k];
k+= 2;
}

What will be the result of this code executing?


(A) arr2 will contain the values 1, 3, 5
(B) arr2 will contain the values 1, 3
(C) arr2 will contain the value 1
(D) a compiler error will occur
(E) a runtime error will occur

71) What is returned from mystery when it is passed {10, 30, 30, 60}?

public static double mystery(int[] arr) {


double output = 0;
for (int i = 0; i < arr.length; i++){
output = output + arr[i];
}
return output / arr.length;
}

(A) 17.5
(B) 30.0
(C) 130
(D) 32
(E) 32.5

72) Given the following method declaration, what will redo(82, 3) return?

public int redo(int i, int j){


if (i==0)
return 0;
else
return redo(i/j, j)+1;
}

(A) 5 (B) 4 (C) 6 (D) 7


(E) The method will never return anything due to infinite recursion.
32
73) Given the following field and method declaration, what is the value in a[1] after myMethod(a) is run?

int[] a = {7, 3, -1};

public static int myMethod(int[] a)


{
a[1]--;
return (a[1] * 2);
}

(A) 4
(B) 2
(C) 12
(D) 6
(E) 3

74) What is the values in the array numbers after the code is executed?

int[] numbers = {2, 4, 6, 8, 10};


numbers[4] = 5;
numbers[2] += 2;
numbers[3] = numbers[0] + numbers[2];

(A) [2, 4, 8, 10, 5]


(B) [4, 5, 10, 8, 10]
(C) [2, 4, 6, 10, 10]
(D) [4, 2, 8, 6, 10]
(E) [2, 5, 8, 10, 10]

75) What is the output of the following code?

int[] A = new int[6];

for (int i = 0; i < A.length; i++){


A[i] = i * i;
}
for (int i = 0; i < A.length; i++){
System.out.print(A[i]+ “ “);
}

(A) 0 1 2 3 4 5
(B) 0 1 4 9 16 25
(C) 1 4 6 8 10 12
(D) 1 2 3 4 5 6
(E) There will be a compiler error because i is declared twice.

33
76) Consider the following method:

public static int[] what(int[] A) {


int n = A.length;
int[] temp = new int[n] ;
for(int i=0; i<n; i++)
{
temp[i] = A[n-i-1];
}
return temp;
}

What is the purpose of this method?


(A) To make an array that is a copy of the array A
(B) To make an array that is a copy of the array A in reverse order
(C) To make an array that is a scrambled copy of the array A
(D) To make an array that is filled with the negatives of the array A
(E) To make an array of random integers

77) What is the value of a[1] after the following code is executed?

int[] a = {0, 2, 4, 1, 3};


for (int i = 0; i < a.length; i++)
a[i] = a[(a[i] + 3) % a.length];

(A) 0
(B) 1
(C) 2
(D) 3
(E) 4

34
78) Consider this inheritance hierarchy, in which Werewolf and Vampire are subclasses of Monster.

Monster

Werewolf Vampire

Which of the following is a false statement about the classes shown?

(A) The Vampire class can have private instance variables that are in neither Monster or Werewolf.

(B) Each of the classes – Monster, Werewolf, Vampire – can have a method growl, whose code in
Monster and Werewolf is identical, but different from the code in Vampire.

(C) If the Monster class has private instance variables age and name, then Werewolf and Vampire
cannot directly access them.

(D) Both Werewolf and Vampire inherit constructors from Monster.

(E) If the Monster class has a private method called wreakHavoc, this method may not be accessed in either
the Werewolf or Vampire classes.

79) The following method is intended to return the smallest int value in an array called arr. Assume that is at
least one value in arr.

public static int findMin(int[] arr) {


int min = arr[0];
int i = 0;
while(i < arr.length){
i++;
if(arr[i] < min)
min = arr[i];
}
return min;
}

Which is true about this method:

(A) The method always works correctly.


(B) The method never works correctly.
(C) The method works correctly only when there is exactly one value in arr.
(D) The method works correctly only when arr contain more than one value.
(E) The method works correctly only when arr.length is even.

35
80) The method fun is defined as:

public static int fun(int[] v)


{
v[0]--;
return (v[0] + 2);
}

What is the value of v[0] after the following code segment is executed?

int[] v = {3, 4, 5};


v[0] = fun(v);

(A) 2
(B) 3
(C) 4
(D) 5
(E) Compiler Error

81) What output will be produced by the following code?

String[] names = {“Kai”, “Emma”, “Gavin”, “Greg”};


for(String x : names)
{
String y = x.substring(0, 1);
System.out.print(y + “ “);
}

(A) Nothing, there is an error in the code.


(B) Ka Em Ga Gr
(C) Kai Emma Gavin Greg
(D) Kai
(E) K E G G

82) Which of the following correctly initializes an array arr to contain four elements each with value 0?

I. int[] arr = {0, 0, 0, 0};

II. int[] arr = new int[4];

III. int[] arr = new int[4];


for(int x : arr)
arr[x] = 0;

A) I only B) II only C) I, II D) I, III E) I, II, III

36
83) What is returned by calling method(8,3)?

public static int method(int x, int y)


{
if (x == y)
return 0;
else
return method(x-1, y) + 1;
}

(A) 4
(B) 5
(C) 0
(D) 1
(E) Nothing, there is a run time error.

84) When designing a class hierarchy, which of the following is NOT true about the parent class?

(A) The parent class should hold any variables and methods common to all of the child classes.
(B) The parent class’ variables should be made public so the child class can access them.
(C) It is possible for a parent class to have a parent class itself.
(D) The parent class cannot access any of the methods in the child classes.
(E) The child class will have access to the parent class’ public methods and constructor via the super
keyword.

37
85) The following incomplete method is intended to reverse the array parameter.

public void reverse(int a[])


{
int temp [] = new int [a.length];

for (int i =0; i < a.length; i++)


temp [i] = a[/*missing code */];

for (int i =0; i < a.length; i++)


a[i] = temp[i];
}

Which of the following could be used to replace /*missing code */ so that executing the code would
reverse the array?

(A) i
(B) a.length - i - 1
(C) a.length - 1
(D) a.length – i
(E) a.length + 1

86) The Boolean expression (A&&B)||A is true

(A) only when A is true.


(B) only when B is true.
(C) whenever either A is true or B is true.
(D) only whenever both A is true and B is true.
(E) for all values of A and B.

87) A class School and its subclass ThirdGrade both have a method calculateGrade(). If arya
refers to an object of type ThirdGrade, what will the following code do?

arya.calculateGrade();

(A) The calculateGrade() method defined in ThirdGrade will be called.


(B) The calculateGrade() method defined in School will be called.
(C) There will be a compiler error saying that calculateGrade() has been defined twice.
(D) Overloading will be used to pick which calculateGrade() is called.
(E) None of the above will be correct.

38
88) A sorted list of 200 integers is to be searched to determine whether the value of 150 is in the list. What is
the most efficient sorting algorithm to use in this case and what is the maximum number of elements that would
be examined?

(A) Linear Search, 12 times


(B) Linear Search, 150 times
(C) Binary Search, 8 times
(D) Binary Search, 12 times
(E) Binary Search, 100 times

89) Which of the following correctly initializes an array arr to contain 5 elements each with a value of zero?

I. double[] arr = new int[5];


II. double arr[] = {0, 0, 0, 0};
III. double arr[] = new double[5];
for(int i =0; i < arr.length; i++)
arr[i] = 0.0;

(A) I only
(B) II only
(C) III only
(D) II and III only
(E) I, II, and III

90) Consider the following declarations in a client class:

ClassA x = new ClassA();


ClassA y = new ClassB();

method2() is a non-static method in ClassA. Which of the following method calls will cause an error?

I. x.method2();
II. y.method2();
III. ((ClassB) x).method2();

(A) I only
(B) II only
(C) III only
(D) I and III only
(E) I, II, and III

39
91) What is the output of the following code segment?

String str1 = “Happy ”;


String str2 = str1;
str2 += “New Year! ”;
str1 = str2.substring(6);
System.out.println(str1 + str2)

(A) Happy New Year!


(B) Happy Happy New Year!
(C) New Year! New Year!
(D) New Year! Happy New Year!
(E) Happy New Year! Happy New Year!

92) Consider the following method.

public static int[] operation(int[][] matrix, int r, int c){


int[] result = new int[matrix.length];

for(int j = 0; j < matrix.length; j++)


{
result[j] = matrix[r][j] * matrix[j][c];
}
return result;
}

The following code segment appears in another method in the same class.

int[][] mat = { {3, 2, 1, 4}, {1, 2, 3, 4}, {2, 2, 1, 2}, {1, 1, 1, 1} };

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, 4}
(B) {1, 6, 3, 4}
(C) {4, 3, 6, 1}
(D) {4, 4, 2, 2}
(E) {2, 2, 4, 4}

40
93) Consider the following code segment:

int x;
x = 53;
if(x>10)
System.out.print(“A”);
if(x>30)
System.out.print(“B”);
if(x>40)
System.out.print(“C”);
if(x>50)
System.out.print(“D”);
if(x>70)
System.out.print(“E”);

What is the output when the code is executed?

(A) A
(B) D
(C) ABD
(D) ABCD
(E) ABCDE

94) Examine the following method.

public static void doSomething(int value) {


if(value > 0 && value < 10)
{
doSomething(value – 1);
doSomething(value + 1);
System.out.print(" " + value);
}
}

Which of the following will be printed as a result of the call doSomething(4)?

(A) 4 3 2 1 5 6 7 8 9
(B) 4 3 5 2 6 1 7 8 9
(C) 9 8 7 6 5 1 2 3 4
(D) 9 8 7 1 6 2 5 3 4
(E) Nothing will be printed due to an infinite recursion

41
95) Which of the following statements shows correct syntax to create an object of a Piggy class?

(A) tom = new Piggy;


(B) Piggy new tom = Piggy();
(C) Piggy = new tom();
(D) Piggy tom = new Piggy();
(E) Tom tom = new Piggy();

96) Which features can you use to recognize constructor methods in a class declaration?

I. The constructor identifier is the same as the class identifier.


II. Constructors use both the public and the static keywords.
III. Constructors are neither void methods nor return methods.

A) I only B) II only C) I & III only D) II & III only E) I, II & III

97) Consider the following incomplete class definition:

public class SomeClass


{
private String myName;

//postcondition: returns myName


public String getName()
{ /* implementation not shown */ }

//postcondition: myName == name


public void setName(String name)
{ /* implementation not shown */ }

//...constructors, other methods


}

42
Now consider the following code in a separate class.

SomeClass x = new SomeClass(“Jessica”);


SomeClass y = new SomeClass(“Julia”);

Which of the following code segments can go after the code above to switch the names of the objects?

I.
SomeClass temp = new SomeClass();
temp = x;
x = y;
y = temp;

II.
String temp;
temp = x.myName;
x.myName = y.myName;
y.myName = temp;

III.
String temp;
temp = x.getName();
x.setName(y.getName());
y.setName(temp);

A) I only B) III only C) I and III D) II and III E) I, II, and III

98) Declare and construct an ArrayList that hold integers and has the reference name numbers.

(A) numbers(integer) = new ArrayList();


(B) ArrayList numbers[] = new ArrayList();
(C) ArrayList[int] numbers = new ArrayList(int);
(D) ArrayList<Integer> numbers = new ArrayList<Integer>();
(E) ArrayList<Integer> numbers = ArrayList(numbers);

43
99) Assume the following variable declarations have been made:

double r = Math.random();
int c;

Which of the following assigns a value to c that is between -10 <= c < 10?

(A) (int)(r * (-10) - 20)


(B) (int)(r * 21 - 11)
(C) r * 21 - 11
(D) (int)r * 21 - 11
(E) (int)(r * 20 - 10)

100) Consider the following code:

String s= "ABCDEFG";
System.out.println(s.substring(s.length()/2, s.length()));

What is the output?

(A) ABCD
(B) ABCDEFG
(C) DEFG
(D) DEF
(E) EFG

44
101) Consider the following class declarations.

public class Appliance


{
private boolean on;
public Appliance()
{
on = true;
}
public Appliance(boolean o)
{
on = o;
}
}

public class Toaster extends Appliance


{
public Toaster(boolean t)
{
super(t);
}
}

Which of the following statements will NOT compile?

(A) Toaster tl = new Toaster(false);


(B) Toaster t2 = new Toaster();
(C) Appliance t3 = new Toaster(true);
(D) Appliance t4 = new Appliance();
(E) Appliance t = new Appliance(false);

45
102) Examine the following code:

ArrayList<String> list = new ArrayList<String>() ;


list.add("Kevin");
list.add("Eric");
list.add("Brenden");
list.add("Luis");
list.add("Ethan");

Which of the following will replace the element "Brenden" with "Brandon" ?

(A) list[2] = "Brandon" ;


(B) list.set("Brenden", "Brandon");
(C) list.add("Brandon", list.indexOf("Brenden"));
(D) list.set(list.indexOf("Brenden"), "Brandon");
(E) list.add(“Brandon”, 2);

103) Examine the following code:

ArrayList<String> list = new ArrayList<String>() ;


list.add("Anthony");
list.add("Betty");
list.add("Cindy");
list.add("Dankia");
list.add("Emmarie");

Which of the following correctly prints the contents of the list?

(A) for(String list : name){


System.out.println(name);
}
(B) for(String name : list){
System.out.println(name);
}
(C) for(int name : String){
System.out.println(name);
}
(D) for(ArrayList name : String){
System.out.println(list);
}
(E) for(list name : name){
System.out.println(String);
}

46
104) Consider the following code segment. What value is in sum after this code executes?

int[][] matrix = {{1,1,2,2},{1,2,2,4},{1,2,3,4},{1,4,1,2}};

int sum = 0;
int col = matrix[0].length - 2;
for (int row = 0; row < 4; row++) {
sum = sum + matrix[row][col];
}

(A) 4
(B) 8
(C) 9
(D) 12
(E) 10

105) What would be printed from the following code?


int t = 0;
for(int r = 5; r >= 1; r--)
{
for(int c = 0; c <= 4; c++)
t++;
}
System.out.println(t);

(A) 12
(B) 16
(C) 20
(D) 23
(E) 25

106) Which of the following is a reason to use an ArrayList instead of an array?

(A) An ArrayList can grow or shrink as needed, while an array is always the same size.
(B) You can use a for-each loop on an ArrayList, but not in an array.
(C) You can store objects in an ArrayList, but not in an array.
(D) Primitive types are easier to manipulate with the wrappers in ArrayList.
(E) There is no benefit to use either; they work in the exact same way.

47
Use the following classes for questions 107 – 109.

public class Student {


private int ID;
private ContactInfo cInfo;
private Grades[] grd;

//Constructors not shown

public int getID() {


return ID;
}
public ContactInfo getInfo() {
return cInfo;
}
public Grades[] getGrades() {
return grd;
}
}
public class ContactInfo {
private String name;
private String guardian;
private String address;

//Constructors not shown

public String getName(){


return name;
}
public String getGuardian(){
return guardian;
}
public String getAdd() {
return address;
}
}
public class Grades {
private String assignment;
private int grade;

//Constructors not shown

public String getAssignment(){


return assignment;
}
public int getGrade() {
return grade;
}
}

48
107) A client method has this declaration followed by the code to initialize the classList;

Student[] classList = new Student[100];

Here is a code segment to generate a list of student ids only:

for(Student s : classList)
/*line of code*/

Which is a correct /*line of code*/?

(A) System.out.println(s[i].getID());
(B) System.out.println(Student[i].getID());
(C) System.out.println(Student.getID());
(D) System.out.println(s.getID());
(E) System.out.println(s.ID);

108) A client method has this declaration followed by the code to initialize the classList.

Student[] classList = new Student[100];

Here is a code segment to generate a list of student names and addresses only:

for(Student s : classList)
/*line of code*/

Which is a correct /*line of code*/?

(A) System.out.println(s[i].getInfo().getName()+” “+s[i].getInfo().getAdd());


(B) System.out.println(s.getName()+” “+s.getAdd());
(C) System.out.println(Student.getInfo().getName()+” “+Student.getInfo().getAdd());
(D) System.out.println(s.getInfo().getName()+” “+s.getInfo().getAdd());
(E) System.out.println(s.name+” “+s.address);

49
109) Here is a method in the Student class intended to return the highest grade for this Student. Assume
each Student has at least 1 grade.

public int getHighest() {


/*method body*/
}

Which of the following would replace /*method body*/ so that the method works as intended?

I. int max = Grades[0].getGrade();


for(int g: Grades) {
if(g>max)
max = g;
}
return max;

II. int max = grd[0].getGrade();


for(Grades g: grd) {
if(g.getGrade() > max)
return g.getGrade();
}
return max;

III. int max = grd[0].getGrade();


for(Grades g: grd) {
if(g.getGrade() > max)
rmax = g.getGrade();
}
return max;

(A) I only
(B) II only
(C) III only
(D) II and III only
(E) I and III only

50
Use the following classes to answer questions 110 - 111.

public class Employee{


private String myName;
private int myEmployeeNum;
private double mySalary, myTaxWithheld;

public Employee(String name, int empNum, double salary, double taxWithheld)


{ /* implementation not shown */ }

//Returns pre-tax salary


public double getSalary()
{ return mySalary; }

public String getName()


{ return myName; }

public int getEmployeeNum()


{ return myEmployeeNum; }

public double getTax()


{ return myTaxwithheld; }

public double computePay()


{ return mySalary - myTaxWithheld; }
}

public class PartTimeEmployee extends Employee {


private double myPayFraction;

public PartTimeEmployee(String name, int empNum, double salary,


double taxWithheld, double payFraction)
{ /* implementation not shown */ }

public double getPayFraction()


{ return myPayFraction; }

public double computePay()


{ return getSalary() * myPayFraction – getTax();}
}

public class Consultant extends Employee {


private static final double BONUS = 5000;

public Consultant(String name, int empNum, double salary, double taxWithheld)


{ /* implementation not shown */ }

public double computePay()


{ /* implementation code */ }
}
51
110) The computePay method in the Consultant class redefines the computePay method of the
Employee class to add a bonus to the salary after subtracting the tax withheld. Which represents correct
/*implementation code */ of computePay for Consultant?

I. return super.computePay() + BONUS;

II. super.computePay();
return getSalary() + BONUS;

III. return getSalary() - getTax() + BONUS;

(A) I only
(B) II only
(C) III only
(D) I and III only
(E) I and II only

111) Consider these valid declarations in a client program:

Employee e = new Employee(”Albert Einstein”, 304, 65000, 10000);


Employee p = new PartTimeEmployee(”Isaac Newton”, 287, 40000, 7000, 0.8);
Employee c = new Consultant(”Grace Hopper”, 694, 55000, 8500);

Which of the following method calls will cause an error?

(A) double x = e.getPayFraction();


(B) double y = p.computePay();
(C) String n = c.getName();
(D) int num = p.getEmployeeNum();
(E) double g = p.getPayFraction();

52
112) Consider an array arr that is initialized with int values. The following code fragment stores in count the
number of positive values in arr.

int count = 0, index = 0;


while (index < arr.length) {
if (arr[index] > 0)
count++;
index++;
}

Which of the following code fragments is equivalent to the above segment?

I. int count = 0;
for (int num : arr){
if (arr[num] > 0)
count++;
}

II. int count = 0;


for (int num : arr){
if (num > 0)
count++;
}

III. int count = 0;


for (int i = 0; i < arr.length; i++){
if (arr[i] > 0)
count++;
}

(A) I only
(B) II only
(C) III only
(D) II and III only
(E) I and III only

53
113) A square matrix is declared as

int[][] mat = new int[SIZE][SIZE];

where SIZE is an appropriate integer constant. Consider the following method:

public static void mystery(int[][] mat, int value, int top, int left, int bottom, int right)
{
for (int i = left; i <= right; i++){
mat [top][i] = value;
mat [bottom][i] = value;
}
for (int i = top + 1; i <= bottom — 1; i++){
mat[i][left] = value;
mat[i][right] = value;
}
}

Assuming that there are no out-of-range errors, which best describes what method mystery does?

(A) Places value in corners of the rectangle with corners (top, left) and (bottom, right).
(B) Places value in the diagonals of the square with corners (top, left) and (bottom, right).
(C) Places value in each element of the rectangle with corners (top, left) and (bottom, right).
(D) Places value in each element of the border of the rectangle with corners (top, left) and (bottom, right).
(E) Places value in the topmost and bottommost rows of the rectangle with corners (top, left) and (bottom, right).

114) Examine the following method

public int t(int n) {


if( n == 1 || n ==2)
return 2 * n;
else
return t(n - 1) - t(n - 2);
}

What will be returned by t(5)?

(A) 4
(B) 2
(C) 0
(D) -2
(E) -4

54
115) Consider the following method:

public int doStuff(int x)


{
int val = 0;
for (int i = 2; i < x; i += 2)
{
for (int j = 2; j < x; j += 2)
{
val = val + i + j;
}
}
return val;
}

What value is returned by the call doStuff(5)?

(A) 16
(B) 18
(C) 24
(D) 32
(E) 72

116) Consider the following recursive method.

public String recur(int n, int b)


{
String oct = " " + n % b;
if (n / b > 0)
return recur (n/b, b) + oct;
return oct;
}

What is printed as a results of executing the following statement?

System.out.println(recur (66, 5));

(A) 1 1
(B) 2 3
(C) 2
(D) 1 3 2
(E) 2 3 1

55
117) Consider the following code segment:

int x = 4;
int y = 142;
while (x <= y)
{
int m = Math.abs(x - y);
y = y/x;
System.out.print(m + " ");
}

What is the output of the code segment above?

(A) 2
(B) 4
(C) 138 31 4
(D) 35 8 2
(E) 138

118) What is printed as a result of executing the following code segment?

ArrayList<Integer> list = new ArrayList<Integer>();


list.add(1);
list.add(2);
list.add(3);
list.set(2, 4);
list.add(2, 5);
list.add(6);
for(int value : list) {
System.out.println(value);
}

(A) [1, 2, 3, 4, 5]
(B) [1, 2, 4, 5, 6]
(C) [1, 2, 5, 4, 6]
(D) [1, 5, 2, 4, 6]
(E) [1, 5. 4, 2, 6]

56
119) Consider the code segment

String[] names = new String[10];

and that further statements (not shown) have put String references into some of the array cells.

Which of the following fragments prints out the cells of the array from last to first, skipping cells that contain
null?

(A) for(int j = 0; j < names.length; j++){


if (names[j] != null)
System.out.println(names[j]);
}

(B) for(int j = names.length; j < names.length; j++){


if (names[j] != null)
System.out.println(names[j]);
}

(C) for(int j = names.length-1; j >= 0; j--){


if (names[j] != null)
System.out.println(names[j]);
}

(D) for(int j = names.length; j >= 0; j++){


if (names[j] != null)
System.out.println(names[j]);
}

(E) for(int j=0; j<names.length; j++){


if(names[names.length-j] != null)
System.out.println(names[names.length-j]);
}

120) Assume that x and y have been defined and initialized as int values. The expression

!(!(a != b) && (b > 7))

is equivalent to which of the following?

(A) (a != b) || (b < 7)
(B) (a != b) || (b <= 7)
(C) (a == b) || (b <= 7)
(D) (a != b) && (b <= 7)
(E) (a == b) && (b > 7)

57

You might also like