HelpContactFor teachers and institutes
>_StemPy Lab
Mock TestsClasses

>_StemPy Lab

Learn. Solve. Code the solutions. Test yourself.

Live examsFree mock exams, timed and rankedEveryone starts together, and your score and rank come the moment it ends.See the exams →

Learn

  • Physics
  • Chemistry
  • Mathematics
  • Biology
  • Python Programming
  • Statistics & Data Science
  • Computer Science
  • Classes
  • Groups

Practice

  • Problems
  • Quizzes
  • Mock Tests
  • Prepare for an Exam
  • Question Sets
  • Exams
  • Certificates
  • Python Labs
  • AI Practice Generator
  • Student-Created Tests

Community

  • Discussions
  • Resources
  • Notes
  • Glossary
  • Blog

Account

  • Log in
© 2026 senLearn. All rights reserved.HelpFor SchoolsAboutContactPrivacy PolicyTerms of UseRefund Policy
  1. Problems
  2. /Shopping.txt holds six lines: bread, rice, milk, eggs, tea and jam…
MediumComputer ScienceHigh School (9–10)Computer Science › ProgrammingNumericalConceptualIGCSE

Shopping.txt holds six lines: bread, rice, milk, eggs, tea and jam…

StemPy LabContributed by StemPy Lab

Try it on paper first. A hint, the key idea and the worked solution are here when you need them.

Shopping.txt holds six lines: bread, rice, milk, eggs, tea and jam. Backup.txt already holds three lines from last week.

pseudocode
DECLARE Count : INTEGER
DECLARE Item : STRING
OPENFILE "Shopping.txt" FOR READ
OPENFILE "Backup.txt" FOR WRITE
FOR Count ← 1 TO 4
    READFILE "Shopping.txt", Item
    WRITEFILE "Backup.txt", Item
NEXT Count
CLOSEFILE "Shopping.txt"
CLOSEFILE "Backup.txt"

How many lines does Backup.txt hold afterwards, and what is its last line? Change the program so that it copies the whole list.

Check your answer

Work it out on paper, then type your number. The answer itself stays hidden.

Need a reminder? The key ideaShowHide

OPENFILE <name> FOR READ or FOR WRITE (WRITE makes a new file and loses the old data), then READFILE <name>, <variable> to read the next line, or WRITEFILE <name>, <value> to write one, then CLOSEFILE <name>. Files keep data after the program stops.

The worked solution

Have you tried it?

Work it out on paper first. It is fine to get stuck or make a mistake: that is how we learn. When you are ready, go through the solution one step at a time, and check each step against your own.

Step 1. The old lines. Opening Backup.txt FOR WRITE creates a new file, so last week's lines are lost.

Step 2. The new lines. The loop runs 4 times, so bread, rice, milk and eggs are copied. The file holds 4 lines, and the last is eggs.

Step 3. The change. The list has 6 lines, so the loop must run 6 times. Better, use a constant:

pseudocode
CONSTANT Items ← 6
FOR Count ← 1 TO Items
    READFILE "Shopping.txt", Item
    WRITEFILE "Backup.txt", Item
NEXT Count

Answer: 4 lines, ending with eggs. Tea and jam are not copied.

Log in to save this for later
← All Problems
← The file Best.txt holds one line: 120. What is output, and what does Best.txt hold afterwards, when…A long scenario question. A teacher has the names of 5 students in the file Names.txt, one name a… →
More onComputer Science › Programming

Discussion (0)

Log in to join the discussion.

Loading…

Learn it in

  1. Reading and writing filesArrays and Files in Pseudocode

More on this topic

  1. A long scenario question. A teacher has the names of 5 students in the file Names.txt, one name a…Hard
  2. The file Best.txt holds one line: 120. What is output, and what does Best.txt hold afterwards, when…Easy
  3. The nine squares of a board are input row by row: X, O, O, then X, X, X, then O, a dot for an empty…Hard
Three teams play three games each. Points[Team, Game] holds the points of a team in a game…Medium
  • A small theatre has 5 rows of 8 seats. The program marks booked seats…Easy
  • All problems on this topic →