0% found this document useful (0 votes)
12 views

Ap22 Apc Computer Science A q3

Uploaded by

zhaolingyun1010
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)
12 views

Ap22 Apc Computer Science A q3

Uploaded by

zhaolingyun1010
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/ 15

2022

AP Computer Science A
®

Sample Student Responses


and Scoring Commentary

Inside:

Free-Response Question 3
R Scoring Guidelines
R Student Samples
R Scoring Commentary

© 2022 College Board. College Board, Advanced Placement, AP, AP Central, and the acorn logo are registered
trademarks of College Board. Visit College Board on the web: collegeboard.org.
AP Central is the official online home for the AP Program: apcentral.collegeboard.org.
AP® Computer Science A 2022 Scoring Guidelines

Applying the Scoring Criteria

Apply the question scoring criteria first, which always takes precedence. Penalty points can only be
deducted in a part of the question that has earned credit via the question rubric. No part of a question (a, b, c)
may have a negative point total. A given penalty can be assessed only once for a question, even if it occurs
multiple times or in multiple parts of that question. A maximum of 3 penalty points may be assessed per
question.

1‐Point Penalty
v) Array/collection access confusion ([] get)
w) Extraneous code that causes side‐effect (e.g., printing to output, incorrect precondition check)
x) Local variables used but none declared
y) Destruction of persistent data (e.g., changing value referenced by parameter)
z) Void method or constructor that returns a value

No Penalty
 Extraneous code with no side‐effect (e.g., valid precondition check, no‐op)
 Spelling/case discrepancies where there is no ambiguity*
 Local variable not declared provided other variables are declared in some part
 private or public qualifier on a local variable
 Missing public qualifier on class or constructor header
 Keyword used as an identifier
 Common mathematical symbols used for operators (× • ÷ ≤ ≥ <> ≠)
 [] vs. () vs. <>
 = instead of == and vice versa
 length/size confusion for array, String, List, or ArrayList; with or without ( )
 Extraneous [] when referencing entire array
 [i,j] instead of [i][j]
 Extraneous size in array declaration, e.g., int[size] nums = new int[size];
 Missing ; where structure clearly conveys intent
 Missing { } where indentation clearly conveys intent
 Missing ( ) on parameter‐less method or constructor invocations
 Missing ( ) around if or while conditions

*Spelling and case discrepancies for identifiers fall under the “No Penalty” category only if the correction can
be unambiguously inferred from context, for example, “ArayList” instead of “ArrayList”. As a counterexample,
note that if the code declares "int G=99, g=0;", then uses "while (G < 10)" instead of
"while (g < 10)", the context does not allow for the reader to assume the use of the lower case
variable.

© 2022 College Board


AP® Computer Science A 2022 Scoring Guidelines

Question 3: Array / ArrayList 9 points

Canonical solution

(a) public double getAverageRating() 3 points


{
int sum = 0;

for (Review r : allReviews)


{
sum += r.getRating();
}

return (double) sum / allReviews.length;


}

(b) public ArrayList<String> collectComments() 6 points


{
ArrayList<String> commentList = new ArrayList<String>();

for (int i = 0; i < allReviews.length; i++)


{
String comment = allReviews[i].getComment();
if (comment.indexOf("!") >= 0)
{
String last =
comment.substring(comment.length() – 1);
if (!last.equals("!") && !last.equals("."))
{
comment += ".";
}
commentList.add(i + "-" + comment);
}
}
return commentList;
}

© 2022 College Board


AP® Computer Science A 2022 Scoring Guidelines
(a) getAverageRating

Scoring Criteria Decision Rules


1 Initializes and accumulates sum Response can still earn the point even if 1 point
they
 fail to use a loop to accumulate
 fail to call getRating or call
getRating incorrectly
2 Accesses every element of allReviews Responses will not earn the point if they 1 point
(no bounds errors)  access the elements of allReviews
incorrectly
3 Computes and returns double average Response can still earn the point even if 1 point
rating based on getRating return they
values (algorithm)  fail to initialize the accumulator for the
sum

