Loading...
Question 161 of 319

Use the BASIC program below to answer this question
10     LET K = 2
20     LET L = 9
30     LET SUM = 0
40     FOR J = K TO STEP 2
50     SUM = SUM + J2
60     PRINT "ANSWER =", SUM
70     NEXT J
80     END
The last output that will be displayed by the program is

  • A.  20
  • B. 36
  • C. 64
  • D. 120

Correct Answer: D

Explanation
To determine the last output displayed by the BASIC program, let's analyze the code step by step. Step-by-Step Explanation
  1. Initialization:
  2. The program starts by initializing two variables:
    • K is set to 2.
    • L is set to 9.
  3. It also initializes SUM to 0, which will be used to accumulate the results.
  4. FOR Loop:
  5. The FOR loop is defined as FOR J = K TO L STEP 2. This means:
    • The loop starts with J equal to K (which is 2).
    • It will continue to increment J by 2 until it reaches or exceeds L (which is 9).
  6. The values of J during the iterations will be: 2, 4, 6, and 8.
  7. Calculating the Sum:
  8. Inside the loop, the program executes the line SUM = SUM + J2. However, it seems there is a typographical error in the code. The correct operation should be SUM = SUM + J^2 (where J^2 means J squared).
  9. Let's calculate the value of SUM for each iteration:
    • Iteration 1: When J = 2
    • SUM = 0 + 2^2 = 0 + 4 = 4
    • Iteration 2: When J = 4
    • SUM = 4 + 4^2 = 4 + 16 = 20
    • Iteration 3: When J = 6
    • SUM = 20 + 6^2 = 20 + 36 = 56
    • Iteration 4: When J = 8
    • SUM = 56 + 8^2 = 56 + 64 = 120
  10. End of Loop:
  11. After the last iteration, J would increment to 10, which exceeds L (9), thus exiting the loop.
  12. Output:
  13. The program then executes the line PRINT "ANSWER =", SUM, which will display the final value of SUM, which is 120.
Conclusion The last output displayed by the program is 120. Explanation of Other Options
  • Option A: 20 - This value corresponds to the sum after the second iteration (when J is 4), but it does not account for the subsequent iterations.
  • Option B: 36 - This value does not match any of the calculated sums at any point in the iterations. It seems to be a miscalculation.
  • Option C: 64 - This value could be confused with the square of 8, but it does not represent the accumulated sum at any point in the program.
  • Option D: 120 - This is the correct answer, as calculated through the iterations.
Revision Summary
  • The program initializes variables and uses a FOR loop to iterate through even numbers from 2 to 8.
  • The sum of squares of these numbers is calculated and stored in SUM.
  • The final output is printed after all iterations are complete.
  • The correct answer is 120, as it is the accumulated sum of squares of 2, 4, 6, and 8.
← Previous Next →
Jump to: 161 162 163 164 165 166 167 168 169 170