The worksheetDownload the PDF
Answers

A1.1 Data types and programming constructs

Programming techniques and object-oriented programming · A level · OCR H446 2.2.1, AQA 7517 4.1.1.1, Eduqas A500QS 1.4 · about 20 min

BugBotLab

What this lesson is about

The A level data types, references and user-defined types, constants, and definite and indefinite iteration.

Questions 6 marks in all

  1. [1 mark]A variable holds the location in memory where a list of readings is stored, rather than the readings themselves. What is its data type?

    1. APointer/reference
    2. BArray
    3. CRecord
    4. DInteger
    Answer: A. A pointer or reference holds where a value is stored, not the value.
  2. [1 mark]What does this program print?

    a = [1, 2]
    b = a
    b.append(3)
    c = list(a)
    c.append(4)
    print(a, len(c))
    Answer:
    [1, 2, 3] 4

    b refers to the same list as a, so a becomes [1, 2, 3]; c is a new copy, so appending to it leaves a alone.

  3. [1 mark]A loop has its condition tested at the end. What is always true of it?

    1. AIts body runs at least once
    2. BIts body may run zero times
    3. CIt is definite iteration
    4. DIt cannot use a Boolean condition
    Answer: A. The body runs before the condition is first checked, so it runs at least once.
  4. [1 mark]Which are advantages of using a named constant such as SAFE_CM instead of typing 25 wherever it is needed?

    Tick every answer that is true.

    1. AThe value only has to be changed in one place
    2. BThe name explains what the value means
    3. CIt cannot be changed by mistake while the program runs, in a language that enforces constants
    4. DIt makes the program run in less memory
    Answer: A, B, C. Constants help maintenance, readability and safety; they do not reliably save memory.
  5. [1 mark]A robot must repeat 'drive 5 cm' until the wall is 25 cm away. The number of repeats is not known in advance. What kind of iteration is this?

    1. AIndefinite iteration
    2. BDefinite iteration
    3. CRecursion
    4. DSelection
    Answer: A. The loop stops when a condition changes, not after a known count.
  6. [1 mark]Python has no character type. What type does it use to store a single character such as "F"? Give the Python type name.

    Answer: str. A character is a str of length 1.

The task: readings as a record type

Define a record type Reading with namedtuple and three fields: heading (an integer, 0, 90, 180 or 270), cm (the real number distance() returns) and clear (a Boolean, True when cm is more than the constant CLEAR_CM). Declare CLEAR_CM = 40 as a constant and use the name, not 40, in the comparison. The robot starts facing heading 0. Take a reading, store it in an array, turn right 90 degrees, and repeat until there are four readings and the robot faces its starting direction. Then print each record with print(r), which shows Reading(heading=90, cm=51.0, clear=True), and finally print clear directions: <n>, the number of records whose clear field is True.

# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()

from collections import namedtuple

Reading = namedtuple("Reading", ["heading", "cm", "clear"])

survey = []
# take four readings, one every 90 degrees

print("clear directions:", 0)

The hint students can ask for: Decide the three fields first and what type each one holds. Then one definite loop does the looking: read, build a record, add it to the array, turn a quarter. A second loop over the array does the counting.

A solution

# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()

from collections import namedtuple

CLEAR_CM = 40
Reading = namedtuple("Reading", ["heading", "cm", "clear"])

survey = []
for i in range(4):
    cm = distance()
    survey.append(Reading(i * 90, cm, cm > CLEAR_CM))
    turn_right(30, angle=90)

clear = 0
for r in survey:
    print(r)
    if r.clear:
        clear = clear + 1
print("clear directions:", clear)

Any program that meets the task's checks is marked correct in the simulator; this is one way, not the only way.