COSC 135 A Exam 2 Practice Problems , Tom Linton Spring 2003
The problems on this practice exam are similar in difficulty level to those that will appear on the second exam. This is NOT intended to be a laundry list of the things you should study for exam 2. I have attempted to include most of the important concepts from chapters 6, 7, and the beginning of 8 on this practice sheet, but certain topics not included here may show up on the exam. Note, we skipped section 7.10 (Recursive Methods) so it will NOT be on the exam.
  1. Use a switch statement to re-write the following if statement:
  2. if (a == 1)
        x += 5;
    else if (a == 2)
        x +=10;
    else if (a == 3)
        x = 16;
    else if (a == 4)
        x += 36;
    else
        x = 25;


  3. Use nested for loops and an OutputBox to print the following patterns (separately):
  4. Pattern I
    1





    1
    2




    1
    2
    3



    1
    2
    3
    4


    1
    2
    3
    4
    5

    1
    2
    3
    4
    5
    6
    Pattern II
    1
    2
    3
    4
    5
    6

    2
    3
    4
    5
    6


    3
    4
    5
    6



    4
    5
    6




    5
    6





    6


  5. Write code that repeatedly asks the user for an integer, and keeps track of the number of positive entries and the number of odd entries. Your code should print the totals and quit, once the user enters 0.
  6. The Java Statement i = i + 1; adds one to the integer variable i. Write two other statements that accomplish the same thing.
  7. Trace the execution of the following code assuming the String variable word = "Hello World!". Explain what the code does, and show what gets printed in the OutputBox named outy.
  8. String vowels = "aeiou";
    vowels += vowels.toUppercase();
    String cons = "bcdfghjklmnpqrstvwxyz";
    cons += cons.toUpperCase();
    int count1, count2, count 3;
    count1 = count2 = count3 = 0;
    char nextLetter;
    for (int i = 0; i < word.length(); i++) {
        nextLetter = word.charAt(i);
        if (vowels.indexOf(nextLetter) >= 0)
            count1++;
        else if (cons.indexOf(nextLetter) >= 0)
            count2++;
        else
            count3++;
    }
    outy.printLine("The word " + word + " has:");
    outy.printLine(Format.leftAlign(15, count1) + " vowels,");
    outy.printLine(Format.leftAlign(15, count2) + " consonants,");
    outy.printLine("and " + Format.leftAlign(15, count3) + " other characters.");
  9. Write Java code that asks the user whether they want to order a plane ticket or a hotel room. Save the result as an int (plane = 0 and hotel = 1) variable named ticket. Ask them how many days until their departure date (for plane tickets) or arrival date (for hotel reservations) and save the result in an int variable named days. For plane tickets, ask them if they prefer a window seat, aisle seat, middle seat, or first class. For hotel reservations, ask them if they prefer 2 double beds, a king size bed, a queen size bed, or a suite. Save these results in an int variable named typePreferred. Define constants for all of these choices, and then determine the value of the variable cost, calculated below, for each of the inputs given. Finally, describe the pricing strategy of this travel broker.
    1. plane ticket, days = 16, window seat.
    2. hotel, days = 5, queen bed.

    double discount = 0.0, cost;
    if (days >= 21)
         discount = .2;
    else if (days > 7)
         discount = .1;
    if (ticket == PLANE) {
        cost = 250.0 - 250.0*discount;
        switch (typePreferred){
            case WINDOW:
            case Aisle:
                cost -=10.0;
            case MIDDLE:
                cost -= 20.0;
                break;
            case FIRSTCLASS:
                cost += 75.0;
                break;
            default:
                break;
        }
    else {
        cost = 80.0;
        switch (typePreferred) {
            case QUEEN:
            case KING:
                cost += 25.0;
                break;
            case SUITE:
                cost += 75.0;
            default:
                break;
        }
        cost -= discount*cost;
    }

  10. Assuming the String variable word already has some text in it, describe what each of the following code segments do.
    1. int count = 0;
      for (int i = 0; i < word.length(); i++) {
          if (word.charAt(i) == ' ')
              count++;
      }
    2. int loc = word.indexOf("hate");
      while (loc > -1) {
          word = word.subString(0, loc) + "dislikes" + word.subString(loc + 4);
          loc = word.indexOf("hate");
      }
    3. String letters = "abcdefghijklmnopqrstuvwxyz", word2 = "";
      word = word.toLowerCase();
      for (int i = 0; i < word.length(); i++) {
          if (letters.indexOf( word.charAt(i) ) >= 0)
              word2 = word2 +  word.charAt(i);
      }
  11. Write a while loop to decide how many times you'd have to multiply 100.0 by 1.06 before the product became 500.0 or more.
  12. Write Java code that asks the user for a positive integer (larger than 1) and either informs the user that their entry is prime (has no factors other than 1 and itself), or lists the smallest positive integer that (evenly) divides their entry.
  13. Write Java code that asks the user for their annual income in dollars (an int) and their name. Then print a message to them (using their name) that informs them they are poor, well-off, or stinking rich, if they earn less than $15000, between $15000 and $50000, or more than $50000 a year respectively.