?

AP® Computer Science A

Free Version

Upgrade subject to access all content

Moderate

Error Classification: Identifying Different Types of Errors

APCSA-VVEJTV

Classify the following lines of code as:

  • Having a syntax error (incorrect grammar, punctuation)
  • A lexical error (word not in dictionary)
  • An execution error (produces an error when run)
  • A logic error (runs ok, but doesn’t do what is intended).

I.

    // print rainfall divided by percentage of annual rainfall
    int total = 20;
    int[] rainfall = {3, 4, 7, 6};
    for (int i=0; i<rainfall.length; i++)
    {
        int pct = rainfall[i] / total;
        System.out.println(rainfall[i] / pct);
    }

II.

    // calculate yearly average
    double monthlyAvg = 2.5;
    double yearlyAvg = monthlyavg * 12;

III.

    //calculate total rainfall from all values in array
    int total = 0;
    int[] rainfall = {3, 4, 7, 6};
    for (int i=0; i<rainfall.length-1; i++)
        total+=rainfall[i];

IV.

    // print all the scores from the array
    int[] scores = new int[ ;
    for (int i=0; i<scores.length; i++)
        scores[i] = i * 10;
A
I.   logic error
II.  lexical error
III. execution error
IV.  syntax error
B
I.   execution error
II.  syntax error
III. logic error
IV.  lexical error
C
I.   logic error
II.  syntax error
III. execution error
IV.  lexical error
D
I.   logic error
II.  lexical error
III. syntax error
IV.  execution error
E
I.   execution error
II.  lexical error
III. logic error
IV.  syntax error