Online Quiz System using Java With Source Code Graphical User Interface [GUI]

Introduction:
The online Quiz Program is a Java-based Quiz System Developed using Java AWT and Swing, This program create a system which is simple yet engaging for learners as well as for educators. This System presents 10 multiple choice questions (MCQ) which is based on java programming language. You can change the questions by making changes in code . As the test completes Program will provide you detail report on your performance which will show selected answers, correct answer and total score.
Features of this System:
- Simple and easy to use GUI Interface – Developed using Java AWT and Swing for an interactive and engaging experience.
- Multiple-Choice Format – Includes 10 Java-related questions with four answer choices each.
- Detailed Report – Displays user-selected answers, correct answers, and the final score.
- Standalone Application – Runs independently without requiring an internet connection.
- Efficient Data Handling – Uses arrays and HashMap for storing questions and answers.
Required Modules:
The required modules in the online quiz system are:
java.awt.*;
– Provides classes necessary to create an applet of system (GUI) using AWT.java.awt.event.*;
– Handles user interactions such as button clicks and other events.javax.swing.*;
– It is a GUI widget toolkit Used for creating GUI components like buttons, labels,frames and other GUI component.java.util.*;
– Provides utility classes such as HashMap for storing user responses and Scanner for taking inputs from user.
How to Run the Code:
Step 1: Write the Java Program
Write the Provided Java Code in your code editor , name it as follows :
QuizProgram.java
If there are no errors, this will generate a QuizProgram.class
file.
Step 2: Run the Program
Run main class
java QuizProgram
Step 3: Interact with the Quiz
- The quiz window will open.
- Select your preferred answer with the help of radio program .
- Click “Next” to proceed.
- After the last question, click on the button “Show Results” to see your score.
Code Explanation :
1. Importing Necessary Libraries
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
java.awt.*
andjavax.swing.*
: Used for creating the graphical user interface (GUI).java.awt.event.*
: Handles user interactions (like button clicks).java.util.*
: Manages data structures likeHashMap
.
2. Declaring the Quiz
Class
class Quiz extends JFrame implements ActionListener {
- This class extends
JFrame
, meaning it creates a GUI window. - It implements
ActionListener
, allowing it to handle button clicks.
3. Declaring UI Components
JPanel panel;
JRadioButton choice1, choice2, choice3, choice4;
ButtonGroup bg;
JLabel lblmess;
JButton btnext;
JRadioButton
: Represents multiple-choice options.ButtonGroup
: Ensures only one option is selected at a time.JLabel
: Displays the question.JButton
: The “Next” button to proceed to the next question.
4. Storing Questions and Answers
String[][] qpa; // Question-Option Array
String[][] qca; // Correct Answer Array
int qaid; // Question ID
HashMap<Integer, String> map; // Stores user-selected answers
qpa
: Stores questions with multiple-choice options.qca
: Stores correct answers.qaid
: Tracks the current question.map
: Stores user-selected answers.
5. Initializing the Quiz
Quiz() {
initializedata();
setTitle("Quiz Program");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 300);
setLayout(new BorderLayout());
initializedata()
: Loads questions and answers.- Sets up the window title, size, and layout.
6. Handling Button Clicks
public void actionPerformed(ActionEvent e) {
if (btnext.getText().equals("Next")) {
if (qaid < qpa.length - 1) {
qaid++;
displayQuestion();
} else {
btnext.setText("Show Results");
}
} else if (btnext.getText().equals("Show Results")) {
showResults();
}
}
- When “Next” is clicked, it moves to the next question.
- When all questions are answered, it shows results.
7. Displaying Results
void showResults() {
int correct = 0;
for (int i = 0; i < qpa.length; i++) {
if (map.get(i).equals(qca[i][1])) {
correct++;
}
}
JOptionPane.showMessageDialog(this, "Your Score: " + correct + "/" + qpa.length);
}
Source Code:
Get Discount on Top Educational Courses
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
class Quiz extends JFrame implements ActionListener {
JPanel panel;
JRadioButton choice1;
JRadioButton choice2;
JRadioButton choice3;
JRadioButton choice4;
ButtonGroup bg;
JLabel lblmess;
JButton btnext;
String[][] qpa;
String[][] qca;
int qaid;
HashMap map;
Quiz() {
initializedata();
setTitle("Quiz Program");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(430, 350);
setLocation(300, 100);
setResizable(false);
Container cont = getContentPane();
cont.setLayout(null);
cont.setBackground(Color.GRAY);
bg = new ButtonGroup();
choice1 = new JRadioButton("Choice1", true);
choice2 = new JRadioButton("Choice2", false);
choice3 = new JRadioButton("Choice3", false);
choice4 = new JRadioButton("Choice4", false);
bg.add(choice1);
bg.add(choice2);
bg.add(choice3);
bg.add(choice4);
lblmess = new JLabel("Choose a correct answer");
lblmess.setForeground(Color.BLUE);
lblmess.setFont(new Font("Arial", Font.BOLD, 11));
btnext = new JButton("Next");
btnext.setForeground(Color.BLUE);
btnext.addActionListener(this);
panel = new JPanel();
panel.setBackground(Color.LIGHT_GRAY);
panel.setLocation(10, 10);
panel.setSize(400, 300);
panel.setLayout(new GridLayout(6, 2));
panel.add(lblmess);
panel.add(choice1);
panel.add(choice2);
panel.add(choice3);
panel.add(choice4);
panel.add(btnext);
cont.add(panel);
setVisible(true);
qaid = 0;
readqa(qaid);
}
public void actionPerformed(ActionEvent e) {
if (btnext.getText().equals("Next")) {
if (qaid >2";
qpa[6][1] = "1";
qpa[6][2] = "0";
qpa[6][3] = "3";
qpa[6][4] = "-3";
qpa[7][0] = "How to do exit a loop?";
qpa[7][1] = "Using exit";
qpa[7][2] = "Using break";
qpa[7][3] = "Using continue";
qpa[7][4] = "Using terminate";
qpa[8][0] = "What is the correct way to allocate one-dimensional array?";
qpa[8][1] = "int[size] arr=new int[]";
qpa[8][2] = "int arr[size]=new int[]";
qpa[8][3] = "int[size] arr=new int[size]";
qpa[8][4] = "int[] arr=new int[size]";
qpa[9][0] = "What is the correct way to allocate two-dimensional array?";
qpa[9][1] = "int[size][] arr=new int[][]";
qpa[9][2] = "int arr=new int[rows][cols]";
qpa[9][3] = "int arr[rows][]=new int[rows][cols]";
qpa[9][4] = "int[][] arr=new int[rows][cols]";
//qca stores pairs of question and its correct answer
qca = new String[10][2];
qca[0][0] = "How to run Java program on the command prompt?";
qca[0][1] = "java JavaProgram";
qca[1][0] = "What is the use of the println method?";
qca[1][1] = "It is used to print text on the screen with the line break.";
qca[2][0] = "How to read a character from the keyboard?";
qca[2][1] = "char c=(char)System.in.read()";
qca[3][0] = "Which one would be an int";
qca[3][1] = "2";
qca[4][0] = "How do you declare an integer variable x?";
qca[4][1] = "int x";
qca[5][0] = "How do you convert a string of number to a number?";
qca[5][1] = "int num=Integer.parseInt(str_num)";
qca[6][0] = "What is the value of x? int x=3>>2";
qca[6][1] = "0";
qca[7][0] = "How to do exit a loop?";
qca[7][1] = "Using break";
qca[8][0] = "What is the correct way to allocate one-dimensional array?";
qca[8][1] = "int[] arr=new int[size]";
qca[9][0] = "What is the correct way to allocate two-dimensional array?";
qca[9][1] = "int[][] arr=new int[rows][cols]";
//create a map object to store pairs of question and selected answer
map = new HashMap();
}
public String getSelection() {
String selectedChoice = null;
Enumeration buttons = bg.getElements();
while (buttons.hasMoreElements()) {
JRadioButton temp = (JRadioButton) buttons.nextElement();
if (temp.isSelected()) {
selectedChoice = temp.getText();
}
}
return (selectedChoice);
}
public void readqa(int qid) {
lblmess.setText(" " + qpa[qid][0]);
choice1.setText(qpa[qid][1]);
choice2.setText(qpa[qid][2]);
choice3.setText(qpa[qid][3]);
choice4.setText(qpa[qid][4]);
choice1.setSelected(true);
}
public void reset() {
qaid = 0;
map.clear();
readqa(qaid);
btnext.setText("Next");
}
public int calCorrectAnswer() {
int qnum = 10;
int count = 0;
for (int qid = 0; qid < qnum; qid++) {
if (qca[qid][1].equals(map.get(qid))) {
count++;
}
}
return count;
}
public class Report extends JFrame {
Report() {
setTitle("Answers");
setSize(850, 550);
setBackground(Color.WHITE);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose();
reset();
}
});
Draw d = new Draw();
add(d);
setVisible(true);
}
class Draw extends Canvas {
public void paint(Graphics g) {
int qnum = 10;
int x = 10;
int y = 20;
for (int i = 0; i 400) {
y = 20;
x = 450;
}
}
//show number of correct answers
int numc = calCorrectAnswer();
g.setColor(Color.BLUE);
g.setFont(new Font("Arial", Font.BOLD, 14));
g.drawString("Number of correct answers:" + numc, 300, 500);
}
}
}
}
public class QuizProgram {
public static void main(String args[]) {
new Quiz();
}
}
Output:

