Home

Multiple-Choice Question: Single Answer

 

Introduction

A multiple choice program is an application that displays a question and a few possible answers in a continuous manner. This application is also called a multiple-choice question or MCQ. After reading the question, the user consults the available answers and selects the one or those that constitute the right answer. After answering a question, the user is provided with a means of moving to the next question. In most cases, the user can also move to the previous question. In some applications, the user can stop the test or application anytime by closing it or by using a control.

When the application starts, you can display the first question to the user. In some cases, before displaying the questions to the user, you can provide a way to specify the desired number of questions.

Practical Learning Practical Learning: Creating a Multiple-Choice Test 

  1. Start Microsoft Visual C# and create a Windows Application named DrivingTest1
  2. Design the form as follows:
     
    Control Text Name Additional Properties
    Label Number of Questions:    
    NumericUpDown   updTotalQuestions Maximum: 20
    Value: 5
    Button Start btnStart  
    Button Close btnClose  
  3. Save all

Single-Answer Questions

The most basic MCQ application displays a question and two to four possible answers for a single choice. The top section of a window displays a question. Under the question, two to four radio buttons display the possible answers. The user can then click one of them. After making a choice, the user can click a button to advance to the next question.

 

Practical Learning Practical Learning: Creating the Questions 

  1. To add a new form, on the main menu, click Project -> Add Windows Form...
  2. In the Add New Item dialog box, set the Name QuestionAnswer
  3. Click Add
  4. Design the form as follows:
     
    Control Caption Name Additional Properties
    Label Question lblQuestion  
    TextBox   txtQuestion Multiline: True
    ScrollBars: Vertical
    Group Box Answer    
    Radio Button   rdoAnswer1 AutoSize: False
    CheckAlign: TopLeft
    TextAlign: TopLeft
    Radio Button   rdoAnswer2 AutoSize: False
    CheckAlign: TopLeft
    TextAlign: TopLeft
    Radio Button   rdoAnswer3 AutoSize: False
    CheckAlign: TopLeft
    TextAlign: TopLeft
    Radio Button   rdoAnswer4 AutoSize: False
    CheckAlign: TopLeft
    TextAlign: TopLeft
    Button Quit btnQuit  
    Button Check Answer btnCheckAnswer  
    Form     ShowInTaskbar: false
  5. Save All
  6. On the form, double-click the Quit button
  7. Make the following changes:
     
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    
    namespace DrivingTest1a
    {
        public partial class QuestionAnswer : Form
        {
            public int TotalNumberOfQuestions;
    
            // This variable will hold a list of randoom non-repeating numbers
            ArrayList lstNumbers;
            // This will represent the index of the question that is being asked
            int CurrentQuestion;
            // These will keep a count of the questions already asked
            int QuestionCounter;
            int CurrentNumberOfQuestions;
            // This will be used to counte the number of correct answers
            double Sum;
            const int MaximumNumberOfQuestions = 20;
            // This variable allows monitoring if a 0 had been
            // added to the list of numbers;
            bool Found0; 
    
            public QuestionAnswer()
            {
                InitializeComponent();
            }
    
            // This method takes a constant integer that represents
            // the index of a question in the list of questions
            // This method then displays the corresponding question
            void PresentQuestion(int qstNumber)
            {
                rdoAnswer1.Checked = false;
                rdoAnswer2.Checked = false;
                rdoAnswer3.Checked = false;
                rdoAnswer4.Checked = false;
    
                switch (qstNumber)
                {
                    case 1:
                        txtQuestion.Text = "If your car and a car coming from your " +
                                           "right reach an intersection at the same " +
                                           "time, who has the right of way?";
    
                        rdoAnswer1.Text = "A. Your car";
                        rdoAnswer2.Text = "B. The other car";
                        rdoAnswer3.Text = "C. You both wait for the first car that makes a move";
                        rdoAnswer4.Text = "D. Neither, as both cars must come to a stop";
                        break;
    
                    case 2:
                        txtQuestion.Text = "How many feet before you make a turn should " +
                                           "you signal that you are going to turn?";
    
                        rdoAnswer1.Text = "A. 50 feet";
                        rdoAnswer2.Text = "B. 100 feet";
                        rdoAnswer3.Text = "C. 150 feet";
                        rdoAnswer4.Text = "D. While turning";
                        
                        break;
    
                    case 3:
                        txtQuestion.Text = "You are driving in an alley at fifteen (15) " +
                                           "miles per hour. You are";
    
                        rdoAnswer1.Text = "A. Breaking the speed limit for alleys";
                        rdoAnswer2.Text = "B. Driving too slow";
                        rdoAnswer3.Text = "C. Obeying the law";
                        rdoAnswer4.Text = "D. Breaking the law as you should never drive " +
                                          "in an alley";
                        break;
    
                    case 4:
                        txtQuestion.Text = "A car driving toward you " +
                                           "at night and it has its blinding " +
                                           "high beam lights on. The driver of this car is";
    
                        rdoAnswer1.Text = "A. Guilty only of bad manners because the " +
                                          "high beams blind other drivers";
                        rdoAnswer2.Text = "B. A safe driver because the high beams " +
                                          "light up the road more brightly than " +
                                          "the lower beams";
                        rdoAnswer3.Text = "C. Not obeying the law because the low beams " +
                                          "must be used at all times";
                        rdoAnswer4.Text = "D. not doing anything wrong (it doesn't matter";
                        break;
    
                    case 5:
                        txtQuestion.Text = "You are driving up to an intersection where " +
                                           "there is no signal light or policeman. A man " +
                                           "is crossing in the cross walk in front of " +
                                           "your car. You should:";
    
                        rdoAnswer1.Text = "A. Continue into the intersection because you " +
                                          "have the right-of-way";
                        rdoAnswer2.Text = "B. Slow down and be careful";
                        rdoAnswer3.Text = "C. Stop and give him the right-of way";
                        rdoAnswer4.Text = "D. Not care";
                        break;
    
                    case 6:
                        txtQuestion.Text = "You wish to make a \"U\" turn at an intersection " +
                                           "controlled by a traffic light. You should: ";
    
                        rdoAnswer1.Text = "A. Drive to another intersection that has no traffic " +
                                          "light or sign saying NO \"U\" Turn";
                        rdoAnswer2.Text = "B. Wait until the light turns green before making " +
                                          "the \"U\" turn";
                        rdoAnswer3.Text = "C. Make the \"U\" turn if there is a policeman " +
                                          "at the intersection";
                        rdoAnswer4.Text = "D. Wait for the red light before making the \"U\" " +
                                          "turn to make sure that the opposing traffic is safe";
                        break;
    
                    case 7:
                        txtQuestion.Text = "You are driving past a school building or its " +
                                           "grounds when the children are going to or " +
                                           "leaving school. The speed limit is:";
    
                        rdoAnswer1.Text = "A. Seven (7) miles per hour";
                        rdoAnswer2.Text = "B. Fifteen (15) miles per hour";
                        rdoAnswer3.Text = "C. Ten (10) miles per hour";
                        rdoAnswer4.Text = "D. Twenty (20) miles per hour";
                        break;
    
                    case 8:
                        txtQuestion.Text = "If you are driving out of an alley " +
                                           "or driveway, you must";
    
                        rdoAnswer1.Text = "A. Stop before reaching the sidewalk";
                        rdoAnswer2.Text = "B. Stop, if possible to do so safely";
                        rdoAnswer3.Text = "C. Stop only if there is heavy traffic";
                        rdoAnswer4.Text = "D. Wait for a policeman of respectable " +
                                          "pedestrian to guide you when it is safer";
                        break;
    
                    case 9:
                        txtQuestion.Text = "You are coming to an intersection " +
                                           "where there is a flashing yellow traffic " +
                                           "light. You should";    
    
                        rdoAnswer1.Text = "A. Slow down and proceed with caution";
                        rdoAnswer2.Text = "B. Stop, if possible to do so safely";
                        rdoAnswer3.Text = "C. Continue at the same speed";
                        rdoAnswer4.Text = "D. Wait for a poileman to direct the traffic";
                        break;
    
                    case 10:
                        txtQuestion.Text = "You are driving on a two-lane street. " +
                                           "The car ahead of you is moving very slowly " +
                                           "and the road ahead is clear for passing. " +
                                           "You should:";
    
                        rdoAnswer1.Text = "A. Pass on the left hand side";
                        rdoAnswer2.Text = "B. Pass on either side";
                        rdoAnswer3.Text = "C. Pass on the right side";
                        rdoAnswer4.Text = "D. Make a gesture to signal the other car " +
                                          "to move to the other lane, so you can " +
                                          "drive straight";
                        break;
    
                    case 11:
                        txtQuestion.Text = "What is the legal rate of speed " +
                                           "unless there are signs " +
                                           "that give a different speed limit?";
    
                        rdoAnswer1.Text = "A. 25 miles per hour";
                        rdoAnswer2.Text = "B. 30 miles per hour";
                        rdoAnswer3.Text = "C. 35 miles per hour";
                        rdoAnswer4.Text = "D. 45 miles per hour";
                        break;
    
                    case 12:
                        txtQuestion.Text = "You are driving a car which is involved in " +
                                           "an accident. Two people are slightly hurt " +
                                           "but don't need to go to the hospital. " +
                                           "You should:";
    
                        rdoAnswer1.Text = "A. Report to the nearest police pricinct";
                        rdoAnswer2.Text = "B. Report to the Department of Motor Vehicles";
                        rdoAnswer3.Text = "C. Report to the police and the Department of " +
                                          "Motor Vehicles";
                        rdoAnswer4.Text = "D. Wait for a policeman";
                        break;
    
                    case 13:
                        txtQuestion.Text = "You're driving up to an intersection where " +
                                           "the traffic light is red. A policeman " +
                                           "motions you to go through. You should";
    
                        rdoAnswer1.Text = "A. Wait for the light to turn green and " +
                                          "then go ahead";
                        rdoAnswer2.Text = "B. Call the policeman's attention to " +
                                          "the red light";
                        rdoAnswer3.Text = "C. Obey the policeman's signal";
                        rdoAnswer4.Text = "D. Wait for another car to move to make " +
                                          "sure the road is safe";
                        break;
    
                    case 14:
                        txtQuestion.Text = "You are pulling into the street from a " +
                                           "parallel parking space. Before doing " +
                                           "so, you should:";
    
                        rdoAnswer1.Text = "A. Blow your horn and pull from the curb slowly";
                        rdoAnswer2.Text = "B. Signal other traffic and then pull into the street";
                        rdoAnswer3.Text = "C. Proceed with caution when there is no traffic " +
                                          "near enough to cause an accident";
                        rdoAnswer4.Text = "D. Wait for a policeman or a respectable citizen " +
                                          "to signal that the road is safer";
                        break;
    
                    case 15:
                        txtQuestion.Text = "You have changed your address. You should notify " +
                                           "the Department of Motor Vehicles, either by " +
                                           "mail or in person, within a period of:";
    
                        rdoAnswer1.Text = "A. Fifteen (15) days";
                        rdoAnswer2.Text = "B. Five (5) days";
                        rdoAnswer3.Text = "C. Thirty (30) days";
                        rdoAnswer4.Text = "D. Ten (10) days";
                        break;
    
                    case 16:
                        txtQuestion.Text = "You are driving on a street and another car is " +
                                           "entering the street from a driveway. Who has " +
                                           "the right-of-way?";
    
                        rdoAnswer1.Text = "A. You";
                        rdoAnswer2.Text = "B. The other driver";
                        rdoAnswer3.Text = "C. Neither you nor the other driver";
                        rdoAnswer4.Text = "D. The next car";
                        break;
    
                    case 17:
                        txtQuestion.Text = "Up to what age are children required to " +
                                           "be restrained in a child restraint seat?";
    
                        rdoAnswer1.Text = "A. 6 months old";
                        rdoAnswer2.Text = "B. 3 years";
                        rdoAnswer3.Text = "C. 6 years";
                        rdoAnswer4.Text = "D. 8 years";
                        break;
    
                    case 18:
                        txtQuestion.Text = "How close to a fire hydrant (fire plug) " +
                                           "may you park a motor vehicle?";
    
                        rdoAnswer1.Text = "A. Five (5) feet";
                        rdoAnswer2.Text = "B. Six (6) feet";
                        rdoAnswer3.Text = "C. Ten (10) feet";
                        rdoAnswer4.Text = "D. It doesn't matter because you will " +
                                          "move your car if the fire fighters request it";
                        break;
    
                    case 19:
                        txtQuestion.Text = "When the light is green and the yellow " +
                                           "light comes on as you approach an " +
                                           "intersection, you should";
    
                        rdoAnswer1.Text = "A. Hurry to cross";
                        rdoAnswer2.Text = "B. Stop the crosswalk";
                        rdoAnswer3.Text = "C. Proceed accross the intersection with caution";
                        rdoAnswer4.Text = "D. Before deciding about what to do, check if the " +
                                          "intersection is equipped with cameras";
                        break;
    
                    case 20:
                        txtQuestion.Text = "A flashing red traffic light signals means the same as:";
    
                        rdoAnswer1.Text = "A. Stop sign";
                        rdoAnswer2.Text = "B. Yield right-of-way sign";
                        rdoAnswer3.Text = "C. Slow sign";
                        rdoAnswer4.Text = "D. An ambulance is approaching";
                        break;
    
                    case 21:
                        txtQuestion.Text = "How close to the intersection are you allowed " +
                                           "to park on a two-way street?";
    
                        rdoAnswer1.Text = "A. Twenty (20) feet";
                        rdoAnswer2.Text = "B. Thirt (30) feet";
                        rdoAnswer3.Text = "C. Fourty (40) feet";
                        rdoAnswer4.Text = "D. Fifty (50) feet";
                        break;
                }
            }
    
            private void btnQuit_Click(object sender, EventArgs e)
            {
                double average = 1;
    
                if (QuestionCounter != 0)
                    average = Sum / QuestionCounter;
                string strCongratulations = "";
    
                if (average >= 0.5)
                    strCongratulations = "\nCongratulations!";
    
                if (Found0 == true )
                    QuestionCounter--;
    
                MessageBox.Show("Total Number of Questions: " +
                                QuestionCounter.ToString() +
                                "\nNumber of Correct Answers: " +
                                Sum.ToString() +
                                "\nSuccess Rate:                   " +
                                (average * 100).ToString("F") +
                                " %\n" + strCongratulations);
                lstNumbers.Remove(MaximumNumberOfQuestions);
                Close();
            }
        }
    }
  8. Under the Click event of the Quit button, implement the following method:
     
    // This method takes as argument a constant integer
    // The argument represents a question that was 
    // previously asked. The method checks the indexed 
    // question against its valid answer. If the answer 
    // to the question is valid, a total value is increased
    void CheckAnswer(int qstNumber)
    {
                switch (qstNumber)
                {
                    case 1:
                        if (rdoAnswer2.Checked == true)
                        {
                            MessageBox.Show("Correct");
                            Sum++;
                        }
                        else
                            MessageBox.Show("Wrong Answer");
                        break;
    
                    case 2:
                        if (rdoAnswer2.Checked == true)
                        {
                            MessageBox.Show("Correct");
                            Sum++;
                        }
                        else
                            MessageBox.Show("Wrong Answer");
                        break;
    
                    case 3:
                        if (rdoAnswer3.Checked == true)
                        {
                            MessageBox.Show("Correct");
                            Sum++;
                        }
                        else
                            MessageBox.Show("Wrong Answer");
                        break;
    
                    case 4:
                        if (rdoAnswer3.Checked == true)
                        {
                            MessageBox.Show("Correct");
                            Sum++;
                        }
                        else
                            MessageBox.Show("Wrong Answer");
                        break;
    
                    case 5:
                        if (rdoAnswer3.Checked == true)
                        {
                            MessageBox.Show("Correct");
                            Sum++;
                        }
                        else
                            MessageBox.Show("Wrong Answer");
                        break;
    
                    case 6:
                        if (rdoAnswer1.Checked == true)
                        {
                            MessageBox.Show("Correct");
                            Sum++;
                        }
                        else
                            MessageBox.Show("Wrong Answer");
                        break;
    
                    case 7:
                        if (rdoAnswer2.Checked == true)
                        {
                            MessageBox.Show("Correct");
                            Sum++;
                        }
                        else
                            MessageBox.Show("Wrong Answer");
                        break;
    
                    case 8:
                        if (rdoAnswer1.Checked == true)
                        {
                            MessageBox.Show("Correct");
                            Sum++;
                        }
                        else
                            MessageBox.Show("Wrong Answer");
                        break;
    
                    case 9:
                        if (rdoAnswer1.Checked == true)
                        {
                            MessageBox.Show("Correct");
                            Sum++;
                        }
                        else
                            MessageBox.Show("Wrong Answer");
                        break;
    
                    case 10:
                        if (rdoAnswer1.Checked == true)
                        {
                            MessageBox.Show("Correct");
                            Sum++;
                        }
                        else
                            MessageBox.Show("Wrong Answer");
                        break;
    
                    case 11:
                        if (rdoAnswer1.Checked == true)
                        {
                            MessageBox.Show("Correct");
                            Sum++;
                        }
                        else
                            MessageBox.Show("Wrong Answer");
                        break;
    
                    case 12:
                        if (rdoAnswer3.Checked == true)
                        {
                            MessageBox.Show("Correct");
                            Sum++;
                        }
                        else
                            MessageBox.Show("Wrong Answer");
                        break;
    
                    case 13:
                        if (rdoAnswer3.Checked == true)
                        {
                            MessageBox.Show("Correct");
                            Sum++;
                        }
                        else
                            MessageBox.Show("Wrong Answer");
                        break;
    
                    case 14:
                        if (rdoAnswer3.Checked == true)
                        {
                            MessageBox.Show("Correct");
                            Sum++;
                        }
                        else
                            MessageBox.Show("Wrong Answer");
                        break;
    
                    case 15:
                        if (rdoAnswer2.Checked == true)
                        {
                            MessageBox.Show("Correct");
                            Sum++;
                        }
                        else
                            MessageBox.Show("Wrong Answer");
                        break;
    
                    case 16:
                        if (rdoAnswer1.Checked == true)
                        {
                            MessageBox.Show("Correct");
                            Sum++;
                        }
                        else
                            MessageBox.Show("Wrong Answer");
                        break;
    
                    case 17:
                        if (rdoAnswer2.Checked == true)
                        {
                            MessageBox.Show("Correct");
                            Sum++;
                        }
                        else
                            MessageBox.Show("Wrong Answer");
                        break;
    
                    case 18:
                        if (rdoAnswer3.Checked == true)
                        {
                            MessageBox.Show("Correct");
                            Sum++;
                        }
                        else
                            MessageBox.Show("Wrong Answer");
                        break;
    
                    case 19:
                        if (rdoAnswer2.Checked == true)
                        {
                            MessageBox.Show("Correct");
                            Sum++;
                        }
                        else
                            MessageBox.Show("Wrong Answer");
                        break;
    
                    case 20:
                        if (rdoAnswer1.Checked == true)
                        {
                            MessageBox.Show("Correct");
                            Sum++;
                        }
                        else
                            MessageBox.Show("Wrong Answer");
                        break;
    
                    case 21:
                        if (rdoAnswer3.Checked == true)
                        {
                            MessageBox.Show("Correct");
                            Sum++;
                        }
                        else
                            MessageBox.Show("Wrong Answer");
                        break;
                }
    
                if (CurrentNumberOfQuestions == (TotalNumberOfQuestions + 1))
                {
                    btnQuit_Click(null, null);
                }
            }
  9. Return to the form
  10. Double-click the Check Answer button and implement its event as follows:
     
    private void btnCheckAnswer_Click(object sender, EventArgs e)
    {
                CurrentNumberOfQuestions++;
                CheckAnswer(CurrentQuestion);
                
                if (QuestionCounter >= lstNumbers.Count)
                    return;
                CurrentQuestion = (int)(lstNumbers[QuestionCounter++]);
                
                if (CurrentQuestion == 0)
                {
                    Found0 = true;
                    CurrentQuestion = (int)(lstNumbers[QuestionCounter++]);
                }
                
                PresentQuestion(CurrentQuestion);
    }
  11. Return to the form and double-click an unoccupied area of its body
  12. Implement the event as follows:
     
    private void QuestionAnswer_Load(object sender, EventArgs e)
    {
        Found0 = false;
        QuestionCounter = 0;
    
        CurrentNumberOfQuestions = 0;
    
        lstNumbers = new ArrayList();
        Random rndNumber = new Random();
    
        int number = rndNumber.Next(1, MaximumNumberOfQuestions + 1);
        lstNumbers.Add(number);
        int count = 0;
    
        // Create a list of 20 non-repeating integers randomly chosen
        do
        {
            // Randomly choose between 1 and the MaximumNumberOfQuestions
            number = rndNumber.Next(0, MaximumNumberOfQuestions + 1);
    
            // If the chosen number is not yet in the list,
            // then add it
            if (!lstNumbers.Contains(number))
            {
                lstNumbers.Add(number);
            }// If the number is already in the list, ignore it
    
            count++;
        } while (count <= 100);
    
        btnCheckAnswer_Click(sender, e);
    }
  13. Display the first form
  14. Double-click the Start button and implement its event as follows:
     
    private void btnStart_Click(object sender, EventArgs e)
    {
        QuestionAnswer frmAnswer = new QuestionAnswer();
        int numberOfQuestions = int.Parse(updTotalQuestions.Value.ToString());
    
        frmAnswer.TotalNumberOfQuestions = numberOfQuestions;
        frmAnswer.ShowDialog();
    }
  15. Display the first form again
  16. Double-click the Close button and implement its event as follows:
     
    private void btnClose_Click(object sender, EventArgs e)
    {
        Close();
    }
  17. Execute the application to see the result
 

Home Copyright © 2006-2016, FunctionX, Inc. Next