Share on Google+Share on Google+

Ask Questions with Options

In this Java Tutorial section, we are going to ask five questions one after the other with four options to test the user.

Ask Questions with Options using Java

In this section, we are going to ask five questions one after the other with four options to test the user. For this, we have stored 5 questions, answers with four options into a list. The list then recall all the questions along with their options. The user will have to select one of the options and write it in the space provided. To display the marks out of 10, a counter have been created. If the user's answer matches with the correct answers, counter value will increment by 2 as each question is of 2 marks.

Here is the code:

import java.util.*;

class Test {
	String question;
	String ans;
	String op1;
	String op2;
	String op3;
	String op4;

	Test(String question, String ans, String op1, String op2, 
	String op3,String op4) {
		this.question = question;
		this.ans = ans;
		this.op1 = op1;
		this.op2 = op2;
		this.op3 = op3;
		this.op4 = op4;

	public String getQuestion() {
		return question;

	public String getAns() {
		return ans;

	public String getOp1() {
		return op1;

	public String getOp2() {
		return op2;

	public String getOp3() {
		return op3;

	public String getOp4() {
		return op4;

public class AskQuestions {
	public static void main(String[] args) {
		Scanner input = new Scanner(;
		int count = 0;
		int i = 0;
		ArrayList list = new ArrayList();
		list.add(new Test("Where is Taj Mahal", "Agra",  
		"Mumbai", "Delhi","Mathura", "Agra"));
		list.add(new Test("Where is Red Fort", "Delhi", 
		"Mumbai", "Delhi","Amritsar", "Agra"));
		list.add(new Test("Where is Golden Temple", 
		"Amritsar", "Mumbai","Delhi", "Amritsar", "Agra"));
		list.add(new Test("Where is Eiffel Tower", "Paris", 
		"New York","Sydney", "Washington", "Paris"));
		list.add(new Test("Where is Statue of Liberty", "New 
		York", "New York","Sydney", "Washington", "Paris"));
		for (Test s : list) {
		      i = i + 1;
		      System.out.println("Question" + i + "  " + 
		      System.out.println("Option 1   " + s.getOp1());
		      System.out.println("Option 2   " + s.getOp2());
		      System.out.println("Option 3   " + s.getOp3());
		      System.out.println("Option 4   " + s.getOp4());
		      System.out.print("Your Answer: ");
		      String answer = input.nextLine();
                        if (answer.equals(s.getAns())) {
				count = count + 2;
		      System.out.println("Correct Answer:"+s.getAns());
		System.out.println("Your result is: " + count + "/10");


Posted on: January 15, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.


Advertisement null