套题2 FRQ
套题2 FRQ
Valid code words have 5 to 8 characters and must not include the string
Example 2
Valid code words must not include the string Because the bounds are
not specified, the length bounds are 6 and 20, inclusive.
1. Directions: SHOW ALL YOUR WORK. REMEMBER THAT PROGRAM SEGMENTS ARE TO BE
WRITTEN IN JAVA.
Notes:
Assume that the classes listed in the Java Quick Reference have been imported where
appropriate.
Unless otherwise noted in the question, assume that parameters in method calls are not null and
that methods are called only when their preconditions are satisfied.
In writing solutions for each question, you may use any of the accessible methods that are listed
in classes defined in that question. Writing significant amounts of code that can be replaced by a
call to one of these methods will not receive full credit.
a) Write method computeTemp, which computes and returns the new temperature for a given element
of temps according to the following rules.
1. If the element is in the border of the array (in the first row or last row or first column or last
column), the new temperature is the same as the old temperature.
2. Otherwise, the new temperature is the average (arithmetic mean) of the temperatures of the four
adjacent values in the table (located above, below, to the left, and to the right of the element).
Copyright © 2021. The College Board. These materials are part of a College Board program. Use or distribution of these materials online or in print beyond your
school’s participation in the program is prohibited.
Page 1 of 3
AP Computer Science A Test Booklet
FRQ-2D Array(11)
(b) Write method updateAllTemps, which computes the new temperature for every element of
temps.The new values should be based on the original values, so it will be necessary to create another
two-dimensional array in which to store the new values. Once all the computations are complete, the
new values should replace the corresponding positions of temps. Method updateAllTemps also
determines whether every new temperature is within tolerance of the corresponding old temperature
(i.e., the absolute value of the difference between the old temperature and the new temperature is less
than or equal to tolerance). If so, it returns true; otherwise, it returns false.
If temps contains the values shown in the first table below, then the call updateAllTemps(0.01)
should update temps as shown in the second table.
Copyright © 2021. The College Board. These materials are part of a College Board program. Use or distribution of these materials online or in print beyond your
school’s participation in the program is prohibited.
Page 2 of 3
AP Computer Science A Test Booklet
FRQ-2D Array(11)
In the example shown, the call updateAllTemps (0.01) should return false because there are several new
temperatures that are not within the given tolerance of the corresponding old temperature. For example,
the updated value in temps[2][3] is 37.5, the original value in temps[2][3] was 40.0, and the absolute
value of (37.5 - 40.0) is greater than the value of tolerance (0.01).
Assume that computeTemp works as specified, regardless of what you wrote in part (a).
Copyright © 2021. The College Board. These materials are part of a College Board program. Use or distribution of these materials online or in print beyond your school’s participation in the
program is prohibited.
Page 3 of 3