O Level / IGCSE Computer Science chapter 8: Programming MCQs with answers

By Team Megalecture · Updated 4 Nov 2026

43 exam-style questions on Programming, each with the answer and a short explanation. Ten of them are below to try. The chapter test draws ten fresh questions each time and is marked the moment you finish, with your score by topic.

43 questions · 4 topics · 10 per test

Test this chapter freeAll O Level / IGCSE Computer Science chapters

What chapter 8 covers

Ten Programming questions with answers

Try each one, then open the answer. These ten stay the same; the test draws new ones every time.

1. Which of these makes a program easier to maintain?

  1. A
    using single-letter names such as X and Q for all variables
  2. B
    writing the whole program as one long block of code
  3. C
    using meaningful identifiers, comments and subroutines
  4. D
    removing all indentation to make the code shorter
Show answer

Answer: C. Meaningful names, comments that explain the code, and procedures and functions make it easier for another programmer to understand and update.

2. The first character of a string is at position 1. What is returned by SUBSTRING("Pakistan", 3, 4)?

  1. A
    akis
  2. B
    kista
  3. C
    ista
  4. D
    kist
Show answer

Answer: D. Start at position 3 (k) and take 4 characters: k, i, s, t. "akis" starts one place too early; "ista" counts from 0.

3. Why is it good practice to use local variables in a subroutine instead of global variables?

  1. A
    local variables can be accessed from anywhere in the program, which makes sharing data easier
  2. B
    local variables never need to be declared
  3. C
    other parts of the program cannot change them by mistake, and names can be reused
  4. D
    local variables make the program run on any computer
Show answer

Answer: C. Local variables keep a subroutine self-contained: it cannot be affected by, or accidentally change, data elsewhere. Their memory is also freed when the subroutine ends.

4. Why would a programmer store 100 temperatures in an array instead of 100 separate variables?

  1. A
    arrays can hold data of different types together
  2. B
    the values share one identifier and can be processed in a loop using an index
  3. C
    arrays keep all of their data safely when the program is closed and opened again
  4. D
    arrays do not need to be declared
Show answer

Answer: B. An array groups values of the same type under one name, and an index variable in a loop can access each element in turn. Data in an array is lost when the program ends unless it is saved to a file.

5. What values are output by FOR i ← 10 TO 1 STEP -3 ... OUTPUT i ... NEXT i?

  1. A
    10, 7, 4, 1
  2. B
    10, 7, 4
  3. C
    1, 4, 7, 10
  4. D
    10, 9, 8, 7
Show answer

Answer: A. i starts at 10 and goes down by 3: 10, 7, 4, 1. The next value, −2, is past the end value 1, so the loop stops.

6. What is the value of X after X ← 4 + 6 * 2 - 8 / 4?

  1. A
    3
  2. B
    14
  3. C
    18
  4. D
    16
Show answer

Answer: B. Multiplication and division are done first: 6 * 2 = 12 and 8 / 4 = 2. Then 4 + 12 - 2 = 14. Working left to right gives the wrong answer 3.

7. A file called Scores.txt already contains data. It is opened with OPENFILE "Scores.txt" FOR WRITE. What happens to the existing data?

  1. A
    it is kept, and new data is added at the end
  2. B
    it is read into an array
  3. C
    it is lost, because a new file is created
  4. D
    it is copied to a backup file
Show answer

Answer: C. In Cambridge pseudocode, WRITE mode creates a new file and any existing data in it is lost.

8. A program must input exactly 30 test scores. Which type of loop is most suitable?

  1. A
    a count-controlled FOR loop
  2. B
    a pre-condition WHILE loop
  3. C
    a post-condition REPEAT loop
  4. D
    an IF statement
Show answer

Answer: A. When the number of repetitions is known in advance, a FOR loop is the simplest choice.

9. What is output by this program?
FUNCTION Gap(A : INTEGER, B : INTEGER) RETURNS INTEGER
    IF A > B
      THEN
        RETURN A - B
      ELSE
        RETURN B - A
    ENDIF
ENDFUNCTION

OUTPUT Gap(4, 9) + Gap(10, 3)

  1. A
    2
  2. B
    12
  3. C
    -2
  4. D
    26
Show answer

Answer: B. Gap(4, 9): 4 > 9 is false, so it returns 9 - 4 = 5. Gap(10, 3) returns 10 - 3 = 7. 5 + 7 = 12.

10. Which statement reads one line of text from the file Scores.txt into the variable LineOfText, after the file has been opened for reading?

  1. A
    INPUT "Scores.txt", LineOfText
  2. B
    READFILE "Scores.txt", LineOfText
  3. C
    WRITEFILE "Scores.txt", LineOfText
  4. D
    OPENFILE LineOfText FOR READ
Show answer

Answer: B. READFILE <file identifier>, <variable> reads data from a file opened in READ mode. WRITEFILE writes; INPUT reads from the user.

Start the chapter testAbout the O Level / IGCSE Computer Science test

Read the notes for this chapter

Ways to practise this chapter

Programming: questions students ask

How many Programming MCQs are there?

43, each with the answer and a short explanation, across 4 topics: Variables, data types and operators, Sequence, selection, iteration and string handling, Procedures, functions and maintainable programs, Arrays and file handling. Every chapter test draws ten of them, so you can take it many times.

Is the O Level / IGCSE Computer Science chapter 8 test free?

Yes. The test, the answers and the explanations are free and need no sign-up. Sign in only if you want your scores kept across devices.

How is the chapter test marked?

The moment you press Finish: your score, the right answer and an explanation for every question, and the ones you got wrong saved in My mistakes to retry.

Keep going

← Chapter 7: Algorithm design and problem-solvingChapter 9: Databases →All chaptersStuck? Ask a question