Skip to content
ExamSpine

Lesson 6.7 · Answer it

Score file two languages

5 marksYou mark it yourself against the marking points

The file scores.txt holds two lines: Kai on the first and 18 on the second, and each line ends with a line end. Two students write the same program, one in Python and one in Java (the Java lines are inside a main that throws IOException, with the imports it needs).

The Python version:

f = open("scores.txt")
player = f.readline()
points = f.readline()
print(player + " scored " + points)

The Java version:

BufferedReader f = new BufferedReader(new FileReader("scores.txt"));
String player = f.readLine();
String points = f.readLine();
System.out.println(player + " scored " + points);

(a) The Python version does not print its message on one line. State the first two lines it prints. [2]

(b) State what the Java version prints. [1]

(c) Both versions leave out one step of using a file. Identify the step, and describe how to change the Python version so that the step happens even if a line raises an exception. [2]

[5]

Report a problem with this page