OCR J277 GCSE Computer Science · 2.2 Programming Fundamentals

Stretch tasks Additional Programming Techniques

Task 1

Quiz Challenge

A quiz game against four computer players, then short and long written answers.

  1. Quiz Blitz12 questions against the clock
  2. Quick Fire4 short answers
  3. Big Think2 long answers

About 20 minutes

Task 2

This or That

Pixel the cat asks 8 questions. Each has two answers: pick the right one.

  • Two answers to every question
  • A fish treat for every right answer
  • Streaks win Pixel shades, then a party hat

About 4 minutes

Task 3

Memory Match

Flip the cards two at a time to match each python card to its result card.

  • 12 cards: each python has a result
  • One peek at every card
  • Combo points for pairs found in a row

6 pairs, about 4 minutes

Task 4

Sort It

Sort each statement into the right box, like the tick-box tables in the exam.

  • 12 cards to sort into 4 boxes
  • Drag a card, tap a box or press its number
  • Combo points for cards sorted in a row

12 cards, about 4 minutes

The games need JavaScript. Every question is below, with the answers at the end.

Read the lesson: Additional Programming Techniques

Additional Programming Techniques: the questions

Quiz Challenge, round 1: Quiz Blitz

  1. What does this print?

    word = "Computing"
    print(word[0:3])

  2. What does this print?

    print(len("Computing"))

  3. scores = [12, 45, 3, 78, 20]. What is scores[1]?

    • 45
    • 12
    • 3
    • 78
  4. Which file mode adds to the end of a file without deleting what is there?

    • "w"
    • "r"
    • "a"
  5. Opening a file in "w" mode erases what was already in it.

    • True
    • False
  6. Which SQL returns only the names of students in Year 11?

    • SELECT * FROM Students WHERE Year = 11
    • SELECT Name WHERE Year = 11 FROM Students
    • SELECT Students FROM Name WHERE Year = 11
    • SELECT Name FROM Students WHERE Year = 11
  7. What is the difference between a parameter and an argument?

    • An argument is a global variable
    • A parameter is in the definition, an argument is the value passed in
    • They are exactly the same
    • A parameter is always a string
  8. board is a 2D list. What does board[1][2] mean?

    • Item number 12
    • Rows 1 to 2
    • Row 1, column 2
    • Row 2, column 1
  9. Which TWO are benefits of subroutines?

    Tick 2.

    • They remove the need for loops
    • They stop the program needing input
    • Code is written once and called many times
    • Each one can be tested on its own
    • They make every variable global
  10. Why are local variables preferred to global ones?

    • They can be used anywhere
    • They never need a value
    • Other parts of the program cannot change them by accident
    • They are faster to type
  11. What does random.randint(1, 6) give?

    • A whole number from 1 to 6, including 6
    • A whole number from 1 to 5
    • A decimal between 1 and 6
    • Always 1 or 6
  12. In OCR's reference language, "Computing".substring(3,4) starts at index 3 and takes 4 characters. What does it give?

Quiz Challenge, round 2: Quick Fire

  1. When finding the largest value in a list, explain why highest should start as the first item, not 0. [2 marks]

  2. Explain the difference between a local and a global variable. [2 marks]

  3. Write an SQL query to show the Name and Grade of every Year 11 student in the Students table. [3 marks]

  4. A program must add a new score to scores.txt without losing the scores already saved. Which file mode should it use, and why? [2 marks]

Quiz Challenge, round 3: Big Think

  1. Write a program that asks for five numbers, stores them in a list, then prints the total and the largest number. [4 marks]

  2. Explain why programmers use subroutines. Use examples. [6 marks]

This or That: tick the right answer

  1. What does this print?

    print("Aisha"[0])
    • i
    • A
  2. Which SQL keyword chooses which rows to include?

    • SELECT
    • WHERE
  3. In an SQL WHERE clause, does a number need quote marks?

    • No: only text does
    • Yes: every value does
  4. What does SELECT * return?

    • Every table
    • Every column
  5. Why close a file after using it?

    • To delete its contents
    • So the data is saved and the file freed
  6. What does this print?

    print("Hello"[::-1])
    • Hello
    • olleH
  7. A function differs from a procedure because it...

    • Takes parameters
    • Returns a value
  8. What is the last index of a list of 10 items?

    • 10
    • 9

Memory Match: match each python to its result

PythonResult
A len("code")1 CODE
B "code".upper()2 codecode
C "code"[1:3]3 c
D "code"[-1]4 od
E "code" * 25 e
F "code"[0]6 4

Sort It: tick one box in each row

String handling, arrays, file handling or SQL?

