CIT 591 Midterm Exam, Fall 2008 Name ______________________________
  1. (15 points) In each of the following, write a loop to print out the numbers 1 through 100, one number per line. You may declare a variable if you need one.
    1. Use a for loop to print the numbers 1 through 100, one number per line.
      for (int i = 1; i <= 100; i++) {
          System.out.println(i);
      }

    2. Use a while loop to print the numbers 1 through 100, one number per line.
      int i = 1;
      while (i <= 100) {
          System.out.println(i); // or: System.out.println(i++);
          i++;
      }
    3. Use a do-while loop to print the numbers 1 through 100, one number per line.
      int i = 1;
      do {
          System.out.println(i); // or: System.out.println(i++);
          i++;
      } while (i <= 100);

  2. (5 points) Write a switch statement to print even if the int variable number is even, or odd if number is odd.
    switch (number % 2) {
        case 0:
            System.out.println("even");
            break;
        case 1:
            System.out.println("even");
            break; //optional
    }

  3. (5 points) Write a single statement to create and throw an IllegalArgumentException if the variable n is greater than 9999. (IllegalArgumentException is a Sun-provided class.)
    if (n > 9999) {
        throw new IllegalArgumentException();
    }

  4. You can compute the average of two numbers by adding them together and dividing by two.
    1. (5 points) Write a method to compute and return the average of two double values.
      public static double average(double x, double y) { // public, static are optional
          return (x + y) / 2.0;
      }

    2. (2 points) Should the above method be static? Why or why not?

      Yes, because it uses nothing from the class it happens to be defined in.
      (But it doesn't have to be static.)

    3. (2 points) What will happen if you try to call the above method with two int values?

      It will correctly find the average (and return it as a double).

  5. (6 points) In Eclipse, what key combination does the following:
    1. Brings you to the declaration of the selected method or variable.
      F3 (also okay: Function-F3)
    2. Finds the next occurrence of the selected word.  
       Control-K
    3. Comment out (or un-comment out) the selected lines.
      Control-/
  6. (5 points) For each of the following in a Payroll class, tell whether it should be static, instance, or local.

    1. An array of employees' Social Security numbers.
        static--just one for the class
    2. Your job title.
      instance--belongs to each individual
    3. A method to compute bonuses based on job titles.
      static--same for everyone

    4. A variable used to hold the number of hours you have worked this week.
      instance--must be kept throughout the week

    5. A variable used to compute your additional pay for overtime work.
      local--just used in computations
  7. (5 points) Every Person must have a name. Every Employee (which extends Person) must have an integer employeeID. Write a minimal constructor for Employee.

    Employee(String name, int employeeID) { // may be public
        super(name);
        this.employeeID = employeeID; // or--could generate new ID here
    }


  8. (10 points) True or False:

    1. True   Every class has at least one constructor.
    2. True   Every class has a toString() method.
    3. True   Every class has an equals(Object obj) method.
    4. False  Only one class in a project may have a main method.
    5. False  Every JUnit test class must have a @Before method.
    6. True   In JUnit, the @Before method is run once for each @Test method.
    7. True   A method that overrides another method must have the same return type.



    8. True   If a method has a double parameter, it can be called with an int value.
    9. True   If a method has a double parameter, it can be called with a char value.
    10. False  The statement x = x++; adds one to x.

  9. (5 points) Tell what the one syntax error is in the following, and correct it.
        Random rand = new Random();
            do {
                int Even = rand.nextInt(100);
            } while (Even % 2 == 1);
    The variable "Even" must be declared outside the block. The fact that it is capitalized is poor style, but is not a syntax error.
  10. (5 points) What words do each of the following acronyms stand for?

    1. JDK
      Java Development Kit
    2. DRY
      Don't Repeat Yourself
    3. XP
      Extreme Programming
    4. TDD
      Test-Driven Design
    5. KISS
      Keep It Simple, Stupid
  11. (4 points) Mention two ways in which writing tests before writing the code to be tested (rather than writing them afterwards) is likely to improve the quality of the program.
    Correct Answers True but irrelevant statements
    • Gives you a clearer idea what to do when you write the methods.
    • Code is necessarily written to be testable.
    • Encourages you to write simpler, single-purpose methods.
    • Methods will be more independent of the environment.
    • Well-tested methods will have fewer bugs.
    • Having tests makes it safer to modify the code in the future.
  12. (10 points) What is printed by each of the following?
    1. System.out.print(1 + 2 + "buckle my shoe" + 3 + 4 + "close the door");
      3buckle my shoe34close the door
    2. System.out.print("hello".length() == 5 ? "yes" : "no");     yes
    3. System.out.print(new char[] { 'a', 'b', 'c' });     abc
    4. int x; System.out.print(x = 5 + 7 * 2);     19
    5. char[] chs = new char[] { 'a', 'b', 'c' }; System.out.print(chs[1]);     b




  13. (4 points) Draw the array that results from the following declaration:
         int[ ][ ] foo = new int[ ][ ] { { 3, 1, 4 }, {1, 5, 9 }, { 2, 6 } };

    3 1 4
    1 5 9
    2 6

  14. (2 points) Can you use the access modifiers public and private for variables declared inside a method? Why or why not?

    No, because variables declared inside a method are never accessable outside that method.



  15. (2 points)
    a. What class contains the method isLetter(char ch)?
    Character
    b. In that class, what is the complete header for the isLetter method?

    public static boolean Character(char ch)   // okay to omit "public"

  16. (2 points)
    a. What class contains the method trim()?
    String
    b. In that class, what is the complete header for the trim method?

    public String trim()

  17. (2 points) What is the difference between a runtime error and a logic error?

    Runtime errors are noticed by the computer, and an Exception is thrown. A logic error does not (necessarily) cause an Exception to be thrown, but it does cause unexpected behavior, or cause the results to be incorrect.

  18. (2 points) Write an assert statement that says you believe the variable difference to be zero.
    assert difference == 0;

  19. (2 points) Suppose you catch an Exception e. Write a statement to print out all available information about e.
    e.printStackTrace();