Multiple-Choice-Question

Introduction

A few months ago, I received a request on this site asking how to write a multiple-choice question such as those you see in tests and certifications. I realize that it is quite impossible to review all types of MCQs. Therefore, we will have only a glimpse of some steps you can follow to create such an application.

A multiple choice question is one of the most vague assignments you can receive. An MCQ completely depends on what you are trying to achieve (just like any other type of application). Therefore, the first thing you should do is to plan the application, on paper. What kind of questions? What kinds of answers? How many questions at a time? How will the user answer those questions?

There is usually one type of question in an MCQ. It displays like a string the user must read to perform the test.

There are three types of answers the user can be asked to use:

  • Single Choice Answer:
     
    Multiple Choice Question: Single Answer
     
    A single answer question provides a list of answers the user must select one from. The list can come as a combo box or a list box but it is usually given as radio buttons. In this type of list, the user can select only one answer.
  • Multiple Answers:
     

     
    The second type of answers also displays a list but the user is allowed to select more than one answer. Although the answers can display in a list box, they usually come in a list of check boxes. The user can select any of the check boxes. In this case, an answer, any answer, is set if the user checks it.
  • The other type of question allows the user to type some text. The user can be presented with a text box to fill out.

Any time you are asked to perform an assignment, as a programmer, one aspect that will take a lot of your time is the design, unless you are lucky to work in a group. In which case sometimes your job simply consists of applying someone else's concept. As beginning programmers that we (you and I) are, we are going to write a simply MCQ that consists of single choice answers.
 

Starting the Application

  1. Start Microsoft Visual C++. On the main menu, click File -> new...
  2. From the New dialog box, click the Projects property sheet and click MFC AppWizard (exe).
  3. In the Project Name edit box, type ExoMCQ1 and press Enter
  4. In the MFC AppWizard - Step 1, click the Dialog Based radio button and press Enter
  5. In the Title edit box, type Multiple Choice Question and press Enter.
  6. Click the No, Thanks You radio button and press Enter
  7. Press Enter twice or click Finish and OK.

 

 

 

 

Designing the Application

  1. Design the dialog box as follows:
     
    Multiple Choice Question: Dialog Design

    Here are the controls:

     
      

     
      Control ID Caption Property
    1 Group Box IDC_QUESTION_NBR Question  
    2 Edit Box IDC_EDIT_QUESTION   Multiline = checked Vertical Scroll = ON
    3 Group Box   Answers  
    4 Radio Button IDC_RDO_ANSWER1   Group = checked
    5 Radio Button IDC_RDO_ANSWER2   Group = checked
    6 Radio Button IDC_RDO_ANSWER3   Group = checked
    7 Radio Button IDC_RDO_ANSWER4   Group = checked
    8 Button IDC_NEXTQUESTION &Next Default = checked
    9 Button IDCANCEL &Close  

     

  2. Press Ctrl + W to access the ClassWizard dialog box.
  3. Click the Member Variables property sheet. Double-click each Control ID to Add a Member Variable. Create the following variables:
     
    MFC ClassWizard
  4. Click OK to close the ClassWizard.
  5. Return to the design of the dialog box and uncheck the Group check box of the bottom three radio buttons.
  6. Save your application

Programming the Application

Most programmers prefer to store the questions in a DLL, which is easy to do but we will use a simpler approach this time (maybe next time, we will make it more complicated or advanced). We are going to use a simple function whose main job will consist of displaying the question and the answers.

  1. In the Workspace, click the ClassView and expand the class folder. Double-click ExoMCQ1Dlg to display its header file.
    In the header file, declare the following variable:
     
    // ExoMCQ1Dlg.h : header file
    //
    
    #if !defined(AFX_EXOMCQ1DLG_H__2EC8A9B0_E93B_4980_B304_FFB6EFDC8D10__INCLUDED_)
    #define AFX_EXOMCQ1DLG_H__2EC8A9B0_E93B_4980_B304_FFB6EFDC8D10__INCLUDED_
    
    #if _MSC_VER > 1000
    #pragma once
    #endif // _MSC_VER > 1000
    
    /////////////////////////////////////////////////////////////////////////////
    // CExoMCQ1Dlg dialog
    
    class CExoMCQ1Dlg : public CDialog
    {
    // Construction
    public:
    	CExoMCQ1Dlg(CWnd* pParent = NULL);	// standard constructor
    
    // Dialog Data
    	//{{AFX_DATA(CExoMCQ1Dlg)
    	enum { IDD = IDD_EXOMCQ1_DIALOG };
    	CButton	m_Answer4;
    	CButton	m_Answer3;
    	CButton	m_Answer2;
    	CButton	m_Answer1;
    	CButton	m_QuestionNumber;
    	CButton	m_NextQuestion;
    	CString	m_Question;
    	int		m_Answer;
    	//}}AFX_DATA
    
    	// ClassWizard generated virtual function overrides
    	//{{AFX_VIRTUAL(CExoMCQ1Dlg)
    	protected:
    	virtual void DoDataExchange(CDataExchange* pDX);	// DDX/DDV support
    	//}}AFX_VIRTUAL
    
    // Implementation
    protected:
    	HICON m_hIcon;
    
    	// Generated message map functions
    	//{{AFX_MSG(CExoMCQ1Dlg)
    	virtual BOOL OnInitDialog();
    	afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
    	afx_msg void OnPaint();
    	afx_msg HCURSOR OnQueryDragIcon();
    	//}}AFX_MSG
    	DECLARE_MESSAGE_MAP()
    		
    private:
    	// The NumberOfQuestions variable is awkwardly declared here.
    	// It should be a constant that carries the number of questions that
    	// will be asked to the user. At the time of this writing, I haven't planned
    	// that aspect of the game, which means I don't know what to do with it.
    	// Therefore, I temporarily put it here
    	int NumberOfQuestions;
    };
    
    //{{AFX_INSERT_LOCATION}}
    // Microsoft Visual C++ will insert additional declarations immediately before the previous line.
    
    #endif // !defined(AFX_EXOMCQ1DLG_H__2EC8A9B0_E93B_4980_B304_FFB6EFDC8D10__INCLUDED_)
  2. Double-click the OnInitDialog method and initialize the variable as follows:
     
                    // TODO: Add extra initialization here
    	// At this time, we have only 30 questions
    	// Therefore, initialize the NumberOfQuestions variable to 30
    	NumberOfQuestions = 30;
    
    	return TRUE;  // return TRUE  unless you set the focus to a control
    }
  3. Right-click the ExoMCQ1 folder and click Add Member Function.
  4. Set the function type as int and, in the Function Name, type AskQuestion(const int q)
  5. Make the function Private and click OK.
  6. Implement the function as follows:
     
    int CExoMCQ1Dlg::AskQuestion(const int q)
    {
        int Answer;
    
        switch(q)
        {
        case 0:
    	m_Question.Format("Who was Jesus Christ\'s mother ?");
    	m_Answer1.SetWindowText("Leah");
    	m_Answer2.SetWindowText("Marthe");
    	m_Answer3.SetWindowText("Mary");
    	m_Answer4.SetWindowText("Salome");
    	Answer = 3;
    	break;
    
        case 1:
    	m_Question.Format("How many books are in the canonical Bible?");
    	m_Answer1.SetWindowText("33");
    	m_Answer2.SetWindowText("44");
    	m_Answer3.SetWindowText("55");
    	m_Answer4.SetWindowText("66");
    	Answer = 4;
    	break;
    
        case 2:
    	m_Question.Format("What is the first book of the Bible ?");
    	m_Answer1.SetWindowText("Malachi");
    	m_Answer2.SetWindowText("Isaiah");
    	m_Answer3.SetWindowText("Revelation");
    	m_Answer4.SetWindowText("Genesis");
    	Answer = 4;
    	break;
    
        case 3:
    	{
    	CString Quest("Which one of Jacob\'s sons was Joseph\'s ");
    	Quest += "brother\r\nhis own mother\'s son ?";
    		m_Question.Format(Quest); }
    	m_Answer1.SetWindowText("Levi");
    	m_Answer2.SetWindowText("Judah");
    	m_Answer3.SetWindowText("Asher");
    	m_Answer4.SetWindowText("Benjamin");
    	Answer = 4;
    	break;
    
        case 4:
    	{
    		CString Quest("What happened to Ananias and Sapphira after ");
    		            Quest += "they lied\r\nabout the money they collected ";
    		            Quest += "from selling the land?";
    			m_Question.Format(Quest); }
    	m_Answer1.SetWindowText("They died");
    	m_Answer2.SetWindowText("They got sick");
    	m_Answer3.SetWindowText("They were abandoned by the other discples in the bush");
    	m_Answer4.SetWindowText("They were drowned in the sea of Galilee");
    	Answer = 1;
    	break;
    
        case 5:
    	m_Question.Format("How many apostles did Jesus Christ have?");
    	m_Answer1.SetWindowText("3");
    	m_Answer2.SetWindowText("6");
    	m_Answer3.SetWindowText("9");
    	m_Answer4.SetWindowText("12");
    	Answer = 4;
    	break;
    
        case 6:
    	{
    		CString Quest("What was the craftiest of the wild animals ");
    				Quest += "the Lord\r\nhad made?";
    				m_Question.Format(Quest); }
    	m_Answer1.SetWindowText("The dragon");
    	m_Answer2.SetWindowText("The serpent");
    	m_Answer3.SetWindowText("The lion");
    	m_Answer4.SetWindowText("The Dove");
    	Answer = 2;
    	break;
    
        case 7:
    	{
    		CString Quest("To which disciple did Jesus promise the key ");
    			Quest += "of the kingdom\r\nof heaven ?";
    			m_Question.Format(Quest); }
    	m_Answer1.SetWindowText("John");
    	m_Answer2.SetWindowText("Peter");
    	m_Answer3.SetWindowText("Philip");
    	m_Answer4.SetWindowText("Paul");
    	Answer = 2;
    	break;
    
        case 8:
    	m_Question.Format("How does Paul address Timothy in 1 Timothy?");
    	m_Answer1.SetWindowText("My disciple");
    	m_Answer2.SetWindowText("My brother");
    	m_Answer3.SetWindowText("My son");
    	m_Answer4.SetWindowText("My father");
    	Answer = 3;
    	break;
    
        case 9:
    	m_Question.Format("Who was the father of the 12 patriarchs?");
    	m_Answer1.SetWindowText("Noah");
    	m_Answer2.SetWindowText("Abraham");
    	m_Answer3.SetWindowText("Isaac");
    	m_Answer4.SetWindowText("Jacob");
    	Answer = 4;
    	break;
    
        case 10:
    	m_Question.Format("Who was the firstborn son of Adam and Eve?");
    	m_Answer1.SetWindowText("Abel");
    	m_Answer2.SetWindowText("Cain");
    	m_Answer3.SetWindowText("Shem");
    	m_Answer4.SetWindowText("Japhet");
    	Answer = 2;
    	break;
    
        case 11:
    	m_Question.Format("To whom did James address his book?");
    	m_Answer1.SetWindowText("To the 12 tribes");
    	m_Answer2.SetWindowText("To those who have received faith");
    	m_Answer3.SetWindowText("To the believers");
    	m_Answer4.SetWindowText("To Epaphroditus");
    	Answer = 1;
    	break;
    
        case 12:
    	{
    		CString Quest("What did Micah from the hill country of Ephraim ");
    		Quest += "do of the young\r\nLevite who came to stay with him?";
    		m_Question.Format(Quest); }
    	m_Answer1.SetWindowText("He taught him to raise the sheep");
    	m_Answer2.SetWindowText("He made him a priest");
    	m_Answer3.SetWindowText("He killed him");
    	m_Answer4.SetWindowText("He sent him away");
    	Answer = 2;
    	break;
    
        case 13:
    	m_Question.Format("What was Paul's former name?");
    	m_Answer1.SetWindowText("Barnabas");
    	m_Answer2.SetWindowText("Mark");
    	m_Answer3.SetWindowText("Matthias");
    	m_Answer4.SetWindowText("Saul");
    	Answer = 4;
    	break;
    
        case 14:
    	m_Question.Format("In the parable of the weed, what does the field represent?");
    	m_Answer1.SetWindowText("The ocean");
    	m_Answer2.SetWindowText("The world");
    	m_Answer3.SetWindowText("The kingdom");
    	m_Answer4.SetWindowText("The Earth");
    	Answer = 2;
    	break;
    
        case 15:
    	m_Question.Format("How many people (including Noah) got in the ark he built?");
    	m_Answer1.SetWindowText("6");
    	m_Answer2.SetWindowText("7");
    	m_Answer3.SetWindowText("8");
    	m_Answer4.SetWindowText("9");
    	Answer = 3;
    	break;
    
        case 16:
    	m_Question.Format("Who was John the Baptist\'s mother?");
    	m_Answer1.SetWindowText("Salome");
    	m_Answer2.SetWindowText("Mary");
    	m_Answer3.SetWindowText("Elizabeth");
    	m_Answer4.SetWindowText("Rebecah");
    	Answer = 3;
    	break;
    
        case 17:
    	{
    	CString Quest("In how many pieces did the young Levite whose concubine ");
    		Quest += "was\r\nraped by the men of Gibeah cut her?";
    		m_Question.Format(Quest); }
    	m_Answer1.SetWindowText("3");
    	m_Answer2.SetWindowText("7");
    	m_Answer3.SetWindowText("12");
    	m_Answer4.SetWindowText("36");
    	Answer = 3;
    	break;
    
        case 18:
    	{
    	CString Quest("\"If anyone would come after me, he must deny\r\n");
    		Quest += "himself and take up his cross and follow me.\"\r\n";
    		Quest += "Who said or wrote it?";
    		m_Question.Format(Quest); }
    	m_Answer1.SetWindowText("Paul");	//( Mt 16:24 )
    	m_Answer2.SetWindowText("Moses");
    	m_Answer3.SetWindowText("John the Baptist");
    	m_Answer4.SetWindowText("Jesus Christ");
    	Answer = 4;
    	break;
    
        case 19:
    	m_Question.Format("How old was Abraham when he died?");
    	m_Answer1.SetWindowText("175");
    	m_Answer2.SetWindowText("230");
    	m_Answer3.SetWindowText("310");
    	m_Answer4.SetWindowText("425");
    	Answer = 1;
    	break;
    
        case 20:
    	m_Question.Format("Who said or wrote, \" You are the light of the world \"?");
    	m_Answer1.SetWindowText("Moses");
    	m_Answer2.SetWindowText("Paul");
    	m_Answer3.SetWindowText("Jesus Christ");
    	m_Answer4.SetWindowText("John");
    	Answer = 3;
    	break;
    
        case 21:
    	{
    	CString Quest("For how long did the rain fall on the earth while ");
    		Quest += "Noah and\r\nhis family were in the ark he built?";
    	    	m_Question.Format(Quest); }
    	m_Answer1.SetWindowText("30 days");
    	m_Answer2.SetWindowText("40 days");
    	m_Answer3.SetWindowText("50 days");
    	m_Answer4.SetWindowText("60 days");
    	Answer = 2;
    	break;
    
        case 22:
    	m_Question.Format("Where was Samson getting his strength from?");//( Jg 16:17 ) 
    	m_Answer1.SetWindowText("His hands");
    	m_Answer2.SetWindowText("His legs");
    	m_Answer3.SetWindowText("His Head");
    	m_Answer4.SetWindowText("His hair");
    	Answer = 4;
    	break;
    
        case 23:
    	{
    		CString Quest("\"If any of you lacks wisdom, he should ask God,\r\n");
    		Quest += "who gives generously to all without finding fault, ";
    		Quest += "and it will be given to him.\"\r\nThis is from";
    		m_Question.Format(Quest); }
    	m_Answer1.SetWindowText("Ex 30:12");//( Ja 1:5 )
    	m_Answer2.SetWindowText("Mt 6:24");
    	m_Answer3.SetWindowText("He 8:3");
    	m_Answer4.SetWindowText("Ja 1:5");
    	Answer = 4;
    	break;
    
        case 24:
    	{
    		CString Quest("These two prophets appeared with Jesus at ");
    		Quest += "the transfiguration";//( Mt 17:3 )
    		m_Question.Format(Quest); }
    	m_Answer1.SetWindowText("Isaiah and Nathan");
    	m_Answer2.SetWindowText("Elisha and Jeremiah");
    	m_Answer3.SetWindowText("Moses and Elijah");
    	m_Answer4.SetWindowText("Joel and Daniel");
    	Answer = 3;
    	break;
    
        case 25:
    	m_Question.Format("Who inherited everything Abraham had?");//( Ge 25:5 )
    	m_Answer1.SetWindowText("Ishmael");
    	m_Answer2.SetWindowText("Isaac");
    	m_Answer3.SetWindowText("Jacob");
    	m_Answer4.SetWindowText("David");
    	Answer = 2;
    	break;
    
        case 26:
    	{
    		CString Quest("In the parable of the weed, who is the enemy who sows ");
    		Quest += "the sons\r\nof the evil one?";//( Mt 13:39
    		m_Question.Format(Quest); }
    	m_Answer1.SetWindowText("The demon-possessed");
    	m_Answer2.SetWindowText("The devil");
    	m_Answer3.SetWindowText("The earth");
    	m_Answer4.SetWindowText("The angel");
    	Answer = 2;
    	break;
    
        case 27:
    	m_Question.Format("According to Peter, who is the Holy and Righteous one?");
    	m_Answer1.SetWindowText("Moses");//( Ac 3:14 )
    	m_Answer2.SetWindowText("Elijah");
    	m_Answer3.SetWindowText("Jesus Christ");
    	m_Answer4.SetWindowText("David");
    	Answer = 3;
    	break;
    
        case 28:
    	{
    		CString Quest("\"Anyone, then, who knows the good he ought to do ");
    		Quest += "and\r\ndoesn\'t do it, sins.\"\r\nThis is from";//  ( Ja 4:17 )
    		m_Question.Format(Quest); }
    	m_Answer1.SetWindowText("Pr 6:16");
    	m_Answer2.SetWindowText("Jn 10:31");
    	m_Answer3.SetWindowText("Ga 4:22");
    	m_Answer4.SetWindowText("Ja 4:17");
    	Answer = 4;
    	break;
    
        case 29:
    	m_Question.Format("How many sons did Jacob have ?");// Ge 29:31--30:24 )
    	m_Answer1.SetWindowText("8");
    	m_Answer2.SetWindowText("12");
    	m_Answer3.SetWindowText("16");
    	m_Answer4.SetWindowText("24");
    	Answer = 12;
    	break;
        }
    
        // Return the correct answer
        return Answer;
    }
  7. Press Ctrl + W to access the ClassWizard. In the Object ID list of the Message Maps property page, click IDC_NEXTQUESTION. In the Messages list, double-click BN_CLICKED, set the function name to OnNextQuestion
  8. Click OK and click Edit Code.
  9. Implement the function as follows:
     
    void CExoMCQ1Dlg::OnNextQuestion() 
    {
    	// TODO: Add your control notification handler code here
    	// Transfer the application process to this function
        UpdateData();
    
        // When a question is about to be asked,
        // make sure no answer is selected
        m_Answer = -1;
    	char QNbr[10];
    
        static int q = 0;
    
        if(q < NumberOfQuestions)
        {
            sprintf(QNbr, "Question %d", q+1);
    
            m_QuestionNumber.SetWindowText(QNbr);
    		AskQuestion(q);
        }
    
        q++;
    
        // Transfer the process back to the operating system
        // This is one of Microsoft stupid things.
        // Programmers should not go through this.
        // Why? Why? Why? This burden on the programmer doesn't make sense
        UpdateData(FALSE);
    }
  10. To make sure the dialog displays a question when it opens, change the OnInitDialog() function as follows:
     
     BOOL CExoMCQ1Dlg::OnInitDialog()
    {
    	CDialog::OnInitDialog();
    
    	// Add "About..." menu item to system menu.
    
    	// IDM_ABOUTBOX must be in the system command range.
    	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
    	ASSERT(IDM_ABOUTBOX < 0xF000);
    
    	CMenu* pSysMenu = GetSystemMenu(FALSE);
    	if (pSysMenu != NULL)
    	{
    		CString strAboutMenu;
    		strAboutMenu.LoadString(IDS_ABOUTBOX);
    		if (!strAboutMenu.IsEmpty())
    		{
    			pSysMenu->AppendMenu(MF_SEPARATOR);
    			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
    		}
    	}
    
    	SetIcon(m_hIcon, TRUE);			// Set big icon
    	SetIcon(m_hIcon, FALSE);		// Set small icon
    	
    	// TODO: Add extra initialization here
    	// At this time, we have only 30 questions
    	// Therefore, initialize the NumberOfQuestions variable to 30
    	NumberOfQuestions = 30;
    
    	// To make the game start when the dialog box comes up, behave
    	// as if the user has just clicked the Next button
    	OnNextQuestion();
    
    	return TRUE;  // return TRUE  unless you set the focus to a control
    }
  11. Test your program and click Next a few times to make sure the questions are changing
     
 

Home Copyright © 2003-2015, FunctionX