Responses will not earn the point if they


 fail to accumulate the sum of all ratings
 use integer division to compute average
 include parameters on call to
getRating
 fail to call getRating on all
elements of allReviews
Total for part (a) 3 points

© 2022 College Board


AP® Computer Science A 2022 Scoring Guidelines
(b) collectComments

Scoring Criteria Decision Rules


4 Instantiates an ArrayList capable of 1 point
holding String objects

5 Accesses every element of allReviews Responses can still earn the point even if 1 point
(no bounds errors) they
 fail to keep track of the index

Responses will not earn the point if they


 access the elements of allReviews
incorrectly
6 Calls getComment on an element of Responses can still earn the point even if 1 point
allReviews, calls at least one String they
method appropriately on the  call some of the String methods on
getComment return value, and all objects other than getComment
String method calls are syntactically return values
valid
Responses will not earn the point if they
 include a parameter when calling
getComment
 call any String methods incorrectly
 call any String methods on objects
other than String values
7 Compares the final character of the Responses can still earn the point even if 1 point
comment to both a period and an they
exclamation point  use incorrect logic in the comparison
 call String methods incorrectly

Responses will not earn the point if they


 use == instead of equals when
comparing String objects
8 Assembles string appropriately based on Responses can still earn the point even if 1 point
result of comparison of last character with they
period and exclamation point (algorithm)  call String methods incorrectly
 use == instead of equals

Responses will not earn the point if they


 fail to keep track of the element index
 use incorrect logic in the comparison
9 Adds all and only appropriate constructed Responses can still earn the point even if 1 point
strings to the ArrayList (algorithm) they
 initialize the ArrayList incorrectly
 fail to return the constructed
ArrayList (return is not assessed)
 assemble the review string incorrectly
 access the elements of allReviews
incorrectly

© 2022 College Board


AP® Computer Science A 2022 Scoring Guidelines
Total for part (b) 6 points
Question‐specific penalties
None

Total for question 3 9 points

© 2022 College Board


Q3 Sample A 1 of 2
--
-- Important: .Completely fill In the circJe Queatlon 1 Question 2 Question 3 Question 4 •

--
that corresponds to the question you
are answering on this page. 0 0 • 0

---
_,n your 1'88ponse to each question at lhe top of a new page.

-- r.,..) Pvbhc.. cl�\le Jfl_ Avel"'l#� l4,-1i.,.,� l)

---
{

--
� I
-:1n,f\ .i•?Jc.l"!!J_,;. /)�... :, . l.

-- "' { ih� i = 0 i (. . Col. II Re,/�. lei,, fN, ; ;,tt- )


--
I

{
--
--
--
-- 1
--
--
--
--
--
--
--
--
--
--..
--
-�

--
--
--
--
-- Page6

-- Use a pencU only. Do NOT write your name. Do NOT write outside the box.

-------------------
-·• • • •• • • ••
�..---.
Q3 Sample A 2 of 2
• Important: Completely fill in the circle· 3 .· �..· ·auelit1on 4

--'...,..
Question 1 Queatlon2 Question
.

---.
,

that corres ponds to the question you


are answering on this page. 0 0 • 0
.,
Begin your response to each question at the top of a new �age.

--...
--
;,
_.,

Arrr.:, t,.:o , 5+r;MJ }'�1: ,ne..,, · A� ra,L..si · < $1ri,e,5f�(); ---


✓.

..,

fw- (��t '- L (. <ell �view5. �"' SI\ ; #f --.


-.-.
: 0; i

{
i r { ,d1 tltv-�,'O\is Ci.J :,�� o('.{,
,! ") l >:
l 0 ) --
{. . . .. . --
�"'j'
;r'-[:·
tr\.{
(l
� � ;�J;J_ · i

ll•.;.� !ll. "'�jf. l 'f


I
--
rfv:� $v� -
&I J. 'f'c.,.-w·.. '$v W}h•...,-.J(�"tew. le-. .s,;
· (
-1 ). st.lf<.t {) ( ''. ,, ))// �
,..�.,;ev, lew5t ..., ),
l .e�u(" f ")))° ;
II I\ •
I ! ---
--.
·•
J I

t ·;.

IJ

\\
♦ �v �w) •
I ----..
,
J ,..-
.....

--
C.'•-f'lt

lowhwt fin t.S • ·"'


)

·��

---
.-
•.�

--..

-..
--..
-
lJae 8 pencil only. Do NOT W11te your name. Do NOT write O)!aide the box.
,.
1111!'

