# APCSA 2015 Q3, grading criteria

If a question (eg free response 2015 #3) has the following criteria:

+1 Accesses all necessary elements of entries (No bounds errors)

+1 Returns identified value or returns 0 if no entry exists in entries with row index row and column index col

... and the student prematurely exits a loop with this common if-else return mistake:

for(SparseArrayEntry entry : entries) {
if(entry.getRow() == row && entry.getCol() == col) {
return entry.getValue();
} else { // mistake, loop will only ever run once
return 0; // should do this return after loop
}
}


Does the student lose both of the above points because 1) the premature return prevents accessing all necessary elements, and 2) a zero might be returned in spite of the fact an entry does exist.

It feels like double jeopardy but I was hoping an AP grader or someone with more experience teaching the course could weigh in on the decision.

• This question is better asked on the APCS mailing list. The people there know the rules. But IIRC, for free response questions there aren't "forbidden but correct" constructs. I hope that is still true. But the student has, in fact failed both tests unless they get a hit on the first element. But I'm not a grader. Mar 25 at 19:54
• @Buffy I would not direct AP teachers away from this site; we have plenty of AP expertise here, I think. And our format is much better than that mess of a mailing list for addressing such questions.
– Ben I.
Mar 26 at 3:20
• @BenI., I disagree for questions like this one. The advice given there for questions like this reflect official policy due to the folks that answer there. Mar 26 at 15:30
• @Buffy I mean, we're not the official college board site, though questioners here would presumably know that. AP teachers all have to sign up through the college board process, and register through AP Classroom. But the listserver there is a morass, difficult to use, and practically unsearchable. Answers here remain searchable, and can be fixed at any time.
– Ben I.
Mar 26 at 16:54

I am also not an AP Grader, though I have taught the class for well over a decade now, and I am pretty sure about this would be approached on the actual AP test.

(If you pursue this further, and I do turn out to be wrong, please post a new answer with a correction.)

There are two considerations:

1. My understanding has been that every area of code is examined in isolation, and examined as if all of the code around it were good.

2. Furthermore, the first rubric point concerns itself with the loop, and the second point with the return. This is a philosophical separation in addition to the code separation I outlined above.

For both reasons, then, since the loop itself is fine, the student would receive that point. However, the student would lose the second point, since they are returning the wrong answer much of the time.

The reason for the separation of concerns in point 1 is because otherwise you get into sudden death scenarios where one small mistake brings you from 5 points on a large item to 0, since none of the items are ever actually fulfilled. Such scores are clearly out of line with the work produced by the students.

• Actually the loop doesn't access all necessary entries. Mar 26 at 11:18
• @Buffy Only if you neglect point 1 it doesn't. If you begin to get overly technical with these rubric wordings, they stop working entirely. Students can give outright incorrect answers to questions, but still get awarded every point on the rubric. These all have to be taken in a spirit of intentionality, or they just stop making sense.
– Ben I.
Mar 26 at 12:32
• Thanks for sharing your experience. I like the idea that I can treat the points as separate concerns and award them accordingly. Mar 26 at 23:01
• What am I missing here? The method/function that contains this code returns after examining only one element. It clearly doesn't "access all necessary elements". The returns short circuit both the loop and the containing method. The student is clearly confused. I'd like to see what an official ruling would be. This seems clearly wrong to me. Mar 27 at 13:40
• @Buffy We can ask on the listserver. I would be surprised if it came out differently. Separate the loop (basically just the declaration) from its contents. Each major component should be read as if the other code around it was appropriate. I believe that the rationale for this is that it avoids point cascades. Under the stricter reading you are using, for instance, the second point is always inaccessible if the first point is unearned, no matter what else the student does.
– Ben I.
Mar 27 at 14:23

I know this is coming a week after you wanted it... but, I am an APCS-A teacher and I have been a AP Reader in the past.

The scoring guidelines are applied thusly:

1. mistake in the code causes bounds error - Deduct the point for accessing array
2. having deducted a point, pretend the problem is fixed
3. continue scoring with fix

The goal is to not have a single issue rob a student of all the points.

The one exception is the "did they get it right" point. This would be the result of debate with your elbow partner, the table leader and the question leaders.

In your specific example the student would probably lose both points as the guideline is specifically mentioning a completed condition that was never reached.

I don't educate on the AP level, but let me trot out one or two of my hobby horses^W^W things that I try to teach my students.

One. I object to this code structure. Semantically, a loop serves to compute something, so you want a code structure

int your_result;
for (whatever) { do something }
return your_result.


By having the return in the loop you subvert this structure, conflating the loop and the surrounding function.

I try to prevent this sort of construct by showing how to derive code by textual refinement. You go from

// compute a result


to

result = compute_result()


to

function compute_result() {
result = init;
// do all tests
return result;
}


to the full code. (This principle of textual refinement also postpones introducing local variables to the last moment, so they automatically are in the correct scope!)

Two. Loops that serve some test are (usually) either a for-all predicate or a there-is predicate. So, with the above structure, this is a there-exists problem:

bool element_is_present = false;
for ( elements )
if ( condition )
element_is_present = true;


and then you can jump out of the loop prematurely because a single true test is enough for the conclusion. And then refine this loop to suit the problem.

• I disagree, actually. See: cseducators.stackexchange.com/q/4427/1293. On the other hand, I write very short methods where it isn't an issue. I strive for Cyclomatic complexity = 1. Mar 28 at 14:27
• I see your point about minimizing mentally retained state. However, in my above distinction of eixsts/forall loops this presupposes that the student has already recognized that there is a exists problem, where indeed early return is possible. In this case, the question asks about accessing "all" elements so an early return is not possible and the whole code structure is wrong. So my second point stands that they need to start by analyzing the propositional status of the for loop. Mar 28 at 14:49
• Addressing your objections to my first point explicitly: I like deriving code by textual refinment. And your early returns are not amenable to such an approach. In fact, I find early returns kind of a hackerish solution with the main justification that "it works". How do you teach when early returns are possible and when not? Mar 28 at 14:51
• @Buffy Another thought: most programming language lack introspection so data is the only thing that is visible. With early returns you're teaching students a pattern that they need to process completely mentally, and the code is either all right or all wrong, with no visual indication as to the structure of that pattern. So I think you are overloading their mental capacities: they have to consider code paths entirely in their head. Whereas in my approach the code is clearly leveled, with each textual refinment having a clear function. Mar 28 at 16:31