A201/A597 Sample questions

Solutions


  1. Given the following declarations:

    int quantity = 5;
    double unitCost = 0.65;
    String drink = "pepsi";
    

    For each expression in the following table, indicate its value and the type of the value.

    Expression

    Value Type
    Numeric.round(quantity * unitCost) 3 int
    ("diet " + drink).length() 10 int
    drink.substring(1, 4).toUpperCase() "EPS" String
    drink.equals("coke") false boolean
    quantity + 10 + "" "15" String
    (10 <= quantity) || ((quantity % 2) == 0) false boolean


  2. Each of the following program fragments was written twenty minutes before it was due to be turned in, and as a result contains one major error that either prevents it from running, or produces the wrong answer. Correct this error by rewriting or inserting one or more lines.

    1. String areaCode, phoneNumber;
      
      System.out.print("Enter your complete telephone number xxx-xxx-xxxx: ");
      phoneNumber = Console.in.readLine();
      
      phoneNumber.substring(0, 3) = areaCode;  // ERROR!
      
      System.out.println("You live in area code " + areaCode + ".");
      
      Error: the assignment to areaCode is backwards.
      Correction: areaCode = phoneNumber.substring(0, 3);

    2. int gallons;
      double mpg, distance;
      
      distance = gallons * mpg;  // ERROR!
      gallons = 148;
      mpg = 21;
      
      System.out.println("You can travel " + distance + " miles on " +
                         gallons + " gallons of gasoline.");
      
      Error: gallons and mpg are initialized after they are used in the distance calculation.
      Correction: Move the ERROR line down two lines.

    3. String name;
      
      System.out.print("Enter your name: ");
      Console.in.readLine();   // ERROR!
      
      System.out.println("Your name starts with " + name.substring(0, 1));
      
      Error: the user input is never stored in name.
      Correction: name = Console.in.readLine();

    4. int jellyDonuts, glazedDonuts;  // ERROR!
      double jellyPrice = 0.45, glazedPrice = 0.35, totalCost;
      
      System.out.print("How many jelly donuts do you want? ");
      jellyDonuts = Console.in.readDouble();
      System.out.print("How many glazed donuts do you want? ");
      glazedDonuts = Console.in.readDouble();
      
      totalCost = jellyDonuts * jellyPrice + glazedDonuts * glazedPrice;
      

      Error: the return value of readDouble is stored in an integer.
      Correction: Either change the type of jellyDonuts and glazedDonuts to be double, or use readInt instead of readDouble.

    5. String name; String letter;
      
      name = "Dorothy";
      letter = name.substring(length() - 1, length());  // ERROR!
      
      System.out.println("This name ends with: " + letter);
      

      Error: the length method is not applied to an object.
      Correction: letter = name.substring(name.length() - 1, name.length());


  3. Complete the following Java application which prompts the user for a word with an odd number of letters, and then prints the middle letter of the word. Here is a sample run. The user input is in italics.
    Enter an odd-length word: chicken

    The letter c is in the middle.

    Do not worry about error checking the input. That is, do not bother to verify that the length of the word the user types is indeed odd. Here is the program that you are to complete.

    public class MiddleLetter {
      public static void main(String[] args) {
    
        System.out.print("Enter an odd-length word: ");
        String word;
        word = Console.in.readLine();
    
        int len, mid;
        len = word.length();
        mid = len / 2;
    
        String letter;
        letter = word.substring(mid, mid + 1);
    
        System.out.println("The letter " + letter + " is in the middle.");
    
      }
    }
    


    1. The user runs a program containing the following fragment and then types 3 8 at the prompt. What exactly is printed by the output statements?

      int x, y;
      System.out.print("Enter two integers: ");
      x = Console.in.readInt();
      y = Console.in.readInt();
      x = 2 * x;
      y = y - 1;
      System.out.println("x = " + x + ", y = " + y);
      y = 5;
      x = y;
      y = y - 1;
      System.out.println("x = " + x + ", y = " + y);
      
      Answer:
      x = 6, y = 7
      x = 5, y = 4

    2. What is printed by the following program fragment?

      Time dinnerTime, examStart, examEnd;
      dinnerTime = new Time(1998, 2, 18, 18, 0, 0);  // Feb 18, 1998 at 6 pm
      examStart = new Time(1998, 2, 18, 19, 0, 0);   // Feb 18, 1998 at 7 pm
      examEnd = examStart;
      
      examEnd.addSeconds(2 * 60 * 60);
      
      System.out.println(examStart.secondsFrom(examEnd));
      System.out.println(dinnerTime.getHours() - examStart.getHours());
      System.out.println(examEnd.getHours() - dinnerTime.getHours());
      

      Answer:

      0
      -3
      3

    3. What is printed by the following code fragment?

      int west = 1, language = 2;
      if (west == 1) {
         if (language != 2)
            System.out.print("Auf Wiedersehen ");
         else
            System.out.print("Adios ");
         }
      else
         System.out.print("Sayonara ");
      
      Answer: Adios


  4. Complete the following program which determines how many days a certain month has. Model your code on the following algorithm:

    You do not have to verify that the user input is in the expected range. Here is the program that you are to complete.

    public class DaysInMonth {
      public static void main(String[] args) {
        int month, year;
        System.out.print("Enter a month number (1-12): ");
        month = Console.in.readInt();
        // You fill in the rest of the code that is needed...
    
    
        System.out.print("Enter a year after 1970: ");
        year = Console.in.readInt();
    
        Time t;
        if (month == 12)
           t = new Time(year + 1, 1, 1);
        else
           t = new Time(year, month + 1, 1);
    
        t.addseconds(-24 * 60 * 60);
    
        System.out.println(month + "/" + year + " has " + t.getDay() + " days.");
    
    
      }
    }