Home

Multiple-Choice Question

 

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++ 2005 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 New Item...
  2. In the Templates list, click Windows Form.
  3. Set the Name QuestionAnswer
  4. Click Add
  5. 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
  6. Save All
  7. On the form, double-click the Quit button
  8. Make the following changes:
     
    #pragma once
    
    using namespace System;
    using namespace System::ComponentModel;
    using namespace System::Collections;
    using namespace System::Windows::Forms;
    using namespace System::Data;
    using namespace System::Drawing;
    
    
    namespace VCDrivingTest1
    {
    	const int MaximumNumberOfQuestions = 20;
    
    	/// <summary>
    	/// Summary for QuestionAnswer
    	///
    	/// WARNING: If you change the name of this class, you will need to change the
    	///          'Resource File Name' property for the managed resource compiler tool
    	///          associated with all .resx files this class depends on.  Otherwise,
    	///          the designers will not be able to interact properly with localized
    	///          resources associated with this form.
    	/// </summary>
    	public ref class QuestionAnswer : public System::Windows::Forms::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;
            // This variable allows monitoring if a 0 had been
            // added to the list of numbers;
            bool Found0; 
    
    	QuestionAnswer(void)
    	{
    		InitializeComponent();
    		//
    		//TODO: Add the constructor code here
    		//
    	}
    		
            // 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 = L"If your car and a car coming from your "
                                            L"right reach an intersection at the same "
                                            L"time, who has the right of way?";
    
                        rdoAnswer1->Text = L"A. Your car";
    					rdoAnswer2->Text = L"B. The other car";
                        rdoAnswer3->Text = L"C. You both wait for the first car that makes a move";
                        rdoAnswer4->Text = L"D. Neither, as both cars must come to a stop";
                        break;
    
                    case 2:
                        txtQuestion->Text = L"How many feet before you make a turn should "
                                            L"you signal that you are going to turn?";
    
                        rdoAnswer1->Text = L"A. 50 feet";
                        rdoAnswer2->Text = L"B. 100 feet";
                        rdoAnswer3->Text = L"C. 150 feet";
                        rdoAnswer4->Text = L"D. While turning";
                        
                        break;
    
                    case 3:
                        txtQuestion->Text = L"You are driving in an alley at fifteen (15) "
                                            L"miles per hour. You are";
    
                        rdoAnswer1->Text = L"A. Breaking the speed limit for alleys";
                        rdoAnswer2->Text = L"B. Driving too slow";
                        rdoAnswer3->Text = L"C. Obeying the law";
                        rdoAnswer4->Text = L"D. Breaking the law as you should never drive "
                                           L"in an alley";
                        break;
    
                    case 4:
                        txtQuestion->Text = L"A car isdriving toward you "
                                            L"at night and it has its blinding "
                                            L"high beam lights on. The driver of this car is";
    
                        rdoAnswer1->Text = L"A. Guilty only of bad manners because the "
                                           L"high beams blind other drivers";
                        rdoAnswer2->Text = L"B. A safe driver because the high beams "
                                           L"light up the road more brightly than "
                                           L"the lower beams";
                        rdoAnswer3->Text = L"C. Not obeying the law because the low beams "
                                           L"must be used at all times";
                        rdoAnswer4->Text = L"D. not doing anything wrong (it doesn't matter)";
                        break;
    
                    case 5:
                        txtQuestion->Text = L"You are driving up to an intersection where "
                                            L"there is no signal light or policeman. A man "
                                            L"is crossing in the cross walk in front of "
                                            L"your car. You should:";
    
                        rdoAnswer1->Text =  L"A. Continue into the intersection because you "
                                            L"have the right-of-way";
                        rdoAnswer2->Text =  L"B. Slow down and be careful";
                        rdoAnswer3->Text =  L"C. Stop and give him the right-of way";
                        rdoAnswer4->Text =  L"D. Not care";
                        break;
    
                    case 6:
                        txtQuestion->Text = L"You wish to make a \"U\" turn at an intersection "
                                            L"controlled by a traffic light. You should: ";
    
                        rdoAnswer1->Text = L"A. Drive to another intersection that has no traffic "
                                           L"light or sign saying NO \"U\" Turn";
                        rdoAnswer2->Text = L"B. Wait until the light turns green before making "
                                           L"the \"U\" turn";
                        rdoAnswer3->Text = L"C. Make the \"U\" turn if there is a policeman "
                                           L"at the intersection";
                        rdoAnswer4->Text = L"D. Wait for the red light before making the \"U\" "
                                           L"turn to make sure that the opposing traffic is safe";
                        break;
    
                    case 7:
                        txtQuestion->Text = L"You are driving past a school building or its "
                                            L"grounds when the children are going to or "
                                            L"leaving school. The speed limit is:";
    
                        rdoAnswer1->Text = L"A. Seven (7) miles per hour";
                        rdoAnswer2->Text = L"B. Fifteen (15) miles per hour";
                        rdoAnswer3->Text = L"C. Ten (10) miles per hour";
                        rdoAnswer4->Text = L"D. Twenty (20) miles per hour";
                        break;
    
                    case 8:
                        txtQuestion->Text = L"If you are driving out of an alley "
                                            L"or driveway, you must";
    
                        rdoAnswer1->Text = L"A. Stop before reaching the sidewalk";
                        rdoAnswer2->Text = L"B. Stop, if possible to do so safely";
                        rdoAnswer3->Text = L"C. Stop only if there is heavy traffic";
                        rdoAnswer4->Text = L"D. Wait for a policeman of respectable "
                                           L"pedestrian to guide you when it is safer";
                        break;
    
                    case 9:
                        txtQuestion->Text = L"You are coming to an intersection "
                                            L"where there is a flashing yellow traffic "
                                            L"light. You should";    
    
                        rdoAnswer1->Text = L"A. Slow down and proceed with caution";
                        rdoAnswer2->Text = L"B. Stop, if possible to do so safely";
                        rdoAnswer3->Text = L"C. Continue at the same speed";
                        rdoAnswer4->Text = L"D. Wait for a poileman to direct the traffic";
                        break;
    
                    case 10:
                        txtQuestion->Text = L"You are driving on a two-lane street. "
                                            L"The car ahead of you is moving very slowly "
                                            L"and the road ahead is clear for passing. "
                                            L"You should:";
    
                        rdoAnswer1->Text = L"A. Pass on the left hand side";
                        rdoAnswer2->Text = L"B. Pass on either side";
                        rdoAnswer3->Text = L"C. Pass on the right side";
                        rdoAnswer4->Text = L"D. Make a gesture to signal the other car "
                                           L"to move to the other lane, so you can "
                                           L"drive straight";
                        break;
    
                    case 11:
                        txtQuestion->Text = L"What is the legal rate of speed "
                                            L"unless there are signs "
                                            L"that give a different speed limit?";
    
                        rdoAnswer1->Text = L"A. 25 miles per hour";
                        rdoAnswer2->Text = L"B. 30 miles per hour";
                        rdoAnswer3->Text = L"C. 35 miles per hour";
                        rdoAnswer4->Text = L"D. 45 miles per hour";
                        break;
    
                    case 12:
                        txtQuestion->Text = L"You are driving a car which is involved in "
    						                L"an accident. Two people are slightly hurt "
                                            L"but don't need to go to the hospital. "
                                            L"You should:";
    
                        rdoAnswer1->Text  = L"A. Report to the nearest police pricinct";
                        rdoAnswer2->Text = L"B. Report to the Department of Motor Vehicles";
                        rdoAnswer3->Text = L"C. Report to the police and the Department of "
                                           L"Motor Vehicles";
                        rdoAnswer4->Text = L"D. Wait for a policeman";
                        break;
    
                    case 13:
    					txtQuestion->Text = L"You're driving up to an intersection where "
                                            L"the traffic light is red. A policeman "
                                            L"motions you to go through. You should";
    
                        rdoAnswer1->Text  = L"A. Wait for the light to turn green and "
                                            L"then go ahead";
                        rdoAnswer2->Text  = L"B. Call the policeman's attention to "
                                            L"the red light";
                        rdoAnswer3->Text  = L"C. Obey the policeman's signal";
                        rdoAnswer4->Text  = L"D. Wait for another car to move to make "
                                            L"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: System::Void btnQuit_Click(System::Object^  sender, System::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(String::Concat("Total Number of Questions: ",
                                QuestionCounter.ToString(),
                                "\nNumber of Correct Answers: ",
                                Sum.ToString(),
                                "\nSuccess Rate:                   ",
                                (average * 100).ToString("F"),
                                " %\n", strCongratulations));
                lstNumbers->Remove(MaximumNumberOfQuestions);
                Close();
    	}
    }
  9. 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(nullptr, nullptr);
                }
    }
  10. Return to the form
  11. Double-click the Check Answer button and implement its event as follows:
     
    System::Void btnCheckAnswer_Click(System::Object^  sender, System::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);
    }
  12. Return to the form and double-click an unoccupied area of its body
  13. Implement the event as follows:
     
    System::Void QuestionAnswer_Load(System::Object^  sender, System::EventArgs^  e)
    {
        Found0 = false;
        QuestionCounter = 0;
    
        CurrentNumberOfQuestions = 0;
    
        lstNumbers = gcnew ArrayList();
        Random ^ rndNumber = gcnew 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);
    }
  14. Display the first form
  15. Double-click the Start button
  16. In the top section of the form, under the #pragma once, type #include "QuestionAnswer.h"
  17. Scroll down in the file and implement its event as follows:
     
    System::Void btnStart_Click(System::Object^  sender, System::EventArgs^  e)
    {
        QuestionAnswer ^ frmAnswer = gcnew QuestionAnswer();
        int numberOfQuestions = int::Parse(nudQuestions->Value.ToString());
    
        frmAnswer->TotalNumberOfQuestions = numberOfQuestions;
        frmAnswer->ShowDialog();
    }
  18. Display the first form again
  19. Double-click the Close button and implement its event as follows:
     
    System::Void btnClose_Click(System::Object^  sender, System::EventArgs^  e)
    {
    	 Close();
    }
  20. Execute the application and test the questions

Home Copyright © 2006-2016, FunctionX, Inc.