StatementStringsArraysFilesSQL
A grid accessed by row, then column
name.upper()
* means every column
Fails if the file does not exist, in read mode
open("log.txt", "a")
scores.append(60)
file.close()
board[1][2]
WHERE Year = 11
SELECT Name FROM Students
word[::-1]
first[0] + last
Answers

Round 1: Quiz Blitz

  1. Com Python slicing stops before the end index, so [0:3] gives indexes 0, 1 and 2.
  2. 9 Computing has 9 characters.
  3. 45 Indexes start at 0, so scores[1] is the second item.
  4. "a" "a" is append. "w" erases the file first, and "r" only reads.
  5. True Use append mode to keep the existing data.
  6. SELECT Name FROM Students WHERE Year = 11 SELECT the columns, FROM the table, WHERE the rows match. * would give every column.
  7. A parameter is in the definition, an argument is the value passed in In def area(w, h), w and h are parameters. In area(5, 3), 5 and 3 are arguments.
  8. Row 1, column 2 Row first, then column.
  9. Code is written once and called many times; Each one can be tested on its own Subroutines also make code easier to maintain, reuse and read.
  10. Other parts of the program cannot change them by accident Their memory is also freed when the subroutine finishes.
  11. A whole number from 1 to 6, including 6 randint includes both ends, just like a dice.
  12. puti Index 3 is p, then u, t and i: puti.

Round 2: Quick Fire

    • Every value in the list might be negative
    • Then 0 would wrongly stay the highest, though it is not in the list
    Model answer

    If every value in the list were negative, 0 would never be replaced, so the answer would wrongly be 0, which is not even in the list.

    • A local variable exists only inside the subroutine where it is created
    • A global variable can be used anywhere in the program
    Model answer

    A local variable exists only inside the subroutine where it is created. A global variable can be used anywhere in the program.

    • SELECT Name, Grade
    • FROM Students
    • WHERE Year = 11
    Model answer

    SELECT Name, Grade FROM Students WHERE Year = 11

    • Append mode
    • Write mode would erase the existing scores; append adds to the end
    Model answer

    Append mode, "a". Write mode would erase everything already in the file, but append adds the new score to the end.

Round 3: Big Think

    • Adds each number to the list
    • Repeats five times
    • Casts each input to a number
    • Works out the total
    • Works out the largest
    Model answer
    Python
    numbers = []
    for i in range(5):
        numbers.append(int(input("Enter a number: ")))
    print("Total:", sum(numbers))
    print("Largest:", max(numbers))
    • Written once and called many times, avoiding repetition
    • Each can be tested on its own
    • Easier to maintain: a bug is fixed in one place
    • Can be reused in other programs
    • Supports decomposition: each sub-problem becomes a subroutine
    • Makes the program easier to read
    • Uses parameters, so one subroutine works with different values
    Model answer

    A subroutine is written once and can be called many times, which avoids repetition: a calculate_area(width, height) function can be used wherever an area is needed, with different arguments each time. Each subroutine can be tested on its own, and if there is a bug it is fixed in one place rather than in ten copies, which makes the program easier to maintain. Subroutines can be reused in other programs, and they support decomposition, because each sub-problem becomes a subroutine. A well-named call such as show_welcome(name) also makes the program easier to read.

This or That

  1. A Index 0 is the first character.
  2. WHERE SELECT picks the columns. WHERE picks the rows.
  3. No: only text does Write WHERE Year = 11, but WHERE Name = 'Aisha'.
  4. Every column The asterisk means all columns of the table named after FROM.
  5. So the data is saved and the file freed Closing makes sure everything is written and releases the file.
  6. olleH [::-1] steps backwards through the string, reversing it.
  7. Returns a value Both can take parameters. Only a function returns a value.
  8. 9 Indexes start at 0, so the last is the length minus 1.

Memory Match

  • A 6 len("code"): 4
  • B 1 "code".upper(): CODE
  • C 4 "code"[1:3]: od
  • D 5 "code"[-1]: e
  • E 2 "code" * 2: codecode
  • F 3 "code"[0]: c

Sort It

  1. Arrays A grid accessed by row, then column That is a 2D array.
  2. Strings name.upper() Changing the case of text is string handling.
  3. SQL * means every column SELECT * returns all the columns.
  4. Files Fails if the file does not exist, in read mode Read mode, "r", needs the file to be there.
  5. Files open("log.txt", "a") Opening a file in append mode.
  6. Arrays scores.append(60) Adding an item to the end of a list.
  7. Files file.close() Closing a file saves it and releases it.
  8. Arrays board[1][2] Row then column in a 2D array.
  9. SQL WHERE Year = 11 WHERE chooses which rows to include.
  10. SQL SELECT Name FROM Students A query that picks columns from a table.
  11. Strings word[::-1] Reversing a word is string handling.
  12. Strings first[0] + last Joining text is concatenation, a string operation.