Introduction
Quizzes are a fun way to practise records, lists and streams. Each question is a record holding its topic, text, options, correct index and an explanation; the game keeps a list of answers and turns it into a score report at the end.
Problem statement
Objectives
- Model fixed data with records and List.of
- Validate single-character input
- Track streaks and bonus points
- Summarise results with streams
Features
- Topics and explanations for every question
- Re-prompts on invalid answers
- Streak bonus scoring
- Per-topic bar breakdown and wrong-answer review
- Handles input ending early
Concepts demonstrated
- Records
- Immutable collections
- Game loop
- Streams: filter, count, distinct
- Formatted output
System architecture
Question and Answer are records; Answer.correct() compares the chosen index with the question's answer. QUESTIONS is an immutable list. The main loop prints each question, reads answers until one is valid, records an Answer and updates the score and streak. report is a separate method so it can be called both at the end and when input runs out; it uses streams to count correct answers, list topics in order of first appearance and select wrong answers for the review.
Folder structure
Complete source code
src/Main.java
What each file does
- src/Main.java
- The question bank, the answer loop with streak scoring, and the report.
How to run it
Compile
Run
Test against the expected output
Expected output
Generated by running the project with tests/sample-input.txt. Your output should match tests/expected-output.txt exactly.
b B e b b a b b
Testing
- Run with
tests/sample-input.txtand compare withtests/expected-output.txt. - The answer
eis rejected and the question is asked again. - Answers 3 and 4 extend the streak, earning two bonuses (+10).
- The one wrong answer (Q5) appears in the review with its explanation.
Common errors
Upper-case answers are rejected
Normalise with toLowerCase() before checking the letter.
ArrayIndexOutOfBoundsException for answer 'e'
Check that the letter is within the option range before converting it to an index.
Possible improvements
- Load questions from a text file
- Shuffle questions and options with a seeded Random
- Add a time limit per question
Additional challenges
- 1Let the user choose topics before starting.
- 2Remember the best score in a file.
- 3Support questions with more than one correct option.
Built and tested it yourself? Mark it to update your dashboard.