Back

Written Response Rehearsal: The Run-Time Error

Six changes · on paper · this is Written Response 2(b)

divider

Objective

The Real Prompt
WRITTEN RESPONSE 2(b)
Describe a change to the procedure that will
result in a RUN-TIME error.
AND explain why the change will result in a
run-time error.

Both halves again. Describe the change and explain why it causes a run-time error.

This page is in C#, not exam notation, and that is deliberate. The reference sheet defines exactly two ways a program can stop early — an out-of-range list index, and an illegal robot move — and nothing else. Your own program can fail in far more ways than that. Run-time errors are a fact about a language, not about an algorithm, and in May you will be writing about your own C#.

Paper only. Do not run these. The whole skill is predicting, and running it first is the one way to learn nothing.

divider

The Program

Hottest
int[] temps = { 18, 22, 31, 27, 15 };
Console.WriteLine($"Hottest of the first 5: {Hottest(5)}");
int Hottest(int count)
{
int best = temps[0];
for (int i = 1; i < count; i++)
{
if (temps[i] > best)
{
best = temps[i];
}
}
return best;
}

It prints Hottest of the first 5: 31. Six changes are proposed below. One at a time, from the original.

Four Possible Outcomes

Sort Each Change Into One
COMPILE ERROR the program does not build, so it never runs
RUN-TIME ERROR the program stops while running
LOGIC ERROR the program finishes and the answer is wrong
NO EFFECT the program finishes with the same answer

Written Response 2(b) asks for the run-time kind only. The other three are here because students hand in the wrong kind, confidently, and lose the point.

The Six Changes
A Change the call to Hottest(6)
B Change temps to an empty array: int[] temps = { };
C Change the loop to start at i = 0 instead of i = 1
D Change the loop condition to i <= count
E Change the last line of Hottest to return bests;
F Change temps[i] > best to temps[i] < best

For Each One

  1. Which of the four outcomes?
  2. Why? One sentence. If it is a run-time error, say what the program tries to do that it cannot.
  3. If it finishes, what does it print?

All four outcomes appear, and no, you are not told how many of each. A count would let you stop thinking once you had found them. At least one of the six will surprise you, and the one that surprises you is the reason this page exists.


Then: Your Own

Find the method you will paste into Procedure (i) of your Project Reference. Write out an answer to Written Response 2(b) about it — a change, and why it causes a run-time error.

It must be a change to that method, and it must genuinely stop the program while it runs. A change that stops it building is a compile error, not a run-time error. "Change the greater-than to a less-than" is a logic error and earns nothing.

divider

Submit

A table with a row for each of the six changes — outcome, why, and what it prints. Plus your own Written Response 2(b) in full sentences.

Worth 5 points, graded on completion.

Commence Challenge