• Practice
  • About 14 min
  • 5 practice questions

Programming concepts mixed practice

Practise mixed questions on algorithms, operators, loops and errors.

If something looks wrong, please tell us.

Mixed programming questions are mostly tracing. Write down the variables, update them in order and stop exactly where the code says.

Method

  1. List the variables and their starting values.
  2. Follow the steps one by one and update the list.
  3. For loops, note the start, end, step and when the condition is checked.
  4. Compute the final output and double-check boundary values.

Worked examples

Example 1. N = 1; while N < 20, N = N x 2; print N. N goes 1, 2, 4, 8, 16, 32. At 16 the condition is true so it doubles to 32; at 32 the condition fails. Output: 32.

Example 2. How many even numbers does a loop print for I from 1 to 10 if it prints only when I % 2 == 0? Even numbers are 2, 4, 6, 8, 10, so 5.

List the variables, update them step by step, and check the boundary.

— Exam method

Key takeaways: trace on paper, watch boundaries and know your three error types.

Check your understanding

  1. 1. Trace: N = 1; while N < 30, N = N x 2; print N. What is printed?

    1. 16
    2. 32
    3. 30
    4. 64
    Show answer

    Answer: B. N goes 1, 2, 4, 8, 16, 32. At 16 the condition 16 < 30 is true, so it doubles to 32; then 32 < 30 fails. Output: 32.

  2. 2. A loop runs for I from 1 to 12 and prints I only when I % 3 == 0. How many numbers are printed?

    1. 3
    2. 6
    3. 4
    4. 12
    Show answer

    Answer: C. The multiples of 3 up to 12 are 3, 6, 9 and 12, so 4 numbers are printed.

  3. 3. What is 37 % 6?

    1. 6
    2. 6.17
    3. 5
    4. 1
    Show answer

    Answer: D. 6 x 6 = 36 and 37 - 36 = 1, so the remainder is 1.

  4. 4. Trace: A = 3; for I from 1 to 3, A = A x 2; print A. What is printed?

    1. 24
    2. 18
    3. 12
    4. 48
    Show answer

    Answer: A. A goes 6, 12, 24 over the three passes, so 24 is printed.

  5. 5. How many times does the body of a loop run for I from 4 to 11 step 1?

    1. 7
    2. 11
    3. 8
    4. 15
    Show answer

    Answer: C. The values are 4, 5, 6, 7, 8, 9, 10, 11, which is 11 - 4 + 1 = 8 times.

Privacy · Terms · Refunds · Delivery