A simple console-based multiple-choice quiz game written in Java. The program presents 10 Java-related questions to the user, collects their answers, and displays a final score.
- 10 built-in multiple-choice questions covering core Java concepts
- Reads user answers from the console
- Calculates and displays the final score at the end of the quiz
File --> Description
Main.java --> Entry point of the application. Creates a QuestionService instance, runs the quiz, and prints the score.
Question.java --> Model class representing a single quiz question (id, question text, four options, and the correct answer), with getters/setters and a toString() override.
QuestionService.java --> Holds the array of Question objects, handles displaying questions, capturing user input, and scoring.
QuestionServiceis initialized with 10 predefinedQuestionobjects (each with an id, question text, four options, and the correct answer).playQuiz()iterates through all questions, printing the question and its four options, then reads the user's typed answer viaScanner.- User responses are stored in a
selectionarray. printScore()compares each stored user answer against the correct answer for that question and tallies the total score.- The final score out of 10 is printed to the console.
- Make sure you have a JDK installed (Java 8 or higher).
- Compile all three files:
javac Main.java Question.java QuestionService.java
- Run the program:
java Main
- For each question, type your answer exactly as shown in the options (e.g.
new,main(),HashSet) and press Enter. - After all 10 questions, your score will be displayed.
**Welcome to Quize Game**
Question no. : 1
Which keyword is used to create an object in Java?
class
new
this
static
new
- Answer matching is case- and exact-text-sensitive. Since answers are compared with
String.equals()against the exact option text, minor typos or different casing will count as wrong. Consider trimming input and usingequalsIgnoreCase(). - No option labels (A/B/C/D). Users must type the full option text instead of selecting a letter/number, which is error-prone. Adding labeled choices (1–4) and matching by index would improve usability.
- Typo in welcome message:
"/n"is printed literally instead of creating a newline; it should be"\n". - Hardcoded questions. Questions are hardcoded in the
QuestionServiceconstructor. Loading them from a file or database would make the quiz easier to extend. - New
Scannercreated per loop iteration. A singleScannerinstance should be created once (e.g., as a field or passed in) rather than inside the loop.
This project is provided as-is for educational purposes.