The Quiz App is a Spring Boot-based REST API that allows users to:
- Start a new quiz session.
- Fetch a random multiple-choice question from a database.
- Submit answers to questions.
- Retrieve session statistics, including the total number of questions answered and details on correct/incorrect submissions.
- Manage user quiz sessions.
- Fetch and store multiple-choice questions in a database.
- Submit and validate answers.
- Track user performance during a session.
- Backend Framework: Spring Boot
- Database: H2 (in-memory database for testing) or any SQL-based database
- Testing: JUnit, Spring Boot Test
- Build Tool: Maven
quiz-app
├── src
│ ├── main
│ │ ├── java
│ │ │ └── com
│ │ │ └── example
│ │ │ └── quizapp
│ │ │ ├── controller
│ │ │ │ └── QuizController.java # REST Controller for APIs
│ │ │ ├── model
│ │ │ │ ├── Question.java # Entity for storing questions
│ │ │ │ └── UserSession.java # Entity for tracking user sessions
│ │ │ ├── repository
│ │ │ │ ├── QuestionRepository.java # Repository for Question entity
│ │ │ │ └── UserSessionRepository.java # Repository for UserSession entity
│ │ │ ├── service
│ │ │ │ └── QuizService.java # Service containing business logic
│ │ │ └── QuizAppApplication.java # Main Spring Boot application class
│ │ └── resources
│ │ ├── application.properties # Spring Boot configuration
│ │ ├── data.sql # Optional: Preload questions into the database
│ │ └── schema.sql # Optional: Define database schema
│ └── test
│ └── java
│ └── com
│ └── example
│ └── quizapp
│ └── QuizAppApplicationTests.java # Unit and integration tests
├── pom.xml # Maven dependencies
└── README.md # Project documentation
- JDK 17 or later
- Maven 3.8+
- An IDE (e.g., IntelliJ IDEA, Eclipse) or a terminal
-
Clone the repository:
git clone https://github.com/your-repo/quiz-app.git cd quiz-app -
Build the project using Maven:
mvn clean install
-
Run the application:
mvn spring-boot:run
-
The application will be accessible at:
http://localhost:8080
Endpoint:
POST /api/quiz/start?userId={userId}
Description: Starts a new quiz session for the specified user. Response Example:
{
"userId": 1,
"totalQuestionsAnswered": 0,
"correctAnswers": 0,
"incorrectAnswers": 0
}Endpoint:
GET /api/quiz/question
Description: Retrieves a random multiple-choice question. Response Example:
{
"id": 1,
"questionText": "What is the capital of France?",
"optionA": "Paris",
"optionB": "London",
"optionC": "Rome",
"optionD": "Berlin"
}Endpoint:
POST /api/quiz/submit?userId={userId}&questionId={questionId}&chosenOption={chosenOption}
Description: Submits the user's answer for a question. Response Example:
{
"userId": 1,
"totalQuestionsAnswered": 1,
"correctAnswers": 1,
"incorrectAnswers": 0
}Endpoint:
GET /api/quiz/session?userId={userId}
Description: Fetches the user's session statistics. Response Example:
{
"userId": 1,
"totalQuestionsAnswered": 3,
"correctAnswers": 2,
"incorrectAnswers": 1
}To execute all tests, run:
mvn testTests are included for:
- Service layer logic
- API endpoints
QuizAppApplicationTests.java contains unit and integration tests to verify the correctness of the application.
By default, the project uses an in-memory H2 database. You can configure another database (e.g., MySQL, PostgreSQL) by updating application.properties:
spring.datasource.url=jdbc:mysql://localhost:3306/quizdb
spring.datasource.username=root
spring.datasource.password=password
spring.jpa.hibernate.ddl-auto=update- Add support for timed quizzes.
- Implement user authentication and authorization.
- Store quiz history for users.
- Enhance question pool with different categories and difficulty levels.
Contributions are welcome! Please fork the repository, create a feature branch, and submit a pull request.
This project is licensed under the MIT License.