Advertisement
SenpaiZero

Untitled

Apr 4th, 2024
548
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.73 KB | None | 0 0
  1. package asd;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class LabExer5B {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.  
  9.         // Initiating 10 questions in array
  10.         String[] questions = {
  11.             "Question 1: What does HTML stand for?\nA. Hyper Text Markup Language\nB. High Tech Markup Language\nC. Hyperlinks and Text Markup Language",
  12.             "Question 2: Which programming language is commonly used for developing mobile applications?\nA. Java\nB. C++\nC. Swift",
  13.             "Question 3: What does CSS stand for?\nA. Cascading Style Sheets\nB. Computer Style Sheets\nC. Creative Style Sheets",
  14.             "Question 4: Which data structure uses LIFO (Last In, First Out) principle?\nA. Queue\nB. Stack\nC. Linked List",
  15.             "Question 5: Which of the following is not a programming paradigm?\nA. Object-Oriented Programming\nB. Functional Programming\nC. Scripting Programming",
  16.             "Question 6: What does IDE stand for?\nA. Integrated Development Environment\nB. Interactive Design Environment\nC. Intelligent Development Environment",
  17.             "Question 7: Which of the following is not a primitive data type in Java?\nA. String\nB. int\nC. boolean",
  18.             "Question 8: What is the purpose of the keyword \"break\" in programming?\nA. To exit a loop or switch statement\nB. To pause the program execution temporarily\nC. To skip to the next iteration of a loop",
  19.             "Question 9: Which of the following sorting algorithms has the worst time complexity?\nA. Merge Sort\nB. Quick Sort\nC. Bubble Sort",
  20.             "Question 10: What is the main advantage of using version control systems like Git?\nA. Automatic bug fixing\nB. Collaboration with multiple developers\nC. Improved program performance",
  21.         };
  22.  
  23.         // Initiating the correct answers on all 10 questions
  24.         String[] answers = {"A", "C", "A", "B", "C", "A", "A", "A", "C", "B"};
  25.  
  26.         int score = 0;
  27.  
  28.         // Loop all questions
  29.         for (int i = 0; i < questions.length; i++) {
  30.             // Printing the questions
  31.             System.out.println(questions[i]);
  32.             System.out.print("Your answer: ");
  33.             String userInput = scanner.nextLine().trim().toUpperCase(); // Taking user on each questions
  34.  
  35.             try {
  36.                 if (userInput.isEmpty()) {
  37.                     // Throw error when the answer is empty or whitespace
  38.                     throw new IllegalArgumentException("Blank answer. Please try again.");
  39.                 }
  40.                 if (userInput.matches("[A-Za-z]") && !userInput.matches("[A-C]")) {
  41.                     // Throw error when the answer is invalid letter
  42.                     throw new IllegalArgumentException("Invalid letter. Please enter A, B, or C.");
  43.                 }
  44.                 if (!userInput.matches("[A-C]")) {
  45.                     // Throw error when the answer is not an alphabet
  46.                     throw new IllegalArgumentException("Invalid input. Please enter A, B, or C.");
  47.                 }
  48.                 // Check if the answer is correct
  49.                 if (userInput.equals(answers[i])) {
  50.                     System.out.println("Correct answer.");
  51.                     score++;
  52.                 } else {
  53.                     System.out.println("Incorrect answer.");
  54.                 }
  55.             } catch (IllegalArgumentException e) {
  56.                 // Printing the error message
  57.                 System.out.println("\n"+e.getMessage());
  58.                 System.out.println("You can answer again.");
  59.                 i--; // Decrease I to repeat the question
  60.             }
  61.            
  62.             System.out.println();
  63.         }
  64.  
  65.         System.out.println("Your score: " + score + "/" + questions.length);
  66.     }
  67. }
  68.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement