|
| 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 | +} |
0 commit comments