Created
November 2, 2025 20:31
-
-
Save ondrej-kvasnovsky/263bdc67495dc877f454b23e30064991 to your computer and use it in GitHub Desktop.
QuizGameMain.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| void main() { | |
| IO.println("=== Java Knowledge Quiz ===\n"); | |
| // FIVE parallel arrays | |
| String[] questions = { | |
| "What type stores decimal numbers?", | |
| "How do you get text from the user?", | |
| "What does ++ do to a variable?", | |
| "Which loop should be used if we don't how many times it would be executed?", | |
| "What method gets string length?", | |
| "What symbol starts a single-line comment?", | |
| "How do you declare an array?", | |
| "What does a method with void return?", | |
| "What type stores whole numbers?", | |
| "What compares two int values for equality?", | |
| "How do you access array element at index 2?", | |
| "What prints text to console?", | |
| "Which loop runs exact number of times?", | |
| "What's the default value of boolean?", | |
| "What is a naming convention for classes?", | |
| "What keyword creates a new object?", | |
| "What is an object called after creation?", | |
| "How do you get array length?", | |
| "What joins two strings together?", | |
| "What naming style for variables?", | |
| "How many parameters this method has: add(int a, int b)?", | |
| "What does a return statement do?", | |
| "What is the purpose of main method?", | |
| "What is a class?", | |
| "What runs a code if a condition is false?" | |
| }; | |
| String[] options = { | |
| "A) int B) double C) String", | |
| "A) IO.readln() B) IO.println() C) IO.read()", | |
| "A) subtracts 1 B) adds 1 C) multiplies by 2", | |
| "A) for B) while C) do-while", | |
| "A) .length() B) .size() C) .count()", | |
| "A) // B) /* C) #", | |
| "A) int[] arr B) int arr[] C) array int", | |
| "A) nothing B) true C) 0", | |
| "A) double B) int C) float", | |
| "A) = B) == C) .equals()", | |
| "A) arr.2 B) arr(2) C) arr[2]", | |
| "A) System.print() B) IO.println() C) IO.write()", | |
| "A) while B) for C) if", | |
| "A) true B) false C) 0", | |
| "A) UpperCamelCase B) camelCase C) snake_case", | |
| "A) create B) new C) make", | |
| "A) object B) instance C) class", | |
| "A) arr.length B) arr.size() C) arr.length()", | |
| "A) + B) & C) .join()", | |
| "A) UpperCamelCase B) camelCase C) snake_case", | |
| "A) 0 B) 1 C) 2", | |
| "A) exits method B) loops back C) prints value", | |
| "A) nothing special B) entry point for our programs", | |
| "A) a loop B) a blueprint C) a method", | |
| "A) if B) else C) while" | |
| }; | |
| String[] answers = { | |
| "B", // double | |
| "A", // IO.readln() | |
| "B", // adds 1 | |
| "B", // while | |
| "A", // .length() | |
| "A", // // | |
| "A", // int[] arr | |
| "A", // nothing | |
| "B", // int | |
| "B", // == | |
| "C", // arr[2] | |
| "B", // IO.println() | |
| "B", // for | |
| "B", // false | |
| "A", // UpperCamelCase | |
| "B", // new | |
| "B", // instance | |
| "A", // arr.length | |
| "A", // + | |
| "B", // camelCase | |
| "C", // 2 parameters | |
| "A", // exits method | |
| "B", // entry point for our programs | |
| "B", // a blueprint | |
| "B" // else | |
| }; | |
| String[] userAnswers = new String[questions.length]; // Store what user typed | |
| boolean[] results = new boolean[questions.length]; // Track correct/wrong | |
| int score = 0; | |
| // Ask all questions (no immediate feedback) | |
| for (int i = 0; i < questions.length; i++) { | |
| IO.println("Question " + (i + 1) + " of " + questions.length); | |
| userAnswers[i] = askQuestion(questions[i], options[i]); | |
| results[i] = checkAnswer(userAnswers[i], answers[i]); | |
| if (results[i]) { | |
| score++; | |
| } | |
| } | |
| // Show final score | |
| displayResults(score, questions.length); | |
| // Review wrong answers only | |
| IO.println("\n=== Review Wrong Answers ==="); | |
| int wrongCount = 0; | |
| for (int i = 0; i < questions.length; i++) { | |
| if (!results[i]) { // If they got it wrong | |
| wrongCount++; | |
| IO.println(wrongCount + ". " + questions[i]); | |
| IO.println(" " + options[i]); | |
| IO.println(" Your answer: " + userAnswers[i]); | |
| IO.println(" Correct answer: " + answers[i]); | |
| IO.println(); | |
| } | |
| } | |
| if (wrongCount == 0) { | |
| IO.println("Perfect! You got everything right!"); | |
| } | |
| } | |
| String askQuestion(String question, String options) { | |
| IO.println(question); | |
| IO.println(options); | |
| IO.println("Your answer (A/B/C):"); | |
| return IO.readln(); | |
| } | |
| boolean checkAnswer(String userAnswer, String correctAnswer) { | |
| return userAnswer.equalsIgnoreCase(correctAnswer); | |
| } | |
| void displayResults(int score, int total) { | |
| IO.println("\n=== Quiz Complete! ==="); | |
| IO.println("Score: " + score + " / " + total); | |
| double percentage = (score * 100.0) / total; | |
| IO.println("Percentage: " + percentage + "%"); | |
| if (percentage >= 90) { | |
| IO.println("Grade: A - Excellent!"); | |
| } else if (percentage >= 80) { | |
| IO.println("Grade: B - Good job!"); | |
| } else if (percentage >= 70) { | |
| IO.println("Grade: C - Not bad"); | |
| } else if (percentage >= 60) { | |
| IO.println("Grade: D - Keep studying"); | |
| } else { | |
| IO.println("Grade: F - Review the lessons!"); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment