AP CSA Diagnostic Test
AP CSA Diagnostic Test
Part I
Multiple Choice
Time: 45 minutes
Number of questions: 20
Percent of total score: 50
Directions: Choose the best answer for each problem. Some problems take
longer than others. Consider how much time you have left before spending
too much time on any one problem.
Notes:
• The diagnostic exam is exactly one-half the length of the actual AP
Computer Science A Exam.
• You may assume that all import statements have been included
where they are needed.
• You may assume that the parameters in method calls are not null.
• You may assume that declarations of variables and methods appear
within the context of an enclosing class.
6. Suppose that grid has been initialized as n-by-n 2-D array of integers.
Which code segment will add all values that are along the two
diagonals?
7. Consider the following partial class declaration.
Assume that the following declaration has been made in the main
method of another class.
13. Assume that k, m, and n have been declared and correctly initialized
with int values. Consider the following statement.
For which statement below does b2 = !b1 for all values of k, m, and n?
(A) boolean b2 = (n >= 4) && ((m == 5 && k < 2) || (n >
12));
(B) boolean b2 = (n < 4) || ((m != 5 || k >= 2) && (n <=
12));
(C) boolean b2 = (n < 4) && (m != 5) && (k >= 2) || (n <=
12);
(D) boolean b2 = (m == 5 || k < 2) && (n > 12);
(E) boolean b2 = (n < 4);
(A) 5
(B) 6
(C) 7
(D) Nothing is returned. Infinite recursion causes a stack overflow
error.
(E) Nothing is returned. Run-time error:
StringIndexOutOfBoundsException
After executing the statement, what are the possible values for the
variable number?
(A) All integers from 13 to 21 (inclusive).
(B) All real numbers from 13 to 34 (not including 34).
(C) All integers from 13 to 34 (inclusive).
(D) All integers from 13 to 33 (inclusive).
(E) All real numbers from 0 to 21 (not including 21).
20. Consider the following class declaration.
(B)
(C)
(D)
Directions: Write all of your code in Java. Show all your work.
Notes:
• The diagnostic exam is exactly one-half the length of the actual AP
Computer Science A Exam.
• You may assume all imports have been made for you.
• You may assume that all preconditions are met when making calls to
methods.
• You may assume that all parameters within method calls are not
null.
• Be aware that you should, when possible, use methods that are
defined in the classes provided as opposed to duplicating them by
writing your own code.
1. Complex Numbers
In mathematics, a complex number is a number that is composed of
both a real component and an imaginary component. Complex
numbers can be expressed in the form a + bi where a and b are real
numbers and i is the imaginary number ✓–1 (which means that i2 =
−1). In complex expressions, a is considered the real part and b is
considered the imaginary part.
Addition with complex numbers involves adding the real parts and
the imaginary parts as two separate sums and expressing the answer as
a new complex number.
Assume that the following code segment appears in a class other
than ComplexNumber. The code segment shows an example of using
the ComplexNumber class to represent two complex numbers and find
their sum.
2. Coin Collector
The High School Coin Collection Club needs new software to help
organize its coin collections. Each coin in the collection is represented
by an object of the Coin class. The Coin class maintains three pieces of
information for each coin: its country of origin, the year it was minted,
and the type of coin it is. Because coin denominations vary from
country to country, the club has decided to assign a coin type of 1 to
the coin of lowest denomination, 2 to the next lowest, and so on. For
American coins, coinType is assigned like this:
The Coin Club currently keeps track of its coins by maintaining an
ArrayList of Coin objects for each country. The coins in the
ArrayList are in order by year, oldest to newest. If two or more coins
were minted in the same year, those coins appear in a random order
with respect to the other coins from the same year.
The Coin Club has acquired some new collection boxes of various
sizes to store their coins. The boxes are rectangular and contain many
small compartments in a grid of rows and columns. The club will store
coins from different countries in different boxes.
The CoinCollectionTools class below assists the Coin Club in
organizing and maintaining their collection.
(a) The CoinCollectionTools class constructor initializes the instance
variable coinBox as a two-dimensional array of Coin objects with
dimensions specified by the parameters. It then instantiates each of
the Coin objects in the array as a default Coin object with country
equal to the country name passed as a parameter, year equal to 0,
and coinType equal to 0.
Complete the CoinCollectionTools class constructor.
(b) The Coin Club intends to fill the collection boxes from their list of
coins, starting in the upper-left corner and moving down the
columns in order until all Coin objects have been placed in a
compartment.
The fillCoinBox method takes as a parameter an ArrayList of
Coin objects in order by year minted and returns a chart showing
their position in the box, filled in column-major order.
You may assume that coinBox is initialized as intended,
regardless of what you wrote in part (a). Complete the method
fillCoinBox.
(c) Sometimes the Coin Club would prefer to see a list of its coins
organized by coin type.
The fillCoinTypeList method uses the values in coinBox to
create and return an ArrayList of Coin objects filled first with all
the Coin objects of type 1, then type 2, and so on through type 6.
You may assume that no country has more than 6 coin types. Note
that the number of coins from any specific type may be 0.
Since the original coinBox was filled in column-major order,
Coin objects should be retrieved from the coinBox in column-
major order. This will maintain ordering by year within each coin
type.
Remember that the CoinCollectionTools class constructor
filled the coinBox with default Coin objects with a coinType of 0,
so no entry in the coinBox is null.
You may assume that coinBox is initialized and filled as
intended, regardless of what you wrote in parts (a) and (b).
Complete the method fillCoinTypeList.
well done !
:)