0% found this document useful (0 votes)
18 views25 pages

AP CSA Diagnostic Test

The AP Computer Science A Diagnostic Exam consists of two parts: a multiple-choice section with 20 questions and a free-response section with 2 questions, each contributing 50% to the total score. The exam tests knowledge of Java programming concepts, including methods, class declarations, and data structures. Students are provided with specific instructions and assumptions for answering the questions, which cover various programming scenarios and problem-solving tasks.

Uploaded by

oliverguoca
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views25 pages

AP CSA Diagnostic Test

The AP Computer Science A Diagnostic Exam consists of two parts: a multiple-choice section with 20 questions and a free-response section with 2 questions, each contributing 50% to the total score. The exam tests knowledge of Java programming concepts, including methods, class declarations, and data structures. Students are provided with specific instructions and assumptions for answering the questions, which cover various programming scenarios and problem-solving tasks.

Uploaded by

oliverguoca
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

AP Computer Science A Diagnostic Exam

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.

1. Consider the following method.

What value is returned by the call someMethod(13)?


(A) 17
(B) 25
(C) 28
(D) 31
(E) Nothing is returned. There is a compile-time error.

2. Consider the following code segment.

What is printed as a result of executing the code segment?


(A) -11
(B) 4
(C) 11
(D) 13
(E) 15

3. Assume list is an ArrayList<Integer> that has been correctly


constructed and populated with the following items.

Consider the following method.


What value is returned by the call calculate(list)?
(A) 10
(B) 11
(C) 13
(D) 35
(E) 45

4. Consider the following class declarations.


Which of the following declarations compiles without error?
I. Planet mars = new Planet();
II. Planet pluto = new DwarfPlanet("Pluto");
III. Planet ceres = new DwarfPlanet();
(A) I only
(B) II only
(C) I and II only
(D) I and III only
(E) I, II, and III

5. Consider the following code segment.


What is printed as a result of executing the code segment?
(A) [Zombie, Werewolf, Mummy, Witch]
(B) [Zombie, Werewolf, Ghost, Witch]
(C) [Zombie, Werewolf, Mummy, Ghost]
(D) [Zombie, Vampire, Werewolf, Mummy, Ghost]
(E) [Zombie, Vampire, Werewolf, Mummy, Witch]

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.

Which of the following statements compiles without error?


(A) int num = park.acres;
(B) String name = central.getName();
(C) boolean play = park.hasPlayground();
(D) int num = park.getAcres(acres);
(E) park.hasPlayground = true;

8. Consider the following code segment.


What is printed as a result of executing the code segment?
(A) mmm
(B) nonono
(C) mnomno
(D) nomnono
(E) mnomnomno

9. Consider the following method.

What value is returned by the call loopy(12)?


(A) 12
(B) 21
(C) 23
(D) 27
(E) 29

10. Consider the following class declarations.


Consider the following code segment.
What is printed as a result of executing the code segment?
(A) abA
(B) aba
(C) letterlettera
(D) letterletterletter
(E) Nothing is printed. There is a compile-time error.

11. Consider the following method.

What is returned by the call lengthen("APCS")?


(A) "APCS"
(B) "APCSACAA"
(C) "APCSAPCS"
(D) Nothing is returned. Run-time error:
StringIndexOutOfBoundsException
(E) Nothing is returned. The call will result in an infinite loop.

12. Consider the following code segment.


What is printed as a result of executing the code segment?
(A) 1
(B) 13
(C) 13 1
(D) 2 4 5
(E) 2 4 5 9

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

14. Consider the following code segment.


What is printed as a result of executing the code segment?
(A) Nothing is printed. Runtime error: ArrayIndexOutOfBounds
(B) 18
(C) 181620
(D) 68
(E) 1820

15. Consider the following method.


Assume that the string codeword has been declared and initialized as
follows.

What value is returned by the call mystery(codeword, 9)?

(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

16. Consider the following method.

Consider the following code segment.

What is printed as a result of executing the code segment?


(A) num = 10 val[3] = 10
(B) num = 10 val[3] = -2
(C) num = 10 val[4] = 8
(D) num = -2 val[3] = 10
(E) num = -2 val[4] = 8
17. Consider the following code segment.

Consider these additional code segments.


Which of the code segments produce the same output as the original
code segment?
(A) I only
(B) II only
(C) III only
(D) II and III only
(E) I, II, and III

18. Consider the following method.

The method above could be best described as an implementation of


which of the following?
(A) Insertion Sort
(B) Binary Search
(C) Selection Sort
(D) Merge Sort
(E) Sequential Sort

19. Consider the following statement.

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.

Assume ArrayList<City> cities has been properly instantiated and


populated with City objects.
Consider the following code segment.

Which of the following should replace /* missing code */ so that,


after execution is complete, maxPop will contain the largest population
that exists in the ArrayList?
(A)

(B)

(C)

(D)

(E) maxPop should have been set to Integer.MAX_VALUE. This cannot


work as written.

STOP. End of Part I.


AP Computer Science A Diagnostic Exam
Part II
Free Response
Time: 45 minutes
Number of questions: 2
Percent of total score: 50%

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.

Write the ComplexNumber class. Your implementation must include a


constructor that has two double parameters that represent a and b, in
that order, and a default constructor. It must also include a method add
that calculates and returns the sum of the two complex numbers
represented by its two parameters and a toString method that will
return a String representing the complex number in the form (a + bi).
Your class must produce the indicated results when invoked by the
code segment given above.

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.

STOP. End of Part II.

well done !
:)

You might also like