Practice
Quiz Item Loaded from a File
2021/May/June·Variant 2·Q3·[15 marks]
HARDObject-Oriented Programming
File I/O question —
quiz.txt is pre-loaded for you during grading.Declare class QuizItem with private Question, Answer and Points. Write NEW, GetQuestion() and CheckAnswer(Guess) which returns TRUE when Guess matches Answer exactly.
The file quiz.txt stores three items as repeating triples of lines: question, answer, points. Load the three objects, then read a choice 1–3 and a guess. Output the chosen question, then Correct or Wrong.
Inspired by Cambridge International AS & A Level Computer Science (9618) May/June 2021 Paper 42 TreasureChest (class + file load), rewritten as an original auto-gradable task.
Input: Choice (1, 2 or 3), then a guess. Output: The question text, then Correct or Wrong.
Example:
Input: 2
Paris
Output: Capital of France
Correct
Premium is coming soon. All grading features are currently unlocked.
Sample Test Cases
Test 1: Second item, exact match
quiz.txt
2*2 4 10 Capital of France Paris 5 5+3 8 8
Inputs: 2, Paris
Expected: Capital of France
Correct
Test 2: Wrong guess on the first item
quiz.txt
2*2 4 10 Capital of France Paris 5 5+3 8 8
Inputs: 1, 5
Expected: 2*2
Wrong