DIE SLOWLY
DO NOT TRY
I think I can
I am a Pro
Don't be a menace
100

1.    Consider the following code segment.


for (int k = 0; k < 20; k = k + 2)

{

if (k % 3 == 1)

{

System.out.print(k + " ");

}

}


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


(a)    4    16            

(B)    4    10    16        

(c)    0    6    12    18    

(D)    1    4    7    10 13    16    19        

(E)    0    2    4    6 8    10    12    14 16    18


4    10    16  

100

2.    Consider the following code segment.

List<String> animals = new ArrayList<String>();


animals.add("dog");

animals.add("cat"); animals.add("snake"); animals.set(2, "lizard"); animals.add(1, "fish"); animals.remove(3); System.out.println(animals);


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

(a)    [dog, fish, cat]

(B)    [dog, fish, lizard]

(c)    [dog, lizard, fish]

(D)    [fish, dog, cat]

(E)    The code throws an ArrayIndexOutOfBoundsException exception.


(a)    [dog, fish, cat]

100

3.    Consider the following method.


public static void mystery(List<Integer> nums)

{

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

{

if (nums.get(k).intValue() == 0)

{

nums.remove(k);

}

}

}

Assume that a List<Integer> values initially contains the following

Integer values.

[0, 0, 4, 2, 5, 0, 3, 0]


What will  values contain as a result of executing  mystery(values) ?


(a)    [0,    0,    4,    2,    5,    0,    3,    0]

(B)    [4,    2,    5,    3]                

(c)    [0,    0,    0,    0,    4,    2,    5,    3]

(D)    [0,    4,    2,    5,    3]            

(E)   The code throws an   ArrayIndexOutOfBoundsException exception.


(D)    [0,    4,    2,    5,    3]    

100

4.    At a certain high school students receive letter grades based on the following scale.

Integer Score    Letter Grade 93 or above        A

From 84 to 92 inclusive    B

From 75 to 83 inclusive    C

Below 75    F


Which of the following code segments will assign the correct string to grade

for a given integer score ?

I.    if (score >= 93) grade = "A";

if (score >= 84 && score <= 92) grade = "B";

if (score >= 75 && score <= 83) grade = "C";

if (score < 75) grade = "F";

II.    if (score >= 93) grade = "A";

if (84 <= score <= 92)

grade = "B";

if (75 <= score <= 83) grade = "C";

if (score < 75) grade = "F";

III.    if (score >= 93) grade = "A";

else if (score >= 84) grade = "B";

else if (score >= 75) grade = "C";

else

grade = "F";


(a)    II only

(B) III only

(c) I and II only

(D)    I and III only

(E)    I, II, and III


(D)    I and III only

100

5.    Consider the following output.

1 1 1 1 1

2 2 2 2

3 3 3

4 4

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 >= 1; 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();

}


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

{

for (int k = 5; k >= j; k--)

{

System.out.print(j + " ");

}

System.out.println();

}


200

6.    A car dealership needs a program to store information about the cars for sale. For each car, they want to keep track of the following information: number of doors (2 or 4), whether the car has air conditioning, and its average number of miles per gallon. Which of the following is the best object-oriented program design?

(a)    Use one class, Car, with three instance variables: int numDoors, boolean hasAir, and double milesPerGallon.

(B)    Use four unrelated classes: Car, Doors, AirConditioning, and

MilesPerGallon.

(C)    Use a class Car with three subclasses: Doors, AirConditioning, and

MilesPerGallon.

(D)    Use a class Car, with a subclass Doors, with a subclass

AirConditioning, with a subclass MilesPerGallon.

(E)    Use three classes:  Doors, AirConditioning, and MilesPerGallon,

each with a subclass Car.


(a)    Use one class, Car, with three instance variables: int numDoors, boolean hasAir, and double milesPerGallon.

200

7.    Consider the following declarations.

public interface Shape

{

int isLargerThan(Shape other);

// Other methods not shown

}

public class Circle implements Shape

{

// Other methods not shown

}


Which of the following method headings of isLargerThan can be added to the declaration of the Circle class so that it will satisfy the Shape interface?

I.    public int isLargerThan(Shape other)

II.    public int isLargerThan(Circle other)

III.    public boolean isLargerThan(Object other)

