Questions

End of File Handling (3 marks)

Consider the following code used to read a file until the end: myFileHandler = open(“myFile.txt”) while NOT myFileHandler.endOfFile() print(myFileHandler.readLine()) endwhile myFileHandler.close() (a) Explain what the endOfFile() method does in this context. (1 mark) (b) What does readLine() do in this loop? (1 mark) (c) Why is it important to close the file after all operations […]

End of File Handling (3 marks) Read More »

Substrings (3 marks)

Given the string s = “ComputerScience”, explain what s.substring(3,6) would output. In this question the string manipulation method .substring(x , i) should be interpreted as: x is starting index, i is number of characters, index begins at 0)

Substrings (3 marks) Read More »

Casting (2 marks)

Study the code below. Identify where casting is used and why it might be useful in this context. quantity = input(“Enter number of products: “) total_cost = input(“Enter total cost of products: “) average_cost = float(total_cost) / int(quantity) print(“Average Cost:”, average_cost)

Casting (2 marks) Read More »

Shopping Cart
Scroll to Top