• 0Na7/T

-------------- --·-· - ·-
•••
-
•• •• I
- -·-· -- ·- ..
I
• • .. �
Q3 Sample B 1 of 2
--
• Important: Completely fill In the circle Question 1 Question 2 Question 3

e
Question 4
---
---
that corresponds to the question you
are answering on this page. 0 0 0

r.'�-tl;le
Begin your response to each question at the top of a new page.

--
���,.,tM�:l� .f.
�0; ,t-... )
�( ,nt \: o; j < �112.��s.1�
--
"'°"� --
i,,W CAJ\�i[:1;,

---
$

'

�:/doMc') �;�u�s.��r.);

� "-1);
--
---
--
--
---
--
--
---
---
--
---
---
--
--
--
--
---
--
··-
Page5

Use a pencil only. Do NOT write your name. Do NOT write outside the box.
■ •• ■ •• ■■■■ --

0063156
011321/6 •• • • •• •
�------------*----·------
Q3 Sample B 2 of 2
--
.--------------------------- ·- --·-·

-- Important: Completely fill In the circle Question 1 Question 2 Queetion 3 Queatlon 4 •

r --
that corresponds to the question you
are answering on this page. 0 0 e 0

--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
-··
r -:
-
UH a pencll only. Do NOT write your name. Do NOT write outalde the box.

• • •• • • •• 0632710
Q3 Sample C 1 of 1
Important: Completely fill in the circle Question 1 Question 2 Question 3 Queatton· 4 --
----�-
that corresponds to the question you
are answering on this page. 0 0 t 0 . ·�.

{),, � Begin your response to each question at the top of a new page.

�l,c � f. l,V'l!! �.,,.,:,'!, l)


t. . �
---
---
(r;O
-� h,\(l
� r:, ��o �

---
�,...(',,,+- ·, :.O; ;'< c,.\l�\�3+-h/i+•)
t
l.,,.,�,_ , ,. ..v �u R,.;,..o() , I £,3 t+,; --
rerum °' l,V'Q, t; -...-
----
-- .... 11111

s�,t)_i.1 -�'"'t-e,�•"e,v �l\jt1:

---
�,..(',,,� ; : t>; ·, < �\\ �$-6(,1. l�t-J,; ·, ,,,�)

�{111 4- i � o; ; < G-1\��1 .�;+,(); ·,4,-�) '


-
._.'!.

i if ( �11�evst) . S�S½n"., � i ,i �1) "� 1


·",1
---
8-.1,..,; e;-<:(�\�f, J i-��
"'-

"'jl�l��LJ .l�·l\� • " .., \\
a.\\<W� .�t"''Jj t�l\Q,e,1;'�). J�",·\\l� " ")
..
\_
.'

----
. ,

-�-
·-.

---
"I!!'
\ I.fee a ·�cll':onry.·bc:;'NbT write your name. Do NOT.\vrtte outside the�:

• ..�-

0008417 ■ ••• ■
• ONtf/• •• • i •• •
AP® Computer Science A 2022 Scoring Commentary

Question 3

Note: Student samples are quoted verbatim and may contain spelling and grammatical errors.

Overview

This question tested the student’s ability to:

• Write program code to create objects of a class and call methods.


• Write program code to satisfy method specifications using expressions, conditional statements,
and iterative statements.
• Write program code to create, traverse, and manipulate elements in 1D array and ArrayList
objects.

This question involved the traversal and manipulation of a one-dimensional (1D) array containing
Review objects and the instantiation and building of an ArrayList containing String objects.
Students were expected to write two methods in the ReviewAnalysis class, using its 1D array
instance variable, and use two methods from the Review class when accessing Review objects.
Students were also expected to use methods of the String class and construct a String object.

In part (a) students were expected to write a loop that accessed each element of the array instance
variable allReviews and returned the calculated average of all the return values of the method
getRating. Students had to declare and initialize a variable to hold the sum of all ratings. Inside
the loop, students were expected to call the method getRating on all Review elements in
allReviews, accumulating a total of the ratings. Outside the loop, students were expected to
calculate and return the average rating as a double value.

In part (b) students were asked to develop an algorithm to: (1) Identify all Review elements of
allReviews that have comments containing an exclamation point, using the getComment
method; (2) build an ArrayList of String objects, each of which would be a string based on an
identified review and formatted according to the specification:

“<index number of Review object><hyphen><comment>”

When building the formatted String to be added to the ArrayList, the specification required
that each identified formatted comment end in a period if the original comment did not already end
with a period or an exclamation point. In identifying the comments meeting the specification,
students were to use methods of the String class, such as indexOf, substring, and equals,
as well as Boolean operators.

© 2022 College Board.


Visit College Board on the web: collegeboard.org.
AP® Computer Science A 2022 Scoring Commentary

Question 3 (continued)

Sample: 3A
Score: 8

In part (a) point 1 was earned because the integer sum is initialized and then accumulated in the
body of the for loop. Point 2 was earned because all elements of the array allReviews are
accessed correctly, and there are no bounds errors. Point 3 was earned because the average is
correctly calculated and returned. The average is calculated by accumulating the sum of ratings from
all elements of allReviews. Each rating is accessed by a call to getRating on an element of
allReviews. The average is then calculated by dividing the sum, cast to a double, by the length
of the array, which represents the number of ratings.

In part (b) point 4 was earned because an ArrayList is properly instantiated to hold String
objects. Point 5 was earned because all elements of allReviews are accessed appropriately with
an indexed for loop, and there are no bounds errors. Point 6 was not earned because the call to
the indexOf method in the first if statement is not correct. The expression
allReviews[i].indexOf("!") is incorrect because the value referred to by allReviews[i] is
not a String. The correct expression would be
allReviews[i].getComment().indexOf("!"). Alternatively, the String returned by the
method call allReviews[i].getComment() could be stored in a variable and used to call
indexOf. If the indexOf call had been correct, point 6 would have been earned, even though
parentheses are missing on the call review.length – 1. The missing parentheses is one of the
minor errors for which no penalty is assessed on this exam. (See the “No Penalty” category on page
1 of the Scoring Guidelines for a complete list of such errors.) Point 7 was earned because the final
character of the comment is compared to both a period and an exclamation point using the equals
method of the String class. A correct response could also correctly extract the final character
using the charAt method of the String class, in which case comparing the final character to
both a period and an exclamation point would require char data types and the use of the == or
!= operators. Point 8 was earned because the logic of examining the final character is correct, and
an appropriate String is assembled using the correct index from the for loop. The logic is
correct in the if statement because the response checks that the final character of the comment is
not a period and is also not an exclamation point. A period is concatenated only to comments that
end with neither a period nor an exclamation point. Point 9 was earned by adding to the ArrayList
only the appropriate constructed strings, containing the required exclamation point, even though the
call to indexOf is not syntactically valid. The incorrect indexOf method call was assessed in
Point 6.

Sample: 3B
Score: 6

In part (a) point 1 was earned because the integer total is initialized and accumulated. The
omitted call to getRating is not assessed in this point. Point 2 was earned because all elements of
the array allReviews are accessed correctly, and there are no bounds errors. Point 3
was not earned because the average is not calculated using accumulated getRating return values.
The omitted getRating method call is assessed in this point.

© 2022 College Board.


Visit College Board on the web: collegeboard.org.
AP® Computer Science A 2022 Scoring Commentary

Question 3 (continued)

In part (b) point 4 was not earned because an ArrayList is not properly instantiated to
hold String objects. The angle brackets (< and >) on the left-hand side of the assignment
statement cannot be empty. The type of object to be stored in the ArrayList must be specified
within the angle brackets. When writing a method that returns an ArrayList<String>, any of the
following ArrayList declarations and instantiations would receive credit.

ArrayList list1 = new ArrayList();


ArrayList list2 = new ArrayList<>();
ArrayList list3 = new ArrayList<String>();
ArrayList<String> list4 = new ArrayList();
ArrayList<String> list5 = new ArrayList<>();
ArrayList<String> list6 = new ArrayList<String>();
ArrayList ArrayList = new ArrayList();

Point 5 was earned because all elements of allReviews are accessed correctly using an indexed
for loop, and there are no bounds errors. Point 6 was earned because the getComment method
retrieves a String from each review and stores its value in a String variable named com.
All String method calls in the response are syntactically correct. Point 7 was earned because the
final character of the String is correctly compared to both a period and an exclamation point. The
lastIndexOf method is a String method that returns the index of the last occurrence of its
parameter. Methods that are not part of the AP Java subset can be used in a response, as long as
they are used correctly. Point 8 was not earned for multiple reasons. First, the logic used to examine
the final character is incorrect; the && operator should be used instead of the || operator. As a
result of the incorrect use of ||, a period is appended to comments that end in either a period or an
exclamation point. Second, the update of an element in the ArrayList is incorrect. The statement
exM[k] = i + "-" + com + "." does not modify the value of the String in the ArrayList
and therefore no correctly constructed String exists. Either of these errors is sufficient to deem
the point unearned. Point 9 was earned because all and only appropriate constructed strings are
added to the ArrayList. Although the constructed values are not entirely correct, that error was
assessed in point 8. Point 9 can still be earned even if a response assembles the review String
incorrectly.

Sample: 3C
Score: 3

In part (a) point 1 was earned because the integer total is initialized and is accumulated in the
body of the for loop. Point 2 was earned because all elements of the array allReviews are
accessed correctly, and there are no bounds errors. The extraneous [] used when referencing the
entire allReviews array is a minor “No Penalty” error. Point 3 was earned because the average is
correctly calculated and returned. Because the accumulated sum is stored in the double variable
total, the division in the calculation of the average produces the correct double value.

In part (b) point 4 was not earned because an ArrayList is not properly instantiated to
hold String objects. The response instead attempts to declare a String array. Point 5
was not earned because the inner loop reuses the variable i. The updated value of i at the end of

© 2022 College Board.


Visit College Board on the web: collegeboard.org.
AP® Computer Science A 2022 Scoring Commentary

Question 3 (continued)

the inner loop is used in the outer loop. This may result in some elements of allReviews being
skipped. All elements of allReviews must be accessed in order to earn this point. Point 6
was not earned because the getComment method is never called to get a String object. All the
String methods are called on Review objects rather than String objects. Point 7
was not earned because the comparisons of the final character with a period and an exclamation
point use != instead of equals. Even though the String methods are called incorrectly, the
point could have been earned if equals had been used correctly. Point 8 was not earned because
the logic used to examine the final character is incorrect; the && operator should be used instead of
the || operator to check that neither a period nor an exclamation point appears at the end of the
String. The invalid use of != is not the reason the point was not earned, as that was assessed in
point 7. Point 9 was not earned for multiple reasons. First, the use of nested loops could allow
duplicate copies of the same comment if multiple exclamation points occur in the comment. Second,
the search for the exclamation point is syntactically incorrect.

© 2022 College Board.


Visit College Board on the web: collegeboard.org.

You might also like