(a)  I only

(B)    II only

(C)    III only

(D)    I and II only

(E)    I, II, and III


(a)  I only

200

10.    Consider the following instance variable and method.

private int[] arr;


/** Precondition: arr contains no duplicates;

*    the elements in arr are in ascending order.

*    @param low an int value such that 0 < low < arr.length

*    @param high an int value such that low - 1 < high < arr.length

*    @param num an int value

*/

public int mystery(int low, int high, int num)

{

int mid = (low + high) / 2; if (low > high)

{

return low;

}

else if (arr[mid] < num)

{

return mystery(mid + 1, high, num);

}

else if (arr[mid] > num)

{

return mystery(low, mid − 1, num);

}

else // arr[mid] == num

{

return mid;

}

}


What is returned by the call mystery(0, arr.length − 1, num)?

(a)    The number of elements in arr that are less than num

(B)    The number of elements in arr that are less than or equal to num

(C)    The number of elements in arr that are equal to num

(D)    The number of elements in arr that are greater than num

(E)    The index of the middle element in arr


a.  The number of elements in arr that are less than num

200

13.    Consider the following instance variable and method.


private int[] numbers;


/** Precondition: numbers contains int values in no particular order.

*/

public int mystery(int num)

{

for (int k = numbers.length − 1; k >= 0; k−−)

{

if (numbers[k] < num)

{

return k;

}

}

return -1;

}


Which of the following best describes the contents of numbers after the following statement has been executed?


int m = mystery(n);


(a)    All values in positions 0 through m are less than n.

(B)    All values in positions m+1 through numbers.length-1 are less than n.

(C)    All values in positions m+1 through numbers.length-1 are greater than or equal to n.

(D)    The smallest value is at position m.

(E)    The largest value that is smaller than n is at position m.


(C)    All values in positions m+1 through numbers.length-1 are greater than or equal to n.

200

14.    Consider the following method.


/** @param x an int value such that x >= 0

*/

public void mystery(int x)

{

System.out.print(x % 10);

if ((x / 10) != 0)

{

mystery(x / 10);

}

System.out.print(x % 10);

}



Which of the following is printed as a result of the call mystery(1234)?


(a)    1234

(B)    4321

(C)    12344321

(D)    43211234

(E)    Many digits are printed due to infinite recursion.

 


(D)    43211234

300

15.    Consider the following two classes.


public class Dog

{

public void act()

{

System.out.print("run "); eat();

}

public void eat()

{

System.out.print("eat ");

}

}

public class UnderDog extends Dog

{

public void act()

{

super.act(); System.out.print("sleep ");

}

public void eat()

{

super.eat(); System.out.print("bark ");

}

}


Assume that the following declaration appears in a class other than Dog.


Dog fido = new UnderDog();


What is printed as a result of the call fido.act() ?

(a)    run eat

(B)    run eat sleep

(C)    run eat sleep bark

(D)    run eat bark sleep

(E)    Nothing is printed due to infinite recursion.


(D)    run eat bark sleep

300

16.    Consider the following recursive method.


public static int mystery(int n)

{

if (n <= 1)

{

return 0;

}

else

{

return 1 + mystery(n / 2);

}

}


Assuming that k is a nonnegative integer and m = 2k, what value is returned as a result of the call mystery(m) ?

(a)    0

(B)    k

(C)    m

(D)    m  1

(E)     k  1 2


(B)    k

300

17.    Consider the following instance variable and method.


private int[] array;


/** Precondition: array.length > 0

*/

public int checkArray()

{

int loc = array.length / 2;

for (int k = 0; k < array.length; k++)

{

if (array[k] > array[loc])

{

loc = k;

}

}

return loc;

}


Which of the following is the best postcondition for checkArray ?

(a)    Returns the index of the first element in array array whose value is greater than array[loc]

(B)    Returns the index of the last element in array array whose value is greater than array[loc]

(C)    Returns the largest value in array array

(D)    Returns the index of the largest value in array array

(E)    Returns the index of the largest value in the second half of array array


(D)    Returns the index of the largest value in array array

300

18.    Consider the following methods.


public void changer(String x, int y)

{

x = x + "peace"; y = y * 2;

}


public void test()

{

String s = "world"; int n = 6; changer(s, n);


/* End of method */

}


