Gce Model Questions Vol1 2018 Computer Studies Question 8

Practice essay / theory question 8 from the 2018 Gce Model Questions Vol1 Computer Studies examination. This question covers Python programming, Control structures, File handling, Algorithms – prime testing .

Gce Model Questions Vol1 2018 Computer Studies Essay / Theory
Topics: Python programming Control structures File handling Algorithms – prime testing
Question 8 GCE_MODEL_QUESTIONS_VOL1 • 2018 • COMPUTER_STUDIES • essay

You are required to develop a small Python utility that processes a list of integers stored in a text file. The utility must identify all prime numbers in the list and write them to a new file. Answer the following:

Question Parts

( a )
Write a Python function named `is_prime(n)` that returns `True` if the integer `n` is a prime number and `False` otherwise. Your algorithm should be efficient for numbers up to 10 000.
( b )
Using the `is_prime` function, write a complete Python program that: 1. Reads integers from a file called `data.txt` (one integer per line). 2. Stores the integers in a list. 3. Creates a new list containing only the prime numbers. 4. Writes the prime numbers to a file called `primes.txt`, one per line. Include appropriate error handling for missing files and non‑integer lines.
( c )
The following code is intended to perform the same task, but it contains three logical errors. Identify the errors and provide a corrected version of the code. ```python try: f = open('data.txt', 'r') nums = f.readlines() f.close() except: print('File not found') primes = [] for n in nums: n = int(n) if n % 2 == 0: primes.append(n) with open('primes.txt', 'w') as out: for p in primes: out.write(p) ```

Detailed Solution & Marking Scheme

Get the step-by-step solution, key points required by examiners and detailed marking guidance through the Edcrib AI Tutor.

Reveal Answer

About This Question

This is Gce Model Questions Vol1 2018 Computer Studies Question 8. It is one of the essay questions from the 2018 Gce Model Questions Vol1 Computer Studies examination.

The question covers Python programming, Control structures, File handling, Algorithms – prime testing .

Master the Exam!

You've seen a preview, but there are thousands more questions plus AI tutor to break down complex solutions.

Unlock Full Access Available for Android & Windows
Chat with Support