0% found this document useful (0 votes)
34 views5 pages

Java Project

The program allows users to take a Java quiz by prompting them for input, scoring their answers, and providing feedback. It uses Scanner to get user input, tracks score and feedback in variables, and prints output using System.out.println.

Uploaded by

javalpatel93
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
34 views5 pages

Java Project

The program allows users to take a Java quiz by prompting them for input, scoring their answers, and providing feedback. It uses Scanner to get user input, tracks score and feedback in variables, and prints output using System.out.println.

Uploaded by

javalpatel93
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 5

AIM : Create a quiz program that allows users to take a quiz, receive

immediate feedback, view their final score, and receive a personalized


message based on their performance.

Source code :

import java.util.Scanner;

public class Quiz {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int score = 0;
StringBuilder feedback = new StringBuilder(); // To store feedback

System.out.println("Welcome to the Java Quiz!");


System.out.print("Enter your name: ");
String name = scanner.nextLine();
System.out.print("Enter your roll number: ");
String rollNo = scanner.nextLine();

System.out.println("Which keyword is used for accessing the features of


a package?");
System.out.println("a) import");
System.out.println("b) package");
System.out.println("c) export");
System.out.print("Your answer: ");
String answer1 = scanner.nextLine();
if (answer1.equalsIgnoreCase("a")) {
score++;
feedback.append("Question 1: Correct\n");
} else {
feedback.append("Question 1: Incorrect\n");
}

System.out.println("\nQuestion 2: What is 2 + 2?");


System.out.print("Your answer: ");
int answer2 = scanner.nextInt();
if (answer2 == 4) {
score++;
feedback.append("Question 2: Correct\n");
} else {
feedback.append("Question 2: Incorrect\n");
}

scanner.nextLine();
System.out.println("\Question 3: Which is used to find and fix bugs in the Java
programs.?");
System.out.println("a) JVM");
System.out.println("b) JBD");
System.out.println("c) JDK");
System.out.print("Your answer: ");
String answer3 = scanner.nextLine();
if (answer3.equalsIgnoreCase("b")) {
score++;
feedback.append("Question 3: Correct\n");
} else {
feedback.append("Question 3: Incorrect\n");
}

System.out.println("\nQuestion 4: What is the output of the following Java code?\n");


System.out.println("public class Main {\n\tpublic static void main(String[] args) {\n\t\tint x =
5;\n\t\tSystem.out.println(x++);\n\t}\n}");
System.out.println("\na) 5");
System.out.println("b) 6");
System.out.println("c) Compilation Error");
System.out.print("Your answer: ");
String answer4 = scanner.nextLine();
if (answer4.equalsIgnoreCase("a")) { score++;
feedback.append("Question 4: Correct\n");
} else {
feedback.append("Question 4: Incorrect\n");
}

System.out.println("\nQuestion 5: What is the keyword used to indicate the beginning of a


class in Java?");
System.out.print("Your answer: ");
String answer5 = scanner.nextLine();
if (answer5.equalsIgnoreCase("class")) {
score++;
feedback.append("Question 5: Correct\n");
} else {
feedback.append("Question 5: Incorrect\n");
}

System.out.println("\nName: " + name);


System.out.println("Roll No: " + rollNo);
System.out.println("Your score is: " + score + "/5");
System.out.println("\nFeedback:");
System.out.println(feedback.toString());

if(score >= 3) {
System.out.println("Great job, " + name + "! Your score is good!");
} else {
System.out.println("Keep practicing, " + name + ". You can do better!");
}

}
}.

Output :

Welcome to the Java Quiz!

Enter your name: Prince

Enter your roll number: IU2241230392

Question 1: Which keyword is used for accessing the features of a package?

a) Import
b) Package
c) Export

Your answer: a

Question 2: What is 2 + 2?

Your answer: 4

Question 3: Which is used to find and fix bugs in the Java programs.?

a) JVM
b) JBD
c) JDK
Your answer: b

Question 4: What is the output of the following Java code?

Public class Main {


Public static void main(String[] args) {
Int x = 5;
System.out.println(x++);
}

a) 5
b) 6
c) Compilation Error

Your answer: b
Incorrect!

Question 5: What is the keyword used to indicate the beginning of a class in Java?

Your answer: b

Incorrect!

Name: Prince

Roll No: IU2241230392

Your score is: 3/5

Feedback:

Question 1: Correct
Question 2: Correct
Question 3: Correct
Question 4: Incorrect
Question 5: Incorrect

Great job, Prince! Your score is good!

Explanation :
The program uses the `Scanner` class to receive input from the user, allowing the user to input
their name, roll number, and answers to the quiz questions.

Variables: It declares variables to store the user's name (`name`), roll number (`rollNo`), quiz
score (`score`), and feedback (`feedback`). These variables are used throughout the program to
capture and manipulate data.

StringBuilder : The `feedback` variable is of type `StringBuilder`, which is used to efficiently


construct a string that stores feedback for each question. This allows appending feedback for
each question as the program progresses.

String Manipulation : The program uses various string manipulation methods, such as
`equalsIgnoreCase()`, to compare the user's answer with the correct answer case-insensitively.

Output Formatting : It uses `System.out.println()` statements to display messages to the user,


such as welcome messages, quiz questions, correct/incorrect feedback, user's score, and
personalized messages based on the score.

User Interaction : The program interacts with the user by prompting them to input their name,
roll number, and answers to quiz questions. It also provides feedback and personalized
messages based on the user's input and performances.

You might also like