0% found this document useful (0 votes)
6 views

Akshaya IT Project

The document outlines the development of an online quiz application using Java and SQL, detailing the features and architecture of Java, including its object-oriented nature and platform independence. It provides SQL code for creating a database and tables for storing quiz questions, options, and correct answers, along with Java source code for the quiz program. Additionally, it lists hardware and software requirements for both Java and MySQL, as well as sample quiz implementations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Akshaya IT Project

The document outlines the development of an online quiz application using Java and SQL, detailing the features and architecture of Java, including its object-oriented nature and platform independence. It provides SQL code for creating a database and tables for storing quiz questions, options, and correct answers, along with Java source code for the quiz program. Additionally, it lists hardware and software requirements for both Java and MySQL, as well as sample quiz implementations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 54

ONLINE QUIZ DEVELOPMENT

S.NO TOPIC PAGE

Page 1 of 1
1 ABOUT JAVA 3

2 ABOUT SQL 9

3 HARDWARE AND SOFTWARE 11


REQUIREMENT
4 ONLINE QUIZ SAMPLE 1 13

5 ONLINE QUIZ SAMPLE 2 21

6 ONLINE QUIZ SAMPLE 3 28

7 ONLINE QUIZ SAMPLE 4 34

8 ONLINE QUIZ SAMPLE 5 41

9 ONLINE QUIZ SAMPLE 6 48

10 CONCLUTION 56

Page 2 of 2
Java is a high-level, class-based, object-oriented programming language
that is designed to have as few implementation dependencies as
possible. It is a general-purpose programming language intended to let
programmers write once, run anywhere (WORA), meaning that compiled
Java code can run on all platforms that support Java without the need
for recompilation. Java is a concurrent, class-based, and object-oriented
language. It is freely accessible, and we can run it on all the platforms or
the operating systems. Java is simple and easy to learn. Java
applications are typically compiled to bytecode that can run on any Java
virtual machine (JVM) regardless of the underlying computer
architecture. The syntax of Java is similar to C and C++, but has fewer
low-level facilities than either of them. The Java runtime provides
dynamic capabilities (such as reflection and runtime code modification)
that are typically not available in traditional compiled languages.

Page 3 of 3
FEATURES:

1. Simple

Java is a simplified version of the C++ language, and therefore, it is


familiar also. Moreover, it eliminates all redundant and unreliable code.
There is no support of pointers, preprocessor header files, operator
overloading, and multiple inheritances in Java. This makes Java easier
as compared to C++.

2. Object-oriented

Java is an object-oriented language and mainly focuses on objects


rather than processes. Java follows the Object-Oriented Programming
(OOP) concept like:

 Objects
 Classes
 Inheritance
 Encapsulation / Data hiding
 Abstraction
 Polymorphism
Note- Java is not a pure object-oriented language as it allows the use of
primitive data types.

3. Platform-independent

Java programs can run on any machine or the operating system that
does not need any special software installed. Although the JVM needs
to be present in the machine. Java code compiles into bytecode (.class
file), which is platform-independent. We can run this bytecode on
Windows, Linux, Mac OS, etc.

4. Portable

Java code is executable on all the major platforms. Once we compile


Java source code to bytecode, we can use it in any Java-supported
platform without modification. Java is portable because we can carry

Page 4 of 4
bytecode over to any other platform it runs on.

5. Robust

 There is no use of explicit pointers in Java.


 Java provides a strong memory management
 It supports Automatic garbage collection, so there is no need to
delete the unreferenced objects manually.

 Java also provides exception handling and type-checking


mechanisms.

6. Secure

 Java does not support pointers that make Java robust and secure.
 All Java programs run inside a virtual machine sandbox.
 The Java Runtime Environment (JRE) has a classloader that
dynamically loads the classes into the Java Virtual Machine.

 The Bytecode Verifier of Java inspects the parts of code for


checking the illegal code that can bypass access.

 The Security Manager of Java decides what resources to allot to a


class. Such access includes reading and writing into files.

 Java also helps us develop virus-free systems.

7. Multithreaded and Interactive

Java can handle different tasks simultaneously. Java supports


multithreaded programs, in which there is no need to wait for one task to
finish for another to start.

Page 5 of 5
JAVA ARCHITECTURE:

1. JVM (Java Virtual Machine)

Java Virtual Machine (JVM) provides a runtime environment in which


bytecode executes. Java Virtual Machine is platform-dependent. The
JVM performs the following tasks:

 Loading the code


 Verifying the code
 Executing the code
 Providing a runtime environment
2. JRE (Java Runtime Environment)

JRE is a collection of tools that allow the development of applications


and provide a runtime environment to run Java programs. JVM is a part
of JRE. JRE is also platform-dependent. JRE facilitates the execution of
Java programs and comprises JVM, Runtime class libraries,Deployment
technologies, Java plugin, etc.

Page 6 of 6
JAVA SUPPORT SYSTEMS:

The operations of Java and Java-enabled browsers on the Internet


requires a variety of support systems, like,

 Internet Connection
 Web server
 Web Browser
 HTML (HyperText Markup Language) which is a language for
creating hypertext for the web.

 APPLET tag
 Java code
 Bytecode
 Proxy Server that acts as an intermediate server between the client
workstation and the original server.

 Mail Server

NET BEANS IDE:

NetBeans is an integrated development environment (IDE) for Java.


NetBeans allows applications to be developed from a set of modular
software components called modules. NetBeans runs on Windows,
macOS, Linux and Solaris. In addition to Java development, it has

Page 7 of 7
extensions for other languages like PHP, C, C++, HTML5, and JavaScript.
Applications based on NetBeans, including the NetBeans IDE, can be
extended by third party developers.

MySQL:

MySQL is an open-source relational database management system


(RDBMS). Its name is a combination of "My", the name of co-founder
Michael Widenius's daughter, and "SQL", the abbreviation for Structured
Query Language.

A relational database organizes data into one or more data tables in


which data types may be related to each other; these relations help
structure the data. SQL is a language programmers use to create, modify
and extract data from the relational database, as well as control user
access to the database. In addition to relational databases and SQL, an
RDBMS like MySQL works with an operating system to implement a
relational database in a computer's storage system, manages users,
allows for network access and facilitates testing database integrity and
creation of backups.

Page 8 of 8
FEATURES:

MYSQL is a relational database management system. This database


language is based on the SQL queries to access and manage the
records of the table.

 Easy to use

 It is secure

 Client/ Server Architecture

 Free to download

 It is scalable

 Speed

 High Flexibility

 Compatible on many operating systems

Page 9 of 9
 Allows transactions to be roll-back

 Memory efficiency

 High Performance

 High Productivity

 Platform Independent

 GUI Support

 Dual Password Support

HARDWARE REQUIREMENT FOR JAVA:

Minimum hardware requirement to download Java on your Windows


operating system as follows:

 Minimum Windows 95 software


 IBM-compatible 486 system
 Hard Drive and Minimum of 8 MB memory
 A CD-ROM drive
 Mouse, keyboard and sound card, if required

SOFTWARE REQUIREMENT FOR JAVA:

Nowadays, Java is supported by almost every operating systems.


whether it is a Windows, Macintosh and Unix all supports the Java
application development. So you can download any of the operating
system on your personal computer. Here are the minimum requirement.

 Operating System
 Java SDK or JRE 1.6 or higher
 Java Servlet Container (Free Servlet Container available)
 Supported Database and library that supports the database
connection with Java.

Page 10 of 10
HARDWARE REQUIREMENTS FOR MYSQL:

MySQL Workbench requires a current system to run smoothly. The


minimum hardware requirements are:

 CPU: Intel Core or Xeon 3GHz (or Dual Core 2GHz) or equal AMD
CPU

 Cores: Single (Dual/Quad Core is recommended)


 RAM: 4 GB (6 GB recommended)
 Graphic Accelerators: nVidia or ATI with support of OpenGL 1.5 or
higher

 Display Resolution: 1280×1024 is recommended, 1024×768 is


minimum.

SOFTWARE REQUIREMENTS FOR MYSQL:

The following operating systems are officially supported:

 Windows 7 (64-bit, Professional level or higher)


 Mac OS X 10.6.1+
 Ubuntu 9.10 (64bit)
 Ubuntu 8.04 (32bit/64bit)
 For convenience the following builds are also available:
 Windows XP SP3, Vista
 Mac OSX (10.5 and 10.6) Intel
 Ubuntu 8.04 (i386/x64)
 Ubuntu 9.04 (i386/x64)
 Fedora 11 (i386/x64)
 MySQL Workbench also has the following general requirements:
 The Microsoft .NET 3.5 Framework.
 Cairo 1.6.0 or later
 glib-2.10
Page 11 of 11
 libxml-2.6
 libsigc++ 2.0
 pcre
 libzip

ONLINE QUIZ SAMPLE 1

To create an Online Quiz Program related to Indian states and their


capitals using both Java and SQL

SQL Source Code

Create the database for the online quiz

CREATE DATABASE IF NOT EXISTS OnlineQuiz;

Use the created database

USE OnlineQuiz;

Table to store quiz questions

CREATE TABLE Questions (

question_id INT AUTO_INCREMENT PRIMARY KEY,

question_text VARCHAR(255) NOT NULL

);

Table to store options for each question

CREATE TABLE Options (

option_id INT AUTO_INCREMENT PRIMARY KEY,

question_id INT,

Page 12 of 12
option_text VARCHAR(255) NOT NULL,

FOREIGN KEY (question_id) REFERENCES Questions(question_id)

);

Table to store correct answers

CREATE TABLE Answers (

question_id INT,

correct_option INT, Refers to the option_id of the correct answer

FOREIGN KEY (question_id) REFERENCES Questions(question_id),

FOREIGN KEY (correct_option) REFERENCES Options(option_id)

);

Insert sample questions related to Indian states and their capitals

INSERT INTO Questions (question_text) VALUES

('What is the capital of Andhra Pradesh?'),

('What is the capital of Maharashtra?'),

('What is the capital of Tamil Nadu?'),

('What is the capital of Uttar Pradesh?'),

('What is the capital of West Bengal?');

Insert options for each question

INSERT INTO Options (question_id, option_text) VALUES

(1, 'Hyderabad'),

(1, 'Amaravati'),

(1, 'Visakhapatnam'),

(1, 'Vijayawada'),

Page 13 of 13
(2, 'Mumbai'),

(2, 'Pune'),

(2, 'Nagpur'),

(2, 'Thane'),

(3, 'Chennai'),

(3, 'Madurai'),

(3, 'Coimbatore'),

(3, 'Trichy'),

(4, 'Lucknow'),

(4, 'Varanasi'),

(4, 'Agra'),

(4, 'Kanpur'),

(5, 'Kolkata'),

(5, 'Darjeeling'),

(5, 'Siliguri'),

(5, 'Asansol');

Insert correct answers (option_ids for the correct answers)

INSERT INTO Answers (question_id, correct_option) VALUES

(1, 2), Amaravati is the correct answer for Question 1

(2, 1), Mumbai is the correct answer for Question 2

(3, 1), Chennai is the correct answer for Question 3

(4, 1), Lucknow is the correct answer for Question 4

(5, 1); Kolkata is the correct answer for Question 5

Page 14 of 14
1. Questions Table

question_id question_text
1 What is the capital of Andhra Pradesh?
2 What is the capital of Maharashtra?
3 What is the capital of Tamil Nadu?
4 What is the capital of Uttar Pradesh?
5 What is the capital of West Bengal?

Options Table

option_id question_id option_text


1 1 Hyderabad
2 1 Amaravati
3 1 Visakhapatnam
4 1 Vijayawada
5 2 Mumbai
6 2 Pune
7 2 Nagpur
8 2 Thane
9 3 Chennai
10 3 Madurai
11 3 Coimbatore
12 3 Tricky
13 4 Lucknow
14 4 Varanasi
15 4 Agra
16 4 Kanpur
17 5 Kolkata
18 5 Darjeeling
19 5 Siliguri

