Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package asd;
- import java.util.Scanner;
- public class LabExer5B {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- // Initiating 10 questions in array
- String[] questions = {
- "Question 1: What does HTML stand for?\nA. Hyper Text Markup Language\nB. High Tech Markup Language\nC. Hyperlinks and Text Markup Language",
- "Question 2: Which programming language is commonly used for developing mobile applications?\nA. Java\nB. C++\nC. Swift",
- "Question 3: What does CSS stand for?\nA. Cascading Style Sheets\nB. Computer Style Sheets\nC. Creative Style Sheets",
- "Question 4: Which data structure uses LIFO (Last In, First Out) principle?\nA. Queue\nB. Stack\nC. Linked List",
- "Question 5: Which of the following is not a programming paradigm?\nA. Object-Oriented Programming\nB. Functional Programming\nC. Scripting Programming",
- "Question 6: What does IDE stand for?\nA. Integrated Development Environment\nB. Interactive Design Environment\nC. Intelligent Development Environment",
- "Question 7: Which of the following is not a primitive data type in Java?\nA. String\nB. int\nC. boolean",
- "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",
- "Question 9: Which of the following sorting algorithms has the worst time complexity?\nA. Merge Sort\nB. Quick Sort\nC. Bubble Sort",
- "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",
- };
- // Initiating the correct answers on all 10 questions
- String[] answers = {"A", "C", "A", "B", "C", "A", "A", "A", "C", "B"};
- int score = 0;
- // Loop all questions
- for (int i = 0; i < questions.length; i++) {
- // Printing the questions
- System.out.println(questions[i]);
- System.out.print("Your answer: ");
- String userInput = scanner.nextLine().trim().toUpperCase(); // Taking user on each questions
- try {
- if (userInput.isEmpty()) {
- // Throw error when the answer is empty or whitespace
- throw new IllegalArgumentException("Blank answer. Please try again.");
- }
- if (userInput.matches("[A-Za-z]") && !userInput.matches("[A-C]")) {
- // Throw error when the answer is invalid letter
- throw new IllegalArgumentException("Invalid letter. Please enter A, B, or C.");
- }
- if (!userInput.matches("[A-C]")) {
- // Throw error when the answer is not an alphabet
- throw new IllegalArgumentException("Invalid input. Please enter A, B, or C.");
- }
- // Check if the answer is correct
- if (userInput.equals(answers[i])) {
- System.out.println("Correct answer.");
- score++;
- } else {
- System.out.println("Incorrect answer.");
- }
- } catch (IllegalArgumentException e) {
- // Printing the error message
- System.out.println("\n"+e.getMessage());
- System.out.println("You can answer again.");
- i--; // Decrease I to repeat the question
- }
- System.out.println();
- }
- System.out.println("Your score: " + score + "/" + questions.length);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement