The document contains code for a Java program that implements a multiple choice quiz. It defines an array of questions and answers, displays the questions to the user one by one, tracks the score, and prints the final score.
The document contains code for a Java program that implements a multiple choice quiz. It defines an array of questions and answers, displays the questions to the user one by one, tracks the score, and prints the final score.
Scanner scanner = new Scanner(System.in); int score = 0; // Initialize score
// Array to store questions and their correct answers
String[][] questions = { {"The computer presents output on which device?", "A. Keyboard", "B. Mouse", "C. Moniter", "D. Headphones", "C"}, {"Which component of computer saves the data permenantely?", "A. RAM", "B. ROM", "C. HDD", "D. Processor", "C"}, {"What is the full form of GPU?", "A. Graphics Processing Unit", "B. Graphics Packaging Unit", "C. Game Processing Unit", "D. Graphics Packaging Ultra", "A"}, {"Which one of these is a input device", "A. Monitor", "B. Printer", "C. RAM", "D. Mouse", "D"}, {"Who painted the Mona Lisa?", "A. Leonardo da Vinci", "B. Pablo Picasso", "C. Vincent van Gogh", "D. Michelangelo", "A"} };
// Display questions, get user answers, and track score
for (int i = 0; i < questions.length; i++) { String[] question = questions[i]; System.out.println(question[0]); for (int j = 1; j < question.length - 1; j++) { System.out.println(question[j]); } System.out.print("Your answer (A, B, C, or D): "); String userAnswer = scanner.nextLine().toUpperCase(); // Convert input to uppercase for case- insensitivity
// Check user's answer against correct answer
if (userAnswer.equals(question[question.length - 1])) { score++; // Increment score if answer is correct } else { System.out.println("Incorrect. Correct answer: " + question[question.length - 1]); } System.out.println(); }