Page 15 of 15
option_id question_id option_text
20 5 Asansol

3. Answers Table

question_id correct_option
1 2
2 5
3 9
4 13
5 17

Java Source Code (Online Quiz Program)

import java.sql.*;

import java.util.*;

public class OnlineQuiz {

// Database connection parameters

private static final String URL = "jdbc:mysql://localhost:3306/OnlineQuiz";

private static final String USER = "root"; // Change if your MySQL user is
different

private static final String PASSWORD = "password"; // Change to your


MySQL password

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

int score = 0;

try {

// Establish the database connection

Connection connection = DriverManager.getConnection(URL, USER,


PASSWORD);

Page 16 of 16
Statement statement = connection.createStatement();

// Fetch all the questions from the database

String getQuestionsQuery = "SELECT * FROM Questions";

ResultSet questionResultSet =
statement.executeQuery(getQuestionsQuery);

while (questionResultSet.next()) {

int questionId = questionResultSet.getInt("question_id");

String questionText = questionResultSet.getString("question_text");

System.out.println(questionText);

System.out.println("Choose the correct option:");

// Fetch options for the current question

String getOptionsQuery = "SELECT * FROM Options WHERE question_id


= " + questionId;

ResultSet optionsResultSet = statement.executeQuery(getOptionsQuery);

int optionIndex = 1;

while (optionsResultSet.next()) {

String optionText = optionsResultSet.getString("option_text");

System.out.println(optionIndex + ". " + optionText);

optionIndex++;

// Get the correct answer for the current question

String getCorrectAnswerQuery = "SELECT correct_option FROM Answers


WHERE question_id = " + questionId;

ResultSet correctAnswerResultSet =
statement.executeQuery(getCorrectAnswerQuery);

correctAnswerResultSet.next();

Page 17 of 17
int correctOption = correctAnswerResultSet.getInt("correct_option");

// Taking user's answer

System.out.print("Enter your answer (1-4): ");

int userAnswer = scanner.nextInt();

// Check if the answer is correct

if (userAnswer == correctOption) {

System.out.println("Correct!\n");

score++;

} else {

System.out.println("Wrong! The correct answer is option " +


correctOption + ".\n");

// Show the final score

System.out.println("Quiz Over!");

System.out.println("Your final score: " + score);

connection.close();

} catch (SQLException e) {

e.printStackTrace();

} finally {

scanner.close();

Page 18 of 18
Sample Output:

What is the capital of Andhra Pradesh?


Choose the correct option:
1. Hyderabad
2. Amaravati
3. Visakhapatnam
4. Vijayawada
Enter your answer (1-4): 2
Correct!

What is the capital of Maharashtra?


Choose the correct option:
1. Mumbai
2. Pune
3. Nagpur
4. Thane
Enter your answer (1-4): 1
Correct!

...
Quiz Over!
Your final score: 5

ONLINE QUIZ SAMPLE 2

Page 19 of 19
Java AND SQL Source Code (Online Quiz Program) This Java program
will ask questions related to historical dates

SQL Source Code (Database Setup)


Create the database for the online quiz
CREATE DATABASE IF NOT EXISTS OnlineQuiz;
Use the created database
USE OnlineQuiz;
Table to store quiz questions
CREATE TABLE Questions (
question_id INT AUTO_INCREMENT PRIMARY KEY,
question_text VARCHAR(255) NOT NULL
);
Table to store options for each question
CREATE TABLE Options (
option_id INT AUTO_INCREMENT PRIMARY KEY,
question_id INT,
option_text VARCHAR(255) NOT NULL,
FOREIGN KEY (question_id) REFERENCES Questions(question_id)
);
Table to store correct answers
CREATE TABLE Answers (
question_id INT,
correct_option INT, -- Refers to the option_id of the correct answer
FOREIGN KEY (question_id) REFERENCES Questions(question_id),
FOREIGN KEY (correct_option) REFERENCES Options(option_id)
);
Insert sample questions related to historical dates
INSERT INTO Questions (question_text) VALUES
('In what year did World War I start?'),
('Who was the first President of the United States?'),
('In which year did India gain independence from the British Empire?'),
('When did the Titanic sink?'),
('What year did the Berlin Wall fall?');

Insert options for each question

Page 20 of 20
INSERT INTO Options (question_id, option_text) VALUES
(1, '1912'),
(1, '1914'),
(1, '1916'),
(1, '1918'),
(2, 'George Washington'),
(2, 'Thomas Jefferson'),
(2, 'Abraham Lincoln'),
(2, 'John Adams'),
(3, '1947'),
(3, '1950'),
(3, '1952'),
(3, '1965'),
(4, '1909'),
(4, '1912'),
(4, '1915'),
(4, '1919'),
(5, '1985'),
(5, '1989'),
(5, '1991'),
(5, '1995');

Insert correct answers (option_ids for the correct answers)


INSERT INTO Answers (question_id, correct_option) VALUES
(1, 2), -- 1914 is the correct answer for Question 1 (World War I start)
(2, 1), -- George Washington is the correct answer for Question 2
(3, 1), -- 1947 is the correct answer for Question 3 (India independence)
(4, 2), -- 1912 is the correct answer for Question 4 (Titanic sinking)
(5, 2); -- 1989 is the correct answer for Question 5 (Berlin Wall fall)

Questions;

question_id question_text

1 In what year did World War I start?

2 Who was the first President of the United States?

3 In which year did India gain independence from the British

Page 21 of 21
question_id question_text

Empire?

4 When did the Titanic sink?

5 What year did the Berlin Wall fall?

Options Table:
option_id question_id option_text

1 1 1912

2 1 1914

3 1 1916

4 1 1918

5 2 George Washington

6 2 Thomas Jefferson

7 2 Abraham Lincoln

8 2 John Adams

9 3 1947

10 3 1950

11 3 1952

12 3 1965

13 4 1909

14 4 1912

15 4 1915

16 4 1919

17 5 1985

Page 22 of 22
option_id question_id option_text

18 5 1989

19 5 1991

20 5 1995

Answers Table:
question_id correct_option

1 2

2 1

3 1

4 2

5 2

Java Source Code (Online Quiz Program)


import java.sql.*;
import java.util.*;

public class HistoricalQuiz {


// Database connection parameters
private static final String URL = "jdbc:mysql://localhost:3306/OnlineQuiz";
private static final String USER = "root"; // Update this to your MySQL
username
private static final String PASSWORD = "password"; // Update this to
your MySQL password

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);
int score = 0;

Page 23 of 23
try {
// Establish the database connection
Connection connection = DriverManager.getConnection(URL, USER,
PASSWORD);
Statement statement = connection.createStatement();

// Fetch all the questions from the database


String getQuestionsQuery = "SELECT * FROM Questions";
ResultSet questionResultSet =
statement.executeQuery(getQuestionsQuery);

while (questionResultSet.next()) {
int questionId = questionResultSet.getInt("question_id");
String questionText = questionResultSet.getString("question_text");

System.out.println(questionText);
System.out.println("Choose the correct option:");

// Fetch options for the current question


String getOptionsQuery = "SELECT * FROM Options WHERE question_id
= " + questionId;
ResultSet optionsResultSet = statement.executeQuery(getOptionsQuery);

int optionIndex = 1;
while (optionsResultSet.next()) {
String optionText = optionsResultSet.getString("option_text");
System.out.println(optionIndex + ". " + optionText);
optionIndex++;
}

// Get the correct answer for the current question


String getCorrectAnswerQuery = "SELECT correct_option FROM Answers
WHERE question_id = " + questionId;
ResultSet correctAnswerResultSet =
statement.executeQuery(getCorrectAnswerQuery);
correctAnswerResultSet.next();
int correctOption = correctAnswerResultSet.getInt("correct_option");

// Taking user's answer


System.out.print("Enter your answer (1-4): ");

Page 24 of 24
int userAnswer = scanner.nextInt();

// Check if the answer is correct


if (userAnswer == correctOption) {
System.out.println("Correct!\n");
score++;
} else {
System.out.println("Wrong! The correct answer is option " +
correctOption + ".\n");
}
}

// Show the final score


System.out.println("Quiz Over!");
System.out.println("Your final score: " + score);
connection.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
scanner.close();
}
}
}
Sample Output:
In what year did World War I start?
Choose the correct option:
1. 1912
2. 1914
3. 1916
4. 1918
Enter your answer (1-4): 2
Correct!

Who was the first President of the United States?


Choose the correct option:
1. George Washington
2. Thomas Jefferson
3. Abraham Lincoln
4. John Adams
Enter your answer (1-4): 1

Page 25 of 25
Correct!

...
Quiz Over!
Your final score: 5

ONLINE QUIZ SAMPLE 3

Online Quiz Program that asks questions related to Traditional Foods in


India

SQL Source Code (Database Setup)


Create the database for the online quiz
CREATE DATABASE IF NOT EXISTS OnlineQuiz;

Use the created database


USE OnlineQuiz;

Table to store quiz questions


CREATE TABLE Questions (
question_id INT AUTO_INCREMENT PRIMARY KEY,
question_text VARCHAR(255) NOT NULL
);

Page 26 of 26
Table to store options for each question
CREATE TABLE Options (
option_id INT AUTO_INCREMENT PRIMARY KEY,
question_id INT,
option_text VARCHAR(255) NOT NULL,
FOREIGN KEY (question_id) REFERENCES Questions(question_id)
);

Table to store correct answers


CREATE TABLE Answers (
question_id INT,
correct_option INT, -- Refers to the option_id of the correct answer
FOREIGN KEY (question_id) REFERENCES Questions(question_id),
FOREIGN KEY (correct_option) REFERENCES Options(option_id)
);

Insert sample questions related to traditional foods in India


INSERT INTO Questions (question_text) VALUES
('What is the traditional dish of Punjab?'),
('Which Indian dish is made from fermented rice and black lentils?'),
('What is the staple food of Kerala?'),
('Which Indian state is famous for its "Pakhala Bhata"?'),
('Which is the popular sweet dish of Rajasthan?');

Insert options for each question


INSERT INTO Options (question_id, option_text) VALUES
(1, 'Biryani'),
(1, 'Butter Chicken'),
(1, 'Sarson Da Saag with Makki Di Roti'),
(1, 'Dosa'),
(2, 'Chole Bhature'),
(2, 'Dosa'),
(2, 'Idli and Sambar'),
(2, 'Biryani'),
(3, 'Idli and Sambar'),
(3, 'Pav Bhaji'),
(3, 'Khichdi'),
(3, 'Fish Curry and Rice'),
(4, 'Odisha'),

Page 27 of 27
(4, 'Kolkata'),
(4, 'Gujarat'),
(4, 'Andhra Pradesh'),
(5, 'Ghevar'),
(5, 'Gulab Jamun'),
(5, 'Rasgulla'),
(5, 'Kachori');

Insert correct answers (option_ids for the correct answers)


INSERT INTO Answers (question_id, correct_option) VALUES
(1, 3), -- Sarson Da Saag with Makki Di Roti is the correct answer for
Question 1
(2, 3), -- Idli and Sambar is the correct answer for Question 2
(3, 1), -- Idli and Sambar is the correct answer for Question 3 (Kerala)
(4, 1), -- Odisha is the correct answer for Question 4 (Pakhala Bhata)
(5, 1); -- Ghevar is the correct answer for Question 5 (Rajasthan sweet
dish)

Uestions Table

question_id question_text
1 What is the traditional dish of Punjab?
Which Indian dish is made from fermented rice and black
2
lentils?
3 What is the staple food of Kerala?
4 Which Indian state is famous for its "Pakhala Bhata"?
5 Which is the popular sweet dish of Rajasthan?
2. Options Table

option_id question_id option_text


1 1 Biryani
2 1 Butter Chicken
3 1 Sarson Da Saag with Makki Di Roti

Page 28 of 28
option_id question_id option_text
4 1 Dosa
5 2 Chole Bhature
6 2 Dosa
7 2 Idli and Sambar
8 2 Biryani
9 3 Idli and Sambar
10 3 Pav Bhaji
11 3 Khichdi
12 3 Fish Curry and Rice
13 4 Odisha
14 4 Kolkata
15 4 Gujarat
16 4 Andhra Pradesh
17 5 Ghevar
18 5 Gulab Jamun
19 5 Rasgulla
20 5 Kachori

3. Answers Table

question_id correct_option
1 3
2 7
3 9
4 13
5 17

Java Source Code (Online Quiz Program)


import java.sql.*;
import java.util.*;

public class TraditionalFoodsQuiz {


// Database connection parameters
private static final String URL = "jdbc:mysql://localhost:3306/OnlineQuiz";
private static final String USER = "root"; // Update with your MySQL

Page 29 of 29
username
private static final String PASSWORD = "password"; // Update with your
MySQL password

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);
int score = 0;

try {
// Establish the database connection
Connection connection = DriverManager.getConnection(URL, USER,
PASSWORD);
Statement statement = connection.createStatement();

// Fetch all the questions from the database


String getQuestionsQuery = "SELECT * FROM Questions";
ResultSet questionResultSet =
statement.executeQuery(getQuestionsQuery);

while (questionResultSet.next()) {
int questionId = questionResultSet.getInt("question_id");
String questionText = questionResultSet.getString("question_text");

System.out.println(questionText);
System.out.println("Choose the correct option:");

// Fetch options for the current question


String getOptionsQuery = "SELECT * FROM Options WHERE question_id
= " + questionId;
ResultSet optionsResultSet = statement.executeQuery(getOptionsQuery);

int optionIndex = 1;
while (optionsResultSet.next()) {
String optionText = optionsResultSet.getString("option_text");
System.out.println (optionIndex + ". " + OptionText);
optionIndex++;
}

// Get the correct answer for the current question


String getCorrectAnswerQuery = "SELECT correct_option FROM Answers

Page 30 of 30
WHERE question_id = " + questionId;
ResultSet correctAnswerResultSet =
statement.executeQuery(getCorrectAnswerQuery);
correctAnswerResultSet.next();
int correctOption = correctAnswerResultSet.getInt("correct_option");

// Taking user's answer


System.out.print("Enter your answer (1-4): ");
int userAnswer = scanner.nextInt();

// Check if the answer is correct


if (userAnswer == correctOption) {
System.out.println("Correct!\n");
score++;
} else {
System.out.println("Wrong! The correct answer is option " +
correctOption + ".\n");
}
}

// Show the final score


System.out.println("Quiz Over!");
System.out.println("Your final score: " + score);
connection.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
scanner.close();
}
}
}
Sample Output:
What is the traditional dish of Punjab?
Choose the correct option:
1. Biryani
2. Butter Chicken
3. Sarson Da Saag with Makki Di Roti
4. Dosa
Enter your answer (1-4): 3
Correct!

Page 31 of 31
Which Indian dish is made from fermented rice and black lentils?
Choose the correct option:
1. Chole Bhature
2. Dosa
3. Idli and Sambar
4. Biryani
Enter your answer (1-4): 3
Correct!

...
Quiz Over!
Your final score: 5

ONLINE QUIZ SAMPLE 4

Java AND SQL Source Code (Online Quiz Program) This Java program
will ask questions related to continent

SQL Source Code (Database Setup)

We will create a database that stores questions related to continents,


the options for each question, and the correct answers.

sql
Copy code
Create the database for the online quiz
CREATE DATABASE IF NOT EXISTS OnlineQuiz;

Use the created database


USE OnlineQuiz;

Table to store quiz questions


CREATE TABLE Questions (
question_id INT AUTO_INCREMENT PRIMARY KEY,
question_text VARCHAR(255) NOT NULL
);

Table to store options for each question


CREATE TABLE Options (

Page 32 of 32
option_id INT AUTO_INCREMENT PRIMARY KEY,
question_id INT,
option_text VARCHAR(255) NOT NULL,
FOREIGN KEY (question_id) REFERENCES Questions(question_id)
);

Table to store correct answers


CREATE TABLE Answers (
question_id INT,
correct_option INT, -- Refers to the option_id of the correct answer
FOREIGN KEY (question_id) REFERENCES Questions(question_id),
FOREIGN KEY (correct_option) REFERENCES Options(option_id)
);

Insert sample questions related to continents


INSERT INTO Questions (question_text) VALUES
('Which is the largest continent by area?'),
('Which continent is known as the "Dark Continent"?'),
('Which continent is located entirely in the Southern Hemisphere?'),
('Which continent is home to the Sahara Desert?'),
('Which continent has the most countries?');

Insert options for each question


INSERT INTO Options (question_id, option_text) VALUES
(1, 'Asia'),
(1, 'Africa'),
(1, 'North America'),
(1, 'Europe'),
(2, 'Asia'),
(2, 'Africa'),
(2, 'Australia'),
(2, 'South America'),
(3, 'South America'),
(3, 'Africa'),
(3, 'Australia'),
(3, 'Antarctica'),
(4, 'Africa'),
(4, 'Australia'),
(4, 'Asia'),
(4, 'North America'),

Page 33 of 33
(5, 'Africa'),
(5, 'Europe'),
(5, 'Asia'),
(5, 'North America');
Insert correct answers (option_ids for the correct answers)
INSERT INTO Answers (question_id, correct_option) VALUES
(1, 1), -- Asia is the largest continent by area
(2, 2), -- Africa is known as the "Dark Continent"
(3, 3), -- Australia is located entirely in the Southern Hemisphere
(4, 1), -- Africa is home to the Sahara Desert
(5, 1); -- Africa has the most countries

1. Questions Table

question_id question_text
1 Which is the largest continent by area?
2 Which continent is known as the "Dark Continent"?
Which continent is located entirely in the Southern
3
Hemisphere?
4 Which continent is home to the Sahara Desert?
5 Which continent has the most countries?

2. Options Table

option_id question_id option_text


1 1 Asia
2 1 Africa
3 1 North America
4 1 Europe
5 2 Asia
6 2 Africa
7 2 Australia
8 2 South America
9 3 South America
10 3 Africa
11 3 Australia

Page 34 of 34
option_id question_id option_text
12 3 Antarctica
13 4 Africa
14 4 Australia
15 4 Asia
16 4 North America
17 5 Africa
18 5 Europe
19 5 Asia
20 5 North America

3. Answers Table

question_id correct_option
1 1
2 2
3 3
4 1
5 1

Java Source Code (Online Quiz Program)

This Java program will connect to the SQL database and fetch the
questions, options, and correct answers, then display them to the user
for answering and track the score.

java
Copy code
import java.sql.*;
import java.util.*;

public class ContinentsQuiz {


// Database connection parameters
private static final String URL = "jdbc:mysql://localhost:3306/OnlineQuiz";
private static final String USER = "root"; // Update with your MySQL
username
private static final String PASSWORD = "password"; // Update with your

Page 35 of 35
MySQL password

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);
int score = 0;

try {
// Establish the database connection
Connection connection = DriverManager.getConnection(URL, USER,
PASSWORD);
Statement statement = connection.createStatement();

// Fetch all the questions from the database


String getQuestionsQuery = "SELECT * FROM Questions";
ResultSet questionResultSet =
statement.executeQuery(getQuestionsQuery);

while (questionResultSet.next()) {
int questionId = questionResultSet.getInt ("question_id");
String questionText = questionResultSet.getString("question_text");

System.out.println(questionText);
System.out.println("Choose the correct option:");

// Fetch options for the current question


String getOptionsQuery = "SELECT * FROM Options WHERE question_id
= " + questionId;
ResultSet optionsResultSet = statement.executeQuery(getOptionsQuery);

int optionIndex = 1;
while (optionsResultSet.next()) {
String optionText = optionsResultSet.getString("option_text");
System.out.println (optionIndex + ". " + optionText);
optionIndex++;
}

// Get the correct answer for the current question


String getCorrectAnswerQuery = "SELECT correct_option FROM Answers
WHERE question_id = " + questionId;
ResultSet correctAnswerResultSet =

Page 36 of 36
statement.executeQuery(getCorrectAnswerQuery);
correctAnswerResultSet.next();
int correctOption = correctAnswerResultSet.getInt("correct_option");

// Taking user's answer


System.out.print("Enter your answer (1-4): ");
int userAnswer = scanner.nextInt();

// Check if the answer is correct


if (userAnswer == correctOption) {
System.out.println("Correct!\n");
score++;
} else {
System.out.println("Wrong! The correct answer is option " +
correctOption + ".\n");
}
}

// Show the final score


System.out.println("Quiz Over!");
System.out.println("Your final score: " + score);
connection.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
scanner.close();
}
}
}