When the call test() is executed, what are the values of s and n at the point indicated by /* End of method */ ?



         s                 n

(a)    world           6

(B)    worldpeace    6

(C)    world            12

(D)    worldpeace    12

(E)    peace            12

 


(a)    world           6

300

19.    Consider the following code segment.


int[][] mat = new int[3][4];

for (int row = 0; row < mat.length; row++)

{

for (int col = 0; col < mat[0].length; col++)

{

if (row < col)

{

mat[row][col] = 1;

}

else if (row == col)

{

mat[row][col] = 2;

}

else

{

mat[row][col] = 3;

}

}

}


What are the contents of mat after the code segment has been executed?


(a)    {{2,    1,    1},    

    {3,    2,    1},    

    {3,    3,    2},    

    {3,    3,    3}}    

(B)    {{2,    3,    3},    

    {1,    2,    3},    

    {1,    1,    2},    

    {1,    1,    1}}    

(C)    {{2,    3,    3,    3},

    {1,    2,    3,    3},

    {1,    1,    2,    3}}

(D)    {{2,    1,    1,    1},

    {3,    2,    1,    1},

    {3,    3,    2,    1}}

(E)    {{1,    1,    1,    1},

    {2,    2,    2,    2},

    {3,    3,    3,    3}}

 


(D)    {{2,    1,    1,    1},

    {3,    2,    1,    1},

    {3,    3,    2,    1}}

500

20.    Consider the following method.


/** Precondition: arr contains only positive values.

*/

public static void doSome(int[] arr, int lim)

{

int v = 0; int k = 0;

while (k < arr.length && arr[k] < lim)

{

if (arr[k] > v)

{

v = arr[k]; /* Statement S */

}

k++; /* Statement T */

}

}


Assume that doSome is called and executes without error. Which of the following are possible combinations for the value of lim, the number of times Statement S is executed, and the number of times Statement T is executed?


Value of

lim    Executions of

Statement S    Executions of

Statement T

I.    5    0    5

II.    7    4    9

III.    3    5    2


(a)    I only

(B)    II only

(C)    III only

(D)    I and III only

(E)    II and III only


(B)    II only

500

 refer to the following information.


Consider the following instance variable nums and method findLongest with line numbers added for reference. Method findLongest is intended to find the longest consecutive block of the value target occurring in the array nums; however, findLongest does not work as intended.


For example, if the array nums contains the values [7, 10, 10, 15, 15, 15, 15, 10,

10, 10, 15, 10, 10], the call findLongest(10) should return 3, the length of the longest consecutive block of 10s.


private int[] nums;


public int findLongest(int target)

{

int lenCount = 0; int maxLen = 0;


Line 1:    for (int val : nums)

Line 2:    {

Line 3:    if (val == target)

Line 4:    {

Line 5:    lenCount++;

Line 6:    }

Line 7:    else

Line 8:    {

Line 9:    if (lenCount > maxLen)

Line 10:    {

Line 11:    maxLen = lenCount;

Line 12:    }

Line 13:    }

Line 14: }

Line 15: if (lenCount > maxLen)

Line 16: {

Line 17:    maxLen = lenCount;

Line 18: }

Line 19: return maxLen;

}

11.    The method findLongest does not work as intended. Which of the following best describes the value returned by a call to findLongest ?

(a)    It is the length of the shortest consecutive block of the value target

in nums.

(B)    It is the length of the array nums.

(C)    It is the number of occurrences of the value target in nums.

(D)    It is the length of the first consecutive block of the value target in nums.

(E)    It is the length of the last consecutive block of the value target in nums.


c. It is the number of occurrences of the value target in nums.

500

 refer to the following information.


Consider the following instance variable nums and method findLongest with line numbers added for reference. Method findLongest is intended to find the longest consecutive block of the value target occurring in the array nums; however, findLongest does not work as intended.


For example, if the array nums contains the values [7, 10, 10, 15, 15, 15, 15, 10,

10, 10, 15, 10, 10], the call findLongest(10) should return 3, the length of the longest consecutive block of 10s.


private int[] nums;


public int findLongest(int target)

