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.

2. Use a `while` loop to print the numbers 1 through 100, one number per line.

3. Use a `do-while` loop to print the numbers 1 through 100, one number per line.

2. (5 points) Write a `switch` statement to print `even` if the int variable `number` is even, or `odd` if `number` is odd.

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.)

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.

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

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

5. (6 points) In Eclipse, what key combination does the following:
1. Brings you to the declaration of the selected method or variable.

2. Finds the next occurrence of the selected word.

3. Comment out (or un-comment out) the selected lines.

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. ___________

3. A method to compute bonuses based on job titles. ___________

4. A variable used to hold the number of hours you have worked this week. ___________

5. A variable used to compute your additional pay for overtime work. ___________

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`.

8. (10 points) True or False:

1. __________ Every class has at least one constructor.

2. __________ Every class has a `toString()` method.

3. __________ Every class has an `equals(Object obj)` method.

4. __________ Only one class in a project may have a `main` method.

5. __________ Every JUnit test class must have a `@Before` method.

6. __________ In JUnit, the `@Before` method is run once for each `@Test` method.

7. __________ A method that overrides another method must have the same return type.

8. __________ If a method has a `double` parameter, it can be called with an `int` value.

9. __________ If a method has a `double` parameter, it can be called with a `char` value.

10. __________ 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);```
10. (5 points) What words do each of the following acronyms stand for?

1. JDK

2. DRY

3. XP

4. TDD

5. KISS

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.

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");```

2. `System.out.print("hello".length() == 5 ? "yes" : "no");`

3. `System.out.print(new char[] { 'a', 'b', 'c' });`

4. `int x; System.out.print(x = 5 + 7 * 2);`

5. `char[] chs = new char[] { 'a', 'b', 'c' }; System.out.print(chs);`

13. (4 points) Draw the array that results from the following declaration:
```int[ ][ ] foo = new int[ ][ ] { { 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?

15. (2 points)
a. What class contains the method `isLetter(char ch)`?

b. In that class, what is the complete header for the `isLetter` method?

16. (2 points)
a. What class contains the method `trim()`?

b. In that class, what is the complete header for the `trim` method?

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

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

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