Sample Output:

Which is the largest continent by area?


Choose the correct option:
1. Asia
2. Africa
3. North America
4. Europe
Enter your answer (1-4): 1

Page 37 of 37
Correct!

Which continent is known as the "Dark Continent"?


Choose the correct option:
1. Asia
2. Africa
3. Australia
4. South America
Enter your answer (1-4): 2
Correct!

Which continent is located entirely in the Southern Hemisphere?


Choose the correct option:
1. South America
2. Africa
3. Australia
4. Antarctica
Enter your answer (1-4): 3
Correct!

Which continent is home to the Sahara Desert?


Choose the correct option:
1. Africa
2. Australia
3. Asia
4. North America
Enter your answer (1-4): 1
Correct!

Which continent has the most countries?


Choose the correct option:
1. Africa
2. Europe
3. Asia
4. North America
Enter your answer (1-4): 1
Correct!Quiz
Over!
Your final score: 5

Page 38 of 38
ONLINE QUIZ SAMPLE 5

Java AND SQL Source Code (Online Quiz Program) This Java program
will ask questions related to stars

SQL Source Code (Database Setup)

Create the database for the online quiz


CREATE DATABASE IF NOT EXISTS OnlineQuiz;

Use the created database


USE OnlineQuiz;

-Table to store quiz questions


