15-100 Fall 2007 Practice Exam #2 (Reid-Miller)

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.) SHOW YOUR WORK!

    (a)

    int total = 1;
    while (total < 80) {
        total = total + total;
    }
    System.out.println(total);
    
    (b)
    int result = 4;
    int limit = 100;
    do {
        result *= 2;
    } while (result != limit);
    System.out.println(result);
    
    (c)
    int sum = 0;
    for (int i = 1; i <= 12; i += 3) {
    	sum += i;
    }
    System.out.println(sum);
    
    (d)
    int product = 1;
    for (int i = 10; i < 5; i--) {
        product *= i;
    }
    System.out.println(product);
    
    (e)
    String str = "Carnegie";
    for (int i = 1; i < str.length(); i += 2) {
        System.out.print(str.charAt(i));
    }
    
  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? Explain why the answer is the same as the answer in (a) or explain why it is not the same.

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

  3. 3. Let score be an array of integer values declared as follows:

    int[] score;
    

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

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

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

    (d) Is the Math.random method a static method? How do you know?

    4. The countOutliers method has three parameters, an array of double data, and two doubles that are lower and upper bounds. This method returns the number of values in the array that are not in the normal range, that is between the lower and upper bounds inclusive.

    (a) Complete the method below to count the outliers in the array.
    public static ____________ countOutliers(double[] data, 
                                   double lower, double upper) {
    
        int count = ____________;
    
        for (int i = 0; i < ____________________; i++) {
    
    	 if ((data[i] _____ lower) ______ (data[i] ______ upper)) {
    
                 count =  _____________;
    	 }
    
        }
        
        __________________
    }
    

    (b) Write a code fragment that prints the number of outliers in an array of doubles pH where 0.0 is the lower bound and 14.0 is the upper bound. You can assume pH has already been assigned values.

    5. Recall that we have defined a Die class that has two methods:

    Die(int numSides)
    Creates a die with the given number of sides
    
    void roll()
    Simulates a roll of the die
    
    int getFaceValue()
    Returns the current value facing up on the die
    

    Write a class called DiceGame with 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 the player's score. If the player rolls doubles, the player loses 10 points instead. (NOTE: If the player does not have at least 10 points and rolls doubles, the player's score goes back to 0.) The player rolls at most 10 times. If the player reaches a score of least 70 points, the player wins and the game ends immediately.

    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.