Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8377869
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T15:47:01+00:00 2026-06-09T15:47:01+00:00

I’m trying to create a random multiple choice quiz for android. I want to

  • 0

I’m trying to create a random multiple choice quiz for android. I want to display a random question from a stringarray, with the corresponding answer from another string array showing up in one of the four options. The other three options will come from another string array, which will be used provide the “wrong” answers for all the questions, randomly.

Two questions:
Is there a better way to make a multiple choice quiz like this?
-and-
When the player selects an answer, how do I identify which array the answer came from?

This is the code I’m using to randomize:

String[] question = { //questions here// };  
ArrayList<String> questionList = new ArrayList(Arrays.asList(question));  

String[] answer = { //answers here// };  
ArrayList<String> answerList = new ArrayList(Arrays.asList(answer));

String[] distractor = { //distractors here// };  
ArrayList<String> distractorList = new ArrayList(Arrays.asList(distractor));  

int i = 0;  
Random r = new Random();  
public void randomize() {

        TextView word = (TextView) findViewById(R.id.textView1);
        TextView choice1 = (TextView) findViewById(R.id.textView2);
        TextView choice2 = (TextView) findViewById(R.id.textView3);
        TextView choice3 = (TextView) findViewById(R.id.textView4);
        TextView choice4 = (TextView) findViewById(R.id.textView5);
        if (i < question.length) {
            int remaining = r.nextInt(questionList.size());
            String q = questionList.get(remaining);
            word.setText(q);
            questionList.remove(remaining);
            String a = answerList.get(remaining);
            int slot = r.nextInt(4);
            TextView[] tvArray = { choice1, choice2, choice3, choice4 };
            tvArray[slot].setText(a);
            answerList.remove(remaining);
          //an if/else statement here to fill the remaining slots with distractors
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T15:47:02+00:00Added an answer on June 9, 2026 at 3:47 pm

    I suggest creating a new class called QuestionAndAnswer. The class should hold the question and the correct answer, it could also hold any customized wrong answers and the user’s choice. The exact implementation is entirely up to you.

    In your Activity have an Array of this QuestionAndAnswer class to cycle through the list asking the questions and tally up the points when done.

    (I could be more specific if you include the relevant code of what you have tried.)


    Addition

    This is what I would start with:
    (From your code I’m guessing the distractorList contains the false answers that you want to display.)

    public class QuestionAndAnswer {
        public List<String> allAnswers; // distractors plus real answer
        public String answer;
        public String question;
        public String selectedAnswer;
        public int selectedId = -1;
    
        public QuestionAndAnswer(String question, String answer, List<String> distractors) {
            this.question = question;
            this.answer = answer;
            allAnswers = new ArrayList<String> (distractors);
    
            // Add real answer to false answers and shuffle them around 
            allAnswers.add(answer);
            Collections.shuffle(allAnswers);
        }
    
        public boolean isCorrect() {
            return answer.equals(selectedAnswer);
        }
    }
    

    For the Activity I changed your four answer TextViews into a RadioGroup, this way the user can intuitively select an answer. I also assume that there will be prev and next buttons, they will adjust int currentQuestion and call fillInQuestion().

    public class Example extends Activity {
        RadioGroup answerRadioGroup;
        int currentQuestion = 0;
        TextView questionTextView;
        List<QuestionAndAnswer> quiz = new ArrayList<QuestionAndAnswer>();
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            questionTextView = (TextView) findViewById(R.id.question);
            answerRadioGroup = (RadioGroup) findViewById(R.id.answers);
    
            // Setup a listener to save chosen answer
            answerRadioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(RadioGroup group, int checkedId) {
                    if(checkedId > -1) {
                        QuestionAndAnswer qna = quiz.get(currentQuestion);
                        qna.selectedAnswer = ((RadioButton) group.findViewById(checkedId)).getText().toString();
                        qna.selectedId = checkedId;
                    }
                }
            });
    
            String[] question = { //questions here// };  
            String[] answer = { //answers here// };  
            String[] distractor = { //distractors here// };  
            ArrayList<String> distractorList = Arrays.asList(distractor);  
    
            /* I assumed that there are 3 distractors per question and that they are organized in distractorList like so:
             *   "q1 distractor 1", "q1 distractor 2", "q1 distractor 3", 
             *   "q2 distractor 1", "q2 distractor 2", "q2 distractor 3",
             *   etc
             *   
             * If the question is: "The color of the sky", you'd see distractors:
             *   "red", "green", "violet"
             */   
            int length = question.length;
            for(int i = 0; i < length; i++)
                quiz.add(new QuestionAndAnswer(question[i], answer[i], distractorList.subList(i * 3, (i + 1) * 3)));
            Collections.shuffle(quiz);
    
            fillInQuestion();
        }
    
        public void fillInQuestion() {
            QuestionAndAnswer qna = quiz.get(currentQuestion);
            questionTextView.setText(qna.question);
    
            // Set all of the answers in the RadioButtons 
            int count = answerRadioGroup.getChildCount();
            for(int i = 0; i < count; i++)
                ((RadioButton) answerRadioGroup.getChildAt(i)).setText(qna.allAnswers.get(i));
    
            // Restore selected answer if exists otherwise clear previous question's choice
            if(qna.selectedId > -1)
                answerRadioGroup.check(qna.selectedId);
            else 
                answerRadioGroup.clearCheck();
        }
    }
    

    You may have noticed that QuestionAndAnswer has an isCorrect() method, when it is time to grade the quiz you can count the correct answers like this:

    int correct = 0;
    for(QuestionAndAnswer question : quiz)
        if(question.isCorrect())
            correct++;
    

    This is my general idea. The code is a complete thought, so it will compile. Of course, you’ll want to add a “next” Button to see the different questions. But this is enough for you to see one way to randomize your questions and answers while keeping them organized.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I am trying to understand how to use SyndicationItem to display feed which is
i want to parse a xhtml file and display in UITableView. what is the
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to create an if statement in PHP that prevents a single post
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.