EECS2030E Practice Test 1B

PRACTICE

This is a practice test. The actual test may contain fewer or more questions. The actual test may or may not supply you with other materials (such as starter code).

The Java API is here.


Question 1

Implement the utility class described by this API. You do not have to include javadoc comments.


Question 2

Create a text file named answers.txt (use File->New-> Untitled text file in eclipse). Type your answer to the following question in the text file.

A.

State the definition of a method precondition.

B.

Suppose that the method name alternatingSum from Question 1 was implemented like so:

        public static int alternatingSum(List<Integer> t) {
          int sum;
          /* code not shown here that assigns the correct value to sum */
          return sum;
        }
        

Suppose a client writes a main method that includes the following two lines of Java code:

        List<Integer> aList = Arrays.asList(8, 9, 10, 11);
        int sum = Practice1B.alternatingSum(aList);
        

The first line of code creates the list [8, 9, 10, 11], and the second line of code calls the alternatingSum method from Question 1.

The memory diagram illustrating the state of memory for the two lines of client code is shown below. What suitable values of a, b, and c would complete the memory diagram?

                   ---------------------
                   |    main method    |
                   ---------------------
        aList   100|       300a        |
          sum   102|        a?         |
                   ---------------------
                   |                   |
                   |                   |
                   ---------------------
                300|    List object    |
                   ---------------------
                   |                   |
                   |                   |
                   ---------------------
                   |  alternatingSum   |
                   ---------------------
             t  500|         b?        |
           sum  502|         c?        |
                   ---------------------
        
C.

Provide the Javadoc necessary to exactly reproduce the API documentation for the method last(List<Integer>) from Question 1.

D.

Provide 3 test cases for the method Practice1B.totalArea. Make sure that each test case tests a different feature of the method (i.e., don't provide 3 test cases that all check if the correct area is returned). For each test case, provide a one sentence explanation of what the test case is testing.