Online Quiz System in Java

Last Updated : 4 Jun, 2025

An online quiz system in Java is a project that allows users to answer multiple-choice questions, and we can also get the score immediately. With the help of this project, beginners can learn more about Java programming concepts such as classes, interfaces, arrays, and user input.

In this article, we are going to build a simple online quiz system in Java, but the most interesting thing is that instead of writing the whole code in just one class, we are going to organize the code with the help of interfaces, model classes, and service classes. With the help of this approach, we are making the code more cleaner and easy to understand as well.

Before moving further, let's first understand how this system is going to work.

How This Quiz System Works

Here is how our quiz system is going to work:

  • The quiz contains multiple-choice questions.
  • Each question has four options.
  • The user is going to select an option as their answer.
  • The program will record the answer and check, if it is correct or not and then accordingly calculate the score.
  • In the end, it will show the total score.

Project Structure

The image below demonstrates the project structure.

Project-Structure


We will organize the code into these parts which are listed below:

  • Interface: This will define what a quiz should do(like when to submit the answer, when to start the quiz and when to show the result).
  • Model class: This class will represent a list of questions, options and the correct answer.
  • Service class: This class implements the Quiz interface and contains the quiz logic.
  • Main class: It is the main class that contains the program entry point to run the quiz.

Let's now try to implement the code.

Java Quiz System Project Implementation

1. Quiz Interface

This interface declares the core methods for the quiz.

Quiz.java:

Java
package quiz.interfaces;

public interface Quiz {
    void start();
    void submitAnswer(int questionIndex, String answer);
    void showResults();
}


2. Question Model Class

This class holds the question data.

Question.java:

Java
package quiz.models;

public class Question {
    private String questionText;
    private String[] options;
    private String correctAnswer;

    public Question(String questionText, String[] options, String correctAnswer) {
        this.questionText = questionText;
        this.options = options;
        this.correctAnswer = correctAnswer;

    }
    public String getQuestionText() {
        return questionText;
    }

    public String[] getOptions() {
        return options;
    }

    public String getCorrectAnswer() {
        return correctAnswer;
    }
}


3. QuizService Class

This class contains the logic to run the quiz, it also take input from the user, validate the answers and then calculated the score.

QuizService.java:

Java
package quiz.services;

import quiz.interfaces.Quiz;
import quiz.models.Question;

import java.util.Scanner;

public class QuizService implements Quiz {
    private Question[] questions;
    private String[] userAnswers;
    private int score = 0;

    public QuizService(Question[] questions) {
        this.questions = questions;
        this.userAnswers = new String[questions.length];
    }

    @Override
    public void start() {
        Scanner scanner = new Scanner(System.in);

        System.out.println("Welcome to the Online Quiz!");
        for (int i = 0; i < questions.length; i++) {
            System.out.println("\nQ" + (i + 1) + ": " + questions[i].getQuestionText());

            String[] options = questions[i].getOptions();
            for (int j = 0; j < options.length; j++) {
                System.out.println((j + 1) + ". " + options[j]);
            }

            System.out.print("Enter your answer (1-" + options.length + "): ");
            String input = scanner.nextLine();

            int answerIndex;
            try {
                answerIndex = Integer.parseInt(input);
                if (answerIndex < 1 || answerIndex > options.length) {
                    System.out.println("Invalid option, answer recorded as blank.");
                    userAnswers[i] = "";
                } else {
                    userAnswers[i] = options[answerIndex - 1];
                }
            } catch (NumberFormatException e) {
                System.out.println("Invalid input, answer recorded as blank.");
                userAnswers[i] = "";
            }
        }
    }

    @Override
    public void submitAnswer(int questionIndex, String answer) {
        if (questionIndex >= 0 && questionIndex < userAnswers.length) {
            userAnswers[questionIndex] = answer;
        }
    }

    @Override
    public void showResults() {
        score = 0;
        for (int i = 0; i < questions.length; i++) {
            if (questions[i].getCorrectAnswer().equalsIgnoreCase(userAnswers[i])) {
                score++;
            }
        }
        System.out.println("\nQuiz Completed!");
        System.out.println("Your score: " + score + " out of " + questions.length);
    }
}


4. Main class to Run the Quiz

This class is the entry point where we create questions and start the quiz.

Main.java:

Java
package quiz;

import quiz.models.Question;
import quiz.services.QuizService;

public class Main {
    public static void main(String[] args) {

        Question[] questions = new Question[] {
                new Question("What is the size of an int data type in Java??",
                        new String[] {"8 bit", "16 bit", "32 bit", "64 bit"}, "32 bit"),

                new Question("Which method is the entry point of a Java program?",
                        new String[] {"start()", "main()", "run()", "init()"}, "main()"),

                new Question("Which of these is NOT a valid access modifier in Java?",
                        new String[] {"public", "private", "protected", "package"}, "package"),
        };

        QuizService quiz = new QuizService(questions);

        quiz.start();
        quiz.showResults();
    }
}

Output:

Output


Notes:

  • If the user inputs something invalid (like "five" or "0"), it will record a blank answer and skip scoring that question.
  • The answers you enter correspond to the index of the correct option (1-based).

How to Run the Project in IntelliJ IDEA?

To run the project on IntelliJ IDEA we just need to follow these steps which are listed below:

  • Open IntelliJ IDEA and create a new Java project.
  • Start creating the packages and the structure must be look like this:
    • quiz.interfaces
    • quiz.models
    • quiz.services
    • quiz
  • Create the classes and interface in the correct packages
  • Save all the files.
  • Run the main class
HowToRun
Comment