{

int lenCount = 0; int maxLen = 0;


Line 1:    for (int val : nums)

Line 2:    {

Line 3:    if (val == target)

Line 4:    {

Line 5:    lenCount++;

Line 6:    }

Line 7:    else

Line 8:    {

Line 9:    if (lenCount > maxLen)

Line 10:    {

Line 11:    maxLen = lenCount;

Line 12:    }

Line 13:    }

Line 14: }

Line 15: if (lenCount > maxLen)

Line 16: {

Line 17:    maxLen = lenCount;

Line 18: }

Line 19: return maxLen;

}


12.    Which of the following changes should be made so that method findLongest will work as intended?

(a)    Insert the statement lenCount = 0;

between lines 2 and 3.

(B)    Insert the statement lenCount = 0;

between lines 8 and 9.

(C)    Insert the statement lenCount = 0;

between lines 10 and 11.

(D)    Insert the statement lenCount = 0;

between lines 11 and 12.

(E)    Insert the statement lenCount = 0;

between lines 12 and 13.


(E)    Insert the statement lenCount = 0;

between lines 12 and 13.

500

 refer to the following incomplete class declaration.


public class TimeRecord

{

private int hours;

private int minutes; // 0 < minutes < 60

/** Constructs a TimeRecord object.

*    @param h the number of hours

*    Precondition: h > 0

*    @param m the number of minutes

*    Precondition: 0 < m < 60

*/

public TimeRecord(int h, int m)

{

hours = h; minutes = m;

}


/** @return the number of hours

*/

public int getHours()

{ /* implementation not shown */ }


/** @return the number of minutes

*    Postcondition: 0 < minutes < 60

*/

public int getMinutes()

{ /* implementation not shown */ }


/** Adds h hours and m minutes to this TimeRecord.

*    @param h the number of hours

*    Precondition: h > 0

*    @param m the number of minutes

*    Precondition: m > 0

*/

public void advance(int h, int m)

{

hours = hours + h; minutes = minutes + m;

/* missing code */

}

// Other methods not shown

}

8.    Which of the following can be used to replace /* missing code */ so that advance

will correctly update the time?

(a)    minutes = minutes % 60;

(B)    minutes = minutes + hours % 60;

(C)    hours = hours + minutes / 60; minutes = minutes % 60;

(D)    hours = hours + minutes % 60; minutes = minutes / 60;

(E)    hours = hours + minutes / 60;


(C)    hours = hours + minutes / 60; minutes = minutes % 60;

500

 refer to the following incomplete class declaration.


public class TimeRecord

{

private int hours;

private int minutes; // 0 < minutes < 60

/** Constructs a TimeRecord object.

*    @param h the number of hours

*    Precondition: h > 0

*    @param m the number of minutes

*    Precondition: 0 < m < 60

*/

public TimeRecord(int h, int m)

{

hours = h; minutes = m;

}


/** @return the number of hours

*/

public int getHours()

{ /* implementation not shown */ }


/** @return the number of minutes

*    Postcondition: 0 < minutes < 60

*/

public int getMinutes()

{ /* implementation not shown */ }


/** Adds h hours and m minutes to this TimeRecord.

*    @param h the number of hours

*    Precondition: h > 0

*    @param m the number of minutes

*    Precondition: m > 0

*/

public void advance(int h, int m)

{

hours = hours + h; minutes = minutes + m;

/* missing code */

}

// Other methods not shown

}

9.    Consider the following declaration that appears in a class other than TimeRecord. TimeRecord[] timeCards = new TimeRecord[100];

Assume that timeCards has been initialized with TimeRecord objects. Consider the following code segment that is intended to compute the total of all the times stored in timeCards.

TimeRecord total = new TimeRecord(0,0);

for (int k = 0; k < timeCards.length; k++)

{

/* missing expression */ ;

}


Question: Which of the following can be used to replace /* missing expression */ so that the code segment will work as intended?

(a)    timeCards[k].advance()

(B)    total += timeCards[k].advance()

(C)    total.advance(timeCards[k].hours,

timeCards[k].minutes)

(D)    total.advance(timeCards[k].getHours(),

timeCards[k].getMinutes())

(E)    timeCards[k].advance(timeCards[k].getHours(),

timeCards[k].getMinutes())


(D)    total.advance(timeCards[k].getHours(),

timeCards[k].getMinutes())