0% found this document useful (0 votes)
4 views7 pages

Exam Review

The document presents a series of programming questions related to Java methods and class structures, focusing on array manipulation, maximum value finding, and a Mancala game implementation. It includes code snippets and multiple-choice questions for evaluating understanding of Java programming concepts. The questions cover topics such as method functionality, array indexing, and class constructors.

Uploaded by

ericzheng105
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)
4 views7 pages

Exam Review

The document presents a series of programming questions related to Java methods and class structures, focusing on array manipulation, maximum value finding, and a Mancala game implementation. It includes code snippets and multiple-choice questions for evaluating understanding of Java programming concepts. The questions cover topics such as method functionality, array indexing, and class constructors.

Uploaded by

ericzheng105
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
You are on page 1/ 7

1. Consider the following method.

public static void change(int[] arr)


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

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

int[] nums = {5, 3, 6, 8, 11, 2};


change(nums);
Which of the following represents the contents of nums as a result of executing the code segment? (remember that arrays are objects)
(A) {2, 5, 6, 8, 11, 3}
(B) {11, 3, 5, 6, 8, 2}
(C) {11, 3, 6, 8, 5, 2}
(D) {2, 3, 6, 8, 11, 5}
(E) {2, 3, 5, 6, 8, 11}

2. Consider the problem of finding the maximum value in an array of integers. The following code segments are proposed solutions to the
problem. Assume that the variable arr has been defined as an array of int values and has been initialized with one or more values.

I. int max = Integer.MIN_VALUE;


for (int value : arr)
{
if (max < value)
{ max = value; }
}

II. int max = 0;


boolean first = true;
for (int value : arr)
{
if (first)
{
max = value;
first = false;
}
else if (max < value)
{ max = value; }
}

III. int max = arr[0];


for (int k = 1; k < arr.length; k++)
{
if (max < arr[k])
{ max = arr[k]; }
}
Which of the code segments will always correctly assign the maximum element of the array to the variable max ?
(A)I only
(B)II only
(C)III only
(D)II and III only
(E)I, II, and III
Questions 3–4 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 Postconition: 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


}

3. Which of the following can be used to replace /* missing code */ so that the advance method will correctly update the time?

(A)
(B) minutes = minutes % 60;
(C) minutes = minutes + hours % 60;
(D) hours = hours + minutes /60;
minutes = minutes % 60;
(E) hours = hours + minutes % 60;
minutes = minutes / 60;
(F) hours = hours + minutes / 60;

4. 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 */; }

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())

5. 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 Executions of Executions of


lim Statement S 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

6. Consider the following code segment.

boolean[] oldVals = {true, false, true, true};


boolean[] newVals = new boolean[4];
for (int j = oldVals.length - 1; j >= 0; j--)
{
newVals[j] = !(oldVals[j]);
}
What, if anything, will be the contents of newVals as a result of executing the code segment?
(A) {true, true, false, true}
(B) {true, false, true, true}
(C) {false, true, false, false}
(D) {false, false, true, false}
(E) The array newVals will not contain any values because the code segment does not compile.

7. Consider the following code segment.

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


for(int j = 0; j < arr.length; j++)
{
System.out.print(arr[j] + j + " ");
}
What, if anything, is printed as a result of executing the code segment?
(A) 3 1 0 4 2
(B) 3 2 2 7 6
(C) 6 2 0 8 4
(D) 7 2 3 6 2
(E) Nothing is printed, because an ArrayIndexOutOfBoundsException is thrown.

8. Consider the code segment below, where arr is a one-dimensional array of integers.

int sum = 0;
for (int n : arr)
{
sum = sum + 2 * n;
}
System.out.print(sum);
Which of the following code segments will produce the same output as the code segment above?

(A) int sum = 0;


for (int k = 0; k < arr.length; k++)
{ sum = sum + 2 * k; }
System.out.print(sum);

(B) int sum = 0;


for (int k = 0; k <= arr.length; k++)
{ sum = sum + 2 * k; }
System.out.print(sum);

(C) int sum = 0;


for (int k = 1; k <= arr.length; k++)
{ sum = sum + 2 * k; }
System.out.print(sum);

(D) int sum = 0;


for (int k = 0; k < arr.length; k++)
{ sum = sum + 2 * arr[k]; }
System.out.print(sum);

(E) int sum = arr[0];


for (int k = 1; k <= arr.length; k++)
{ sum = sum + 2 * arr[k]; }
System.out.print(sum);

9. Assume that an array of integers values has been declared as follows and has been initialized.

int[] arr = new int[10];


Which of the following correctly interchanges the values of arr[0] and arr[5]?
(A)
(B) arr[0] = 5;
arr[5] = 0;

(C) arr[0] = arr[5];


arr[5] = arr[0];

(D) int k = arr[5];


arr[0] = arr[5];
arr[5] = k;

(E) int k = arr[0];


arr[0] = arr[5];
arr[5] = k;

(F) int k = arr[5];


arr[5] = arr[0];
arr[0] = arr[5];

Mancala is a board game for 2 players. A Mancala board has 12 small pits, called “houses” and 2 large pits,
called “stores”. At the beginning of the game, the same number of “seeds” (for example, 3) is placed in each
house; stores are left empty. Each player controls the six houses on her side of the board, and her store is to the
right of her houses.

On each round of the game, a player takes ALL the seeds from one of their houses and
“sows” them, moving along the board counterclockwise and placing one seed in each pit, including their
houses, their own store, and their opponent’s houses, but excluding the opponent’s store. If the move ends in
the payer’s own store, the player gets another turn.
For example:
The Java class Mancala is used in the computer implementation of the game. In this question you are asked to
write the constructor and one method of that class.

The board is represented as int[] board with 14 elements. The first player’s houses are represented by the
elements with the indices from 1 – 6 and her store has index 7. The second player’s houses are represented by
the elements with indices from 8 – 13 and her stores has index 0. A partial declaration of the Mancala class is
shown below.

public class Mancala


{
private int[] board;

/** sets board to proper values


*/
public Mancala(int n)
{ /** to be implemented in part (a) */ }

/** updates board for the move from the given pit
* @return true if the move ends in the player’s own store; otherwise false
* precondition: k != store1 && k != store2
*/
public boolean move(int k)
{ /** to be implemented in part (b) */ }

/** There may be other instance variables, constructor and methods that are not shown */
}

(a) Write the constructor for the Mancala class that takes one integer parameter n and initializes board,
placing n seeds in each house and leaving the stores empty.
/** sets board to proper values
*/
public Mancala(int n)
{

(b) As stated above, on each round of the game, a player takes ALL the seeds from one of their houses and
“sows” them, moving along the board counterclockwise and placing one seed in each pit, including their
houses, their own store, and their opponent’s houses, but excluding the opponent’s store. If the move
ends in the payer’s own store, the player gets another turn.

Write the method move that implements each round of the game beginning from the pit with index k. The
method should return true if the move end in the player’s own store; otherwise it should return false.

/** updates board for the move from the given pit
* @reutrn true if the nmove ends in the player’s own store; otherwise false
* precondition: k != store1 && k != store2
*/
public boolean move(int k)
{

You might also like