CREATE TABLE Questions (
question_id INT AUTO_INCREMENT PRIMARY KEY,
question_text VARCHAR(255) NOT NULL
);

Table to store options for each question


CREATE TABLE Options (
option_id INT AUTO_INCREMENT PRIMARY KEY,
question_id INT,
option_text VARCHAR(255) NOT NULL,
FOREIGN KEY (question_id) REFERENCES Questions(question_id)
);

Table to store correct answers


CREATE TABLE Answers (
question_id INT,
correct_option INT, -- Refers to the option_id of the correct answer
FOREIGN KEY (question_id) REFERENCES Questions(question_id),
FOREIGN KEY (correct_option) REFERENCES Options(option_id)
);

Insert sample questions related to stars


INSERT INTO Questions (question_text) VALUES
('Which star is the closest to Earth?'),

Page 39 of 39
('What is the name of the largest star in the Milky Way?'),
('Which star is known as the "Evening Star"?'),
('What type of star is the Sun?'),
('Which star is the brightest in the night sky?');

Insert options for each question


INSERT INTO Options (question_id, option_text) VALUES
(1, 'Proxima Centauri'),
(1, 'Sirius'),
(1, 'Alpha Centauri A'),
(1, 'Betelgeuse'),
(2, 'Sirius'),
(2, 'VY Canis Majoris'),
(2, 'Betelgeuse'),
(2, 'Alpha Centauri A'),
(3, 'Mars'),
(3, 'Venus'),
(3, 'Jupiter'),
(3, 'Mercury'),
(4, 'Red Giant'),
(4, 'Main Sequence'),
(4, 'White Dwarf'),
(4, 'Supernova'),
(5, 'Sirius'),
(5, 'Alpha Centauri A'),
(5, 'Betelgeuse'),
(5, 'Rigel');

