Exam Review
Exam Review
The following code segment appears in another method in the same class.
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.
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.
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.
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())
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?
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
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?
9. Assume that an array of integers values has been declared as follows and has been initialized.
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.
/** 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)
{