15-100 Fall 2007 Practice Exam #1 Questions (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. Evaluate the following expressions. Show your work. If there is a syntax error, explain.
    (a) double d = 10 + 4 * 8 - 6 / 2 ;
    
    (b) int i = 16 % 5 ;
    
    (c) int i = (int)Math.round(1 + 1/2 + 1/4 + 1/8);
    
    (d) String s = "123" + "456";
    
    (e) int i = Math.pow(2, 3);
    

  2. Given the following Java string definition

    String phrase = "No news is good news.";

    what is the output of each of the following Java statements?

    (a) System.out.println(phrase.length());
    
    (b) System.out.println(phrase.charAt(5));
    
    (c) System.out.println(phrase.substring(3,7).toUpperCase());
    
    (d) System.out.println(phrase.substring(0,3) + phrase.substring(11));
    
    (e) System.out.println(phrase.replace('n', 's'));
    

  3. (a) Using the random method of the Math class, show how to initialize a double variable named value that contains a random floating point value in the range [0, 100).

    (b) Using the random method of the Math class, show how to initialize an int variable named number that contains a random integer from the set {12, 18, 24, 30, 36, 42}.

    (c) The following simple Java program prints out two 3-digit random sequences. Complete the missing code.

    public class LotteryNumberGenerator {
    
       // This simple program prints out TWO 3-digit random
       // numbers for a lottery
       public static void main(String[] args) {
     
            System.out.println("Play these lottery numbers:");
     
           // complete the main method here
    
    
    
    
    
        }
    
        // display THREE random digits from the numbers string
        public static void displayNumber() {
    
            String numbers = "0123456789";
            System.out.___________(numbers.charAt(________________));
            System.out.___________(numbers.charAt(________________));
            System.out.___________(numbers.charAt(________________));
    
        }
    }
    
  4. Consider the following code fragment in Java:
    boolean rain = true;
    boolean snow = false;
    if (!rain || snow)
        System.out.println("HAPPY");
    else
        System.out.println("SAD");
    

    (a) What is the output of this code fragment?

    (b) Draw a flowchart of the conditional in this code fragment.

    (c) Use de Morgan's law to complete the following code fragment so that it is equivalent to the one above.

    boolean rain = true;
    boolean snow = false;
    if (_________________________________)
        System.out.println("SAD");
    else
        System.out.println("HAPPY");
    

    (d) What is the output of the following code fragment?

    int x = 4;
    int y = 9;
    if (x % 2 == 1)
        y *= 2;
        y += 2;
    System.out.println(y);
    
  5. Electricity is billed based on the number of kilowatt-hours (kWh) used by a customer. To determine how many kilowatt-hours are used by a customer, we find the difference between the meter reading of a customer from the previous month to the meter reading for the current month. For example, if the meter reads 15100 for the previous month and 16232 for the current month, the customer used 16232-15100 = 1132 kWh. Note that a meter reading is always 5 digits, and the meter "wraps around" after 99999 back to 00000.

    Once the reading is calculated, compute the charge for the customer based on the following table:

    Kilowatt-Hours Used Charge
    Less than 1000 7 cents per kWh
    1000-2500 $70, plus 5 cents per kWh for each kWh above 1000
    More than 2500 $145, plus 3 cents per kWh for eaach kWh above 2500

    Write a simple Java program that uses the Scanner class to read in the two valid meter readings as integers (you may assume a meter reading of 00235 would be entered as 235). It should then print out the number of kilowatt-hours used and the charge for the user in dollars and cents. Do your computations using cents only and then display in dollars and cents when you're done.

    Sample output for two runs of the program (user input in italics):

    Please input last month's meter reading [0-99999]: 15100
    Please input this month's meter reading [0-99999]: 16232
    You used 1132 kilowatt-hour(s).
    Your bill is $76.60
    
    Please input last month's meter reading [0-99999]: 99900
    Please input this month's meter reading [0-99999]: 1
    You used 101 kilowatt-hour(s).
    Your bill is $7.07
    
    Your Program:

    import java.util.Scanner;
    public class ElectricBillGenerator{
    
        public static void main(String[] args) {
    
        	// complete the missing program code below:
    
    
    
    
        }
    }