Skip to content

Commit 8fc0512

Browse files
author
SOSWAL007
committed
Added Quiz applictaion in java
1 parent 765c5ef commit 8fc0512

File tree

2 files changed

+98
-0
lines changed

2 files changed

+98
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import java.util.Scanner;
2+
3+
class Question {
4+
String question;
5+
String[] options;
6+
char correctAnswer;
7+
8+
Question(String question, String[] options, char correctAnswer) {
9+
this.question = question;
10+
this.options = options;
11+
this.correctAnswer = correctAnswer;
12+
}
13+
14+
boolean checkAnswer(char answer) {
15+
return Character.toUpperCase(answer) == Character.toUpperCase(correctAnswer);
16+
}
17+
}
18+
19+
public class QuizApplication {
20+
public static void main(String[] args) {
21+
Scanner sc = new Scanner(System.in);
22+
int score = 0;
23+
24+
Question[] questions = {
25+
new Question("1. Which company developed Java?",
26+
new String[]{"A. Sun Microsystems", "B. Microsoft", "C. Apple", "D. IBM"}, 'A'),
27+
new Question("2. Which of the following is not a Java feature?",
28+
new String[]{"A. Object-Oriented", "B. Portable", "C. Use of pointers", "D. Secure"}, 'C'),
29+
new Question("3. Which keyword is used to inherit a class in Java?",
30+
new String[]{"A. super", "B. this", "C. extends", "D. implement"}, 'C'),
31+
new Question("4. What is the size of int in Java?",
32+
new String[]{"A. 2 bytes", "B. 4 bytes", "C. 8 bytes", "D. Depends on OS"}, 'B'),
33+
new Question("5. Which method is the entry point for a Java program?",
34+
new String[]{"A. start()", "B. main()", "C. run()", "D. execute()"}, 'B')
35+
};
36+
37+
System.out.println("===== WELCOME TO JAVA QUIZ =====");
38+
for (Question q : questions) {
39+
System.out.println("\n" + q.question);
40+
for (String opt : q.options) {
41+
System.out.println(opt);
42+
}
43+
System.out.print("Enter your answer (A/B/C/D): ");
44+
char ans = sc.next().charAt(0);
45+
46+
if (q.checkAnswer(ans)) {
47+
System.out.println("✅ Correct!");
48+
score++;
49+
} else {
50+
System.out.println("❌ Wrong! Correct Answer: " + q.correctAnswer);
51+
}
52+
}
53+
54+
System.out.println("\n===== QUIZ FINISHED =====");
55+
System.out.println("Your Score: " + score + "/" + questions.length);
56+
sc.close();
57+
}
58+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
**Contributor**
2+
# Java Quiz Application
3+
4+
A simple **Java console-based quiz application** that allows users to answer multiple-choice questions and calculates their score at the end. The program demonstrates **object-oriented programming**, **arrays**, and **user input handling** in Java.
5+
6+
---
7+
8+
### Project Overview
9+
10+
This application presents a set of multiple-choice questions (MCQs) to the user.
11+
- Users select an answer by entering the corresponding option (A/B/C/D).
12+
- The program checks the answer and provides instant feedback (Correct/Wrong).
13+
- At the end, it displays the total score.
14+
15+
The application uses a `Question` class to store the question text, options, and the correct answer.
16+
17+
---
18+
19+
### Features
20+
21+
- Multiple-choice questions with 4 options each
22+
- Instant feedback after each answer
23+
- Total score calculation at the end
24+
- Case-insensitive answer checking
25+
- Easy to add or modify questions
26+
27+
---
28+
29+
### Working Principle
30+
31+
1. Create an array of `Question` objects, each containing the question, options, and correct answer.
32+
2. Loop through each question:
33+
- Display the question and its options.
34+
- Accept the user's answer from the console.
35+
- Check if the answer is correct using the `checkAnswer` method.
36+
- Display feedback (Correct/Wrong).
37+
3. After all questions, display the total score.
38+
39+
---
40+

0 commit comments

Comments
 (0)