0% found this document useful (0 votes)
2 views2 pages

P4

This document contains a Java program for a simple quiz system that asks users five questions about Java programming. Users input their answers, and the program checks if they are correct, keeping track of the score. At the end of the quiz, it displays the user's score and a performance message based on their percentage score.

Uploaded by

aryanrajpro949
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views2 pages

P4

This document contains a Java program for a simple quiz system that asks users five questions about Java programming. Users input their answers, and the program checks if they are correct, keeping track of the score. At the end of the quiz, it displays the user's score and a performance message based on their percentage score.

Uploaded by

aryanrajpro949
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

import java.util.

Scanner;

class Question {
String questionText;
String[] options;
int correctAnswer; // index (0 to 3)

public Question(String questionText, String[] options, int correctAnswer) {


this.questionText = questionText;
this.options = options;
this.correctAnswer = correctAnswer;
}

public void display() {


System.out.println(questionText);
for (int i = 0; i < options.length; i++) {
System.out.println((i + 1) + ". " + options[i]);
}
}

public boolean isCorrect(int userAnswer) {


return userAnswer - 1 == correctAnswer;
}
}

public class QuizSystem {


static Scanner sc = new Scanner(System.in);
static Question[] questions;
static int score = 0;

public static void main(String[] args) {


initializeQuestions();

System.out.println("********** JAVA QUIZ SYSTEM **********\n");

for (int i = 0; i < questions.length; i++) {


System.out.println("Question " + (i + 1) + ":");
questions[i].display();

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


int answer = sc.nextInt();

if (questions[i].isCorrect(answer)) {
System.out.println("Correct! ?");
score++;
} else {
System.out.println("Wrong ?. Correct answer: " +
questions[i].options[questions[i].correctAnswer]);
}

System.out.println("--------------------------------------");
}

System.out.println("Quiz Completed!");
System.out.println("Your Score: " + score + " out of " + questions.length);
displayResult();
}

static void initializeQuestions() {


questions = new Question[5];

questions[0] = new Question(


"What is the size of int in Java?",
new String[] {"4 bytes", "2 bytes", "8 bytes", "Depends on OS"},
0
);

questions[1] = new Question(


"Which keyword is used to inherit a class in Java?",
new String[] {"super", "this", "extends", "inherits"},
2
);

questions[2] = new Question(


"Which of these is not a primitive data type?",
new String[] {"int", "boolean", "char", "String"},
3
);

questions[3] = new Question(


"What does JVM stand for?",
new String[] {
"Java Variable Machine",
"Java Virtual Machine",
"Just Virtual Machine",
"Java Verified Module"
},
1
);

questions[4] = new Question(


"What is the default value of a boolean variable?",
new String[] {"true", "false", "0", "null"},
1
);
}

static void displayResult() {


double percentage = ((double) score / questions.length) * 100;
System.out.println("Percentage: " + percentage + "%");

if (percentage == 100) {
System.out.println("Outstanding! ??");
} else if (percentage >= 80) {
System.out.println("Excellent Work! ??");
} else if (percentage >= 50) {
System.out.println("Good Job! ??");
} else {
System.out.println("Try again. Practice makes perfect! ??");
}
}
}

You might also like