15-110 EXAM 2 PRACTICE QUESTIONS

NOTE: The sample questions below do not necessarily represent the exact questions you will receive on the exam, but they cover similar topics that you are expected to understand.

1. For each of the following code fragments, determine the value that will be output. (If no output will occur, explain why.)

(a)
int sum = 0;
for (int i = 1; i <= 15; i += 3) {
	sum += i;
}
System.out.println(sum);
(b)
int total = 5;
int y = 100;
while (total != y) {
	total *= 2;
}
System.out.println(total);
(c)
int result = 1;
do {
	result = result + result;
} while (result < 100);
System.out.println(result);
(d)
int product = 1;
int i = 1;
while (i > 1) {
	i++;
	product *= i;
}
System.out.println(product);

2. Consider the following code fragment in Java:

int product = 1;
for (int i = 1; i < 4; i++)
{
	int sum = 0;
	for (int j = 1; j <= i; j++)
	{
		sum += j;
	}
	product *= sum; 
} 
System.out.println(product); 

(a) Trace this nested loop carefully and determine the value of product that is output after this nested loop completes. Show your work by creating a table that shows the values for i, j, sum and product as the code executes.

(b) If the statement int sum = 0; is moved so that it is before the start of the first for loop, what is the output this time? will this yield the same answer as in (a)? Why or why not?

(c) Rewrite the original nested loop above using two while loops.

3. Let score be an array of integer values.

(a) Show how to create the array score so that it can hold 30 integers.

(b) Write a Java code fragment that initializes each cell of the array to a random integer from the set {5, 10, 15, 20, ..., 100} using the Math.random method.

(c) Write a Java code fragment that finds the maximum of all of the random numbers stored in the array.

4. The countTemps method requires two parameters, an array of doubles representing temperature measurements and a double representing a threshold temperature. This method is to return the number of temperatures in the array that are above the threshold temperature.

(a) Complete the method so it works with any array of temperatures and any threshold.

public static ___________ countTemps(double[] tempArray, double threshold)
{
	int count = 0;

	for (int i = ______; i < _____________; i++)

	{
		if (threshold _____ tempArray[i])

			count = ___________________;
	}
	
	return count;
}

(b) Complete the Java main method below so that it asks the user for the threshold to use and then prints the number of temperatures in the given array that are above the threshold using the method above.

public static void main(String[] args) {
	Scanner scan = new Scanner(System.in);
	double[] tempValues = { 23.1, 15.3, 64.4, 23.6, 86.4, 49.3, 77.3 };



	

}

5. Suppose there exists a Die class that has the following method

public int roll()
Rolls the die randomly and returns the face value rolled.

Write a main method that plays the following dice game. There are two 6-sided dice in this game. The player rolls the dice, and if the player does not roll "doubles" (the same value on each die), the player adds the total rolled value to his or her score. If the player rolls doubles, the player loses 10 points. In this case, if the player has less than 10 points, the player's score goes down to 0 instead. The game ends when the player reaches at least 70 points or rolls at most 10 times, whichever comes first.

Write your main method so that each roll is displayed along with the total score so far. At the end of the game, output "WINNER" or "LOSER" depending on the final outcome.

Be careful: The roll method is NOT static. To call this method, what do you need to do first?