We Value Your Privacy

We and our partners use technology such as cookies on our site to personalise content and ads, provide social media features, and analyse our traffic. Click below to consent to the use of this technology across the web. You can change your mind and change your consent choices at anytime by returning to this site.

Update Consent
Loading ...

How to create a quiz with random questions using Java

With the Java programming language, you can create interactive applications such as quizzes. To create a Java quiz, you need to model your quiz questions and answers, then present them to the user. Your application also needs to capture the user input and check it against the correct answers. How you choose to store the data and present it to the user will depend on the needs of your own project. However, the logic of creating a quiz with randomly selected questions remains broadly the same whatever the details of your requirements are. Creating a quiz application is an accessible task even for Java beginners.

Loading ...
  1. Create the questions and answers for your quiz. You can store your quiz data in many possible ways, such as in a database or external file. If you choose either of these options you will need to read the data into your program. For a simpler approach you can "hard-code" the question and answer data in your program using lists, as in the following example code:

  2. List questions = new LinkedList(); List answers = new LinkedList();

  3. Instantiate the question and answer variables. Add your quiz questions and answers to the lists you created, as in the following example:

  4. questions.add("What is the capital of Egypt?"); answers.add("Cairo"); questions.add("What is the capital of Spain?"); answers.add("Madrid");

  5. Continue to add to the lists until you have all of your questions and answers entered. Add the following code to create a random number generator and variable to keep track of the number of quiz questions:

  6. Random rand = new Random(); int numQu=10;

  7. Alter the number of questions if necessary.

  8. Iterate through the questions using a loop. What you do inside the loop depends on how you want to display the questions and capture user input. The following sample code demonstrates using the standard console. You need "try" and "catch" blocks in case of input/output errors:

  9. try{ BufferedReader read = new BufferedReader(new InputStreamReader(System.in)); } catch(Exception e){ System.err.println(e); e.printStackTrace(); }

  10. The "catch" block will output the details of any errors. Inside the "try" block, the code creates a Buffered Reader for user input. After that line you can loop through the questions, outputting a randomly selected one each time:

  11. while(!questions.isEmpty()){ int randQu = rand.nextInt(numQu); System.out.println(questions.remove(randQu)); }

  12. The code removes the question so that it is not asked again.

  13. Capture user input inside the loop you created to iterate through the questions. After writing the current question out to the console, read the user input as follows:

  14. String ans = read.readLine();

  15. Each time the loop iterates, the current question will be output, then whatever the user types will be read in.

  16. Check the user's answer. After the line in which you read the user input into the program, check it against the correct answer you stored in your answers list, writing out an appropriate message:

  17. if(ans.equalsIgnoreCase(answers.remove(randQu))) System.out.println("CORRECT!"); else System.out.println("INCORRECT!");

  18. Finally, decrement the number of questions each time the loop iterates:

  19. numQu--;

  20. The program will present each question, read in and check the answer then move onto the next question until it runs out of questions.

  21. Tip

    If you want your quiz program to use a GUI (Graphical User Interface), you can use Java's JFC and Swing code libraries.


    If you choose to store your quiz data externally to the application, you will need to carry out exception handling and plenty of testing.

Loading ...

About the Author

Sue Smith

Sue Smith started writing in 2000. She has produced tutorials for companies including Apex Computer Training Software and articles on computing topics for various websites. Smith has a Master of Arts in English language and literature, as well as a Master of Science in information technology, both from the University of Glasgow.

Loading ...
Loading ...