Insert correct answers (option_ids for the correct answers)


INSERT INTO Answers (question_id, correct_option) VALUES
(1, 1), -- Proxima Centauri is the closest star to Earth
(2, 2), -- VY Canis Majoris is the largest star in the Milky Way
(3, 2), -- Venus is known as the "Evening Star"
(4, 2), -- The Sun is a Main Sequence star
(5, 1); -- Sirius is the brightest star in the night sky

1. Questions Table

Page 40 of 40
question_id question_text
1 Which star is the closest to Earth?
What is the name of the largest star in the
2
Milky Way?
3 Which star is known as the "Evening Star"?
4 What type of star is the Sun?
5 Which star is the brightest in the night sky?

2. Options Table

option_id question_id option_text


1 1 Proxima Centauri
2 1 Sirius
3 1 Alpha Centauri A
4 1 Betelgeuse
5 2 Sirius
6 2 VY Canis Majoris
7 2 Betelgeuse
8 2 Alpha Centauri A
9 3 Mars
10 3 Venus
11 3 Jupiter
12 3 Mercury
13 4 Red Giant
14 4 Main Sequence
15 4 White Dwarf
16 4 Supernova
17 5 Sirius
18 5 Alpha Centauri A
19 5 Betelgeuse
20 5 Rigel

3. Answers Table

question_id correct_option
1 1

Page 41 of 41
question_id correct_option
2 2
3 2
4 2
5 1

Java Source Code (Online Quiz Program)

import java.sql.*;
import java.util.*;

public class StarsQuiz {


// Database connection parameters
private static final String URL = "jdbc:mysql:
//localhost:3306/OnlineQuiz";
private static final String USER = "root"; // Update with your MySQL
username
private static final String PASSWORD = "password"; // Update with your
MySQL password

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);
int score = 0;

try {
// Establish the database connection
Connection connection = DriverManager.getConnection(URL, USER,
PASSWORD);
Statement statement = connection.createStatement();

// Fetch all the questions from the database


String getQuestionsQuery = "SELECT * FROM Questions";
ResultSet questionResultSet =

Page 42 of 42
statement.executeQuery(getQuestionsQuery);

while (questionResultSet.next()) {
int questionId = questionResultSet.getInt("question_id");
String questionText = questionResultSet.getString("question_text");

System.out.println(questionText);
System.out.println("Choose the correct option:");

// Fetch options for the current question


String getOptionsQuery = "SELECT * FROM Options WHERE question_id
= " + questionId;
ResultSet optionsResultSet = statement.executeQuery(getOptionsQuery);

int optionIndex = 1;
while (optionsResultSet.next()) {
String optionText = optionsResultSet.getString("option_text");
System.out.println(optionIndex + ". " + optionText);
optionIndex++;
}

// Get the correct answer for the current question


String getCorrectAnswerQuery = "SELECT correct_option FROM Answers
WHERE question_id = " + questionId;
ResultSet correctAnswerResultSet =
statement.executeQuery(getCorrectAnswerQuery);
correctAnswerResultSet.next();
int correctOption = correctAnswerResultSet.getInt("correct_option");

// Taking user's answer


System.out.print("Enter your answer (1-4): ");
int userAnswer = scanner.nextInt();

// Check if the answer is correct


if (userAnswer == correctOption) {
System.out.println("Correct!\n");
score++;
} else {
System.out.println("Wrong! The correct answer is option " +
correctOption + ".\n");

Page 43 of 43
}
}

// Show the final score


System.out.println("Quiz Over!");
System.out.println("Your final score: " + score);
connection.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
scanner.close();
}
}
}

Sample Output:

Which star is the closest to Earth?


Choose the correct option:
1. Proxima Centauri
2. Sirius
3. Alpha Centauri A
4. Betelgeuse
Enter your answer (1-4): 1
Correct!

What is the name of the largest star in the Milky Way?


Choose the correct option:
1. Sirius
2. VY Canis Majoris
3. Betelgeuse
4. Alpha Centauri A
Enter your answer (1-4): 2
Correct!

Which star is known as the "Evening Star"?


Choose the correct option:
1. Mars
2. Venus
3. Jupiter

Page 44 of 44
4. Mercury
Enter your answer (1-4): 2
Correct!

What type of star is the Sun?


Choose the correct option:
1. Red Giant
2. Main Sequence
3. White Dwarf
4. Supernova
Enter your answer (1-4): 2
Correct!
Which star is the brightest in the night sky?
Choose the correct option:
1. Sirius
2. Alpha Centauri A
3. Betelgeuse
4. Rigel
Enter your answer (1-4): 1
Correct!
Quiz Over!
Your final score: 5

Page 45 of 45
ONLINE QUIZ SAMPLE 6

Java AND SQL Source Code (Online Quiz Program) This Java program
will ask questions related to friendship game

SQL Source Code (Database Setup)

Create the database for the online quiz

CREATE DATABASE IF NOT EXISTS OnlineQuiz;

Use the created database

USE OnlineQuiz;

Table to store quiz questions

CREATE TABLE Questions (

question_id INT AUTO_INCREMENT PRIMARY KEY,

question_text VARCHAR(255) NOT NULL

);

Table to store options for each question

CREATE TABLE Options (

option_id INT AUTO_INCREMENT PRIMARY KEY,

question_id INT,

option_text VARCHAR(255) NOT NULL,

FOREIGN KEY (question_id) REFERENCES Questions(question_id)

);

Table to store correct answers

Page 46 of 46
CREATE TABLE Answers (

question_id INT,

correct_option INT, -- Refers to the option_id of the correct answer

FOREIGN KEY (question_id) REFERENCES Questions(question_id),

FOREIGN KEY (correct_option) REFERENCES Options(option_id)

);

Insert sample questions related to the theme of Friendship Game

INSERT INTO Questions (question_text) VALUES

('Which of the following is a popular game played by friends?'),

('Which phrase signifies strong friendship?'),

('Which character is best known for friendship in Harry Potter?'),

('Which friendship game involves a lot of physical activity?'),

('Which game is commonly played by friends in India?');

Insert options for each question

INSERT INTO Options (question_id, option_text) VALUES

(1, 'Hide and Seek'),

(1, 'Football'),

(1, 'Truth or Dare'),

(1, 'Chess'),

(2, 'Friends Forever'),

(2, 'Besties for Life'),

(2, 'Partners in Crime'),

(2, 'Soulmates'),

(3, 'Harry Potter'),

Page 47 of 47
(3, 'Ron Weasley'),

(3, 'Hermione Granger'),

(3, 'Dumbledore'),

(4, 'Monopoly'),

(4, 'Football'),

(4, 'Basketball'),

(4, 'Truth or Dare'),

(5, 'Kabaddi'),

(5, 'Ludo'),

(5, 'Carrom'),

(5, 'Pictionary');

Insert correct answers (option_ids for the correct answers)

INSERT INTO Answers (question_id, correct_option) VALUES

(1, 3), -- Truth or Dare is a popular friendship game

(2, 1), -- "Friends Forever" signifies strong friendship

(3, 2), -- Ron Weasley is known for friendship in Harry Potter

(4, 3), -- Basketball involves a lot of physical activity

(5, 1); -- Kabaddi is commonly played by friends in India

1. Questions Table

Page 48 of 48
question_id question_text
1 Which of the following is a popular game played by friends?
2 Which phrase signifies strong friendship?
Which character is best known for friendship in Harry
3
Potter?
4 Which friendship game involves a lot of physical activity?
5 Which game is commonly played by friends in India?

2. Options Table

option_id question_id option_text


1 1 Hide and Seek
2 1 Football
3 1 Truth or Dare
4 1 Chess
5 2 Friends Forever
6 2 Besties for Life
7 2 Partners in Crime
8 2 Soulmates
9 3 Harry Potter
10 3 Ron Weasley
11 3 Hermione Granger
12 3 Dumbledore
13 4 Monopoly
14 4 Football
15 4 Basketball
16 4 Truth or Dare
17 5 Kabaddi
18 5 Ludo
19 5 Carrom
20 5 Pictionary

3. Answers Table
question_id correct_option
1 3
2 1

Page 49 of 49
question_id correct_option
3 2
4 3
5 1

Java Source Code (Online Quiz Program)

import java.sql.*;

import java.util.*;

public class ChessQuiz {


// Database connection parameters
private static final String URL = "jdbc:mysql://localhost:3306/OnlineQuiz";
private static final String USER = "root"; // Update with your MySQL
username
private static final String PASSWORD = "password"; // Update with your
MySQL password

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);
int score = 0;

try {
// Establish the database connection
Connection connection = DriverManager.getConnection(URL, USER,
PASSWORD);
Statement statement = connection.createStatement();

// Fetch all the questions from the database


String getQuestionsQuery = "SELECT * FROM Questions";
ResultSet questionResultSet =
statement.executeQuery(getQuestionsQuery);

while (questionResultSet.next()) {
int questionId = questionResultSet.getInt("question_id");
String questionText = questionResultSet.getString("question_text");

System.out.println(questionText);
System.out.println("Choose the correct option:");

Page 50 of 50
// Fetch options for the current question
String getOptionsQuery = "SELECT * FROM Options WHERE question_id
= " + questionId;
ResultSet optionsResultSet = statement.executeQuery(getOptionsQuery);

int optionIndex = 1;
while (optionsResultSet.next()) {
String optionText = optionsResultSet.getString("option_text");
System.out.println(optionIndex + ". " + optionText);
optionIndex++;
}

// Get the correct answer for the current question


String getCorrectAnswerQuery = "SELECT correct_option FROM Answers
WHERE question_id = " + questionId;
ResultSet correctAnswerResultSet =
statement.executeQuery(getCorrectAnswerQuery);
correctAnswerResultSet.next();
int correctOption = correctAnswerResultSet.getInt("correct_option");

// Taking user's answer


System.out.print("Enter your answer (1-4): ");
int userAnswer = scanner.nextInt();

// Check if the answer is correct


if (userAnswer == correctOption) {
System.out.println("Correct!\n");
score++;
} else {
System.out.println("Wrong! The correct answer is option " +
correctOption + ".\n");
}
}

// Show the final score


System.out.println("Quiz Over!");
System.out.println("Your final score: " + score);
connection.close();
} catch (SQLException e) {

Page 51 of 51
e.printStackTrace();
} finally {
scanner.close();
}
}
}

Sample Output:

markdown
Copy code
Who is known as the greatest chess player of all time?
Choose the correct option:
1. Bobby Fischer
2. Magnus Carlen
3. Garry Kasparov
4. Anatoly Karpov
Enter your answer (1-4): 2
Correct!

What is the chess piece that moves in an L-shape?


Choose the correct option:
1. Rook
2. Bishop
3. Knight
4. Queen
Enter your answer (1-4): 3
Correct!

How many squares are there on a standard chessboard?


Choose the correct option:
1. 64
2. 36
3. 72
4. 50
Enter your answer (1-4): 1
Correct!

What is the term for a move where the king is threatened by an


opponent’s piece?

Page 52 of 52
Choose the correct option:
1. Checkmate
2. Check
3. Fork
4. Castling
Enter your answer (1-4): 2
Correct!

What is the name of the opening move 1. e4 in chess?


Choose the correct option:
1. King’s Gambit
2. Sicilian Defines
3. Ruy Lopez
4. King’s Pawn Opening
Enter your answer (1-4): 4
Correct!

Quiz Over!
Your final score: 5

Page 53 of 53
Conclusion:

The Online Quiz system developed using Java and SQL has successfully
met its objectives of creating an interactive, user-friendly, and data-
driven quiz platform. By leveraging Java for the user interface and SQL
for managing quiz data, the project offers a seamless user experience
and a scalable solution for administering quizzes.

The project demonstrates how Java and SQL can be integrated to build
dynamic, real-time applications that provide valuable feedback and
improve user engagement. The system is both efficient and scalable,
with the potential for future enhancements such as user authentication,
advanced analytics, and the inclusion of additional quiz features.

The Online Quiz system holds great potential for both educational and
entertainment purposes, offering an effective way to assess knowledge,
engage users, and facilitate learning. Future iterations of the system
could focus on enhancing security, adding more question types, and
integrating additional multimedia elements to create a more engaging
and comprehensive experience.

Page 54 of 54

You might also like