Home

Multiple-Choice Question: Multiple Answers

 

Introduction

A multiple-choice exercise usually consists of single answers. This can done using radio buttons. Another type of multiple-choice application can be made of single and multiple answers. In such a program, the user can  be presented with two types of questions, some requiring one answer out of many options, and some others allowing the user to select more than one answer. When creating the application, you must find a way to alternate the questions. Furthermore, you may want to randomly present the questions. There are different ways you can do this.

Practical Learning Practical Learning: Creating a Multiple-Choice Test 

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

Single and Multi-Answer Questions

If you want to create a multiple-choice application, you must find a way to alternate questions. There are various ways you can do this. You can create a series of radio buttons and check boxes. Then, when asking a question that expects a single answer, you can present only the radio buttons. When a question expects more than one answer, you can present the check boxes. Because of the way controls are built, you can use group boxes to group controls so that, when a series of controls is needed, you can display its corresponding group of questions and hide the other group box.

 

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   grpSingleAnswers  
    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. On the form, click the group box to select
  6. On the main menu, click Edit -> Copy
  7. To add another form, on the main menu, click Project -> Add Windows Form...
  8. In the Add New Item dialog box, set the Name Experiment and press Enter
  9. Enlarge the form enough and, on the main menu, click Edit -> Paste
  10. Design the form as follows:
     
    Control Caption Name Additional Properties
    Group Box   grpMultipleAnswers  
    CheckBox   chkAnswer1 AutoSize: False
    CheckAlign: TopLeft
    TextAlign: TopLeft
    CheckBox   chkAnswer2 AutoSize: False
    CheckAlign: TopLeft
    TextAlign: TopLeft
    CheckBox   chkAnswer3 AutoSize: False
    CheckAlign: TopLeft
    TextAlign: TopLeft
    CheckBox   chkAnswer4 AutoSize: False
    CheckAlign: TopLeft
    TextAlign: TopLeft
  11. On the Experiment form, click the group box to select it
  12. On the main menu, click Edit -> Copy
  13. Display the QuestionAnswer form
  14. Click an unoccupied area of the form to make sure no control (especially not the group box) is selected
  15. On the main menu, click Edit -> Paste (the idea is to make sure that one group box doesn't act as the parent control of another group box; otherwise, the application may not produce the intended result)
  16. Move the new group box to place it exactly on top of the other group box
     
  17. On the form, double-click the Quit button
  18. 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 CSharpKnowledge1
    {
        public partial class QuestionAnswer : Form
        {
            public int TotalNumberOfQuestions;
    
            ArrayList lstNumbers;
            int CurrentQuestion;
            int QuestionCounter;
            int CurrentNumberOfQuestions;
            double Sum;
    
            public QuestionAnswer()
            {
                InitializeComponent();
            }
    
            void PresentQuestion(int qstNumber)
            {
                lblQuestion.Visible = true;
                txtQuestion.Visible = true;
    
                rdoAnswer1.Checked = false;
                rdoAnswer2.Checked = false;
                rdoAnswer3.Checked = false;
                rdoAnswer4.Checked = false;
                rdoAnswer5.Checked = false;
    
                chkAnswer1.Checked = false;
                chkAnswer2.Checked = false;
                chkAnswer3.Checked = false;
                chkAnswer4.Checked = false;
                chkAnswer5.Checked = false;
    
                switch (qstNumber)
                {
    
                    case 1: // Single Answers
                        txtQuestion.Text = "Which one of the following was a new language " +
                                           "addition to C# 2.0?";
    
                        grpSingleChoice.Visible = true;
                        grpMultipleChoice.Visible = false;
    
                        rdoAnswer1.Text = "A. Arrays";
                        rdoAnswer2.Text = "B. The decimal data type";
                        rdoAnswer3.Text = "C. Generics";
                        rdoAnswer4.Text = "D. unions";
                        rdoAnswer5.Visible = false;
                        break;
    
                    case 2: // Multiple Answers
                        txtQuestion.Text = "Select C# Keywords (Choose two)";
    
                        grpSingleChoice.Visible = false;
                        grpMultipleChoice.Visible = true;
    
                        chkAnswer1.Text = "A. array";
                        chkAnswer2.Text = "B. abstract";
                        chkAnswer3.Text = "C. fixed";
                        chkAnswer4.Text = "D. nullptr";
                        chkAnswer5.Visible = true;
                        chkAnswer5.Text = "E. template";
                        break;
    
                    case 3: // Single Answers
                        txtQuestion.Text = "What is wrong with the following code?" + Environment.NewLine +
                                           "class Class" + Environment.NewLine +
    "{" + Environment.NewLine +
    "    private void ShowMessage()" + Environment.NewLine +
    "    {" + Environment.NewLine +
    "	Console::WriteLine(\"Welcome to C# Programming\");" + Environment.NewLine +
    "    }" + Environment.NewLine +
    "}";
    
                        grpSingleChoice.Visible = true;
                        grpMultipleChoice.Visible = false;
    
                        rdoAnswer1.Text = "A. A class cannot be named Class";
                        rdoAnswer2.Text = "B. The creation of a class must end with a semi-colon";
                        rdoAnswer3.Text = "C. The ShowMessage() method must be declared static";
                        rdoAnswer4.Text = "D. The \"::\" operator is not valid";
                        rdoAnswer5.Visible = false;
                        break;
    
                    case 4: // Single Answers
                        txtQuestion.Text = "Imagine you write code and include a backslash " +
                                           "in a string, the compiler would try to " +
                                           "find its equivalent escape sequence. If you " +
                                           "want the compiler the consider the backslashes " +
                                           "as normal characters, what symbol can you use " +
                                           "in the beginning of the string?";
    
                        grpSingleChoice.Visible = true;
                        grpMultipleChoice.Visible = false;
    
                        rdoAnswer1.Text = "A. #";
                        rdoAnswer2.Text = "B. @";
                        rdoAnswer3.Text = "C. &";
                        rdoAnswer4.Text = "D. ^";
                        rdoAnswer5.Visible = true;
                        rdoAnswer5.Text = "E. %";
                        break;
    
                    case 5: // Multiple Answers
                        txtQuestion.Text = "What are the characteristics of the " +
                                           "Main method? (Choose two)";
    
                        grpSingleChoice.Visible = false;
                        grpMultipleChoice.Visible = true;
    
                        chkAnswer1.Text = "A. It must always return void";
                        chkAnswer2.Text = "B. It is always the first method inside the " +
                                          "main class of a program";
                        chkAnswer3.Text = "C. It is the start and end point of a program";
                        chkAnswer4.Text = "D. It must always be passed an array of strings " +
                                          "as in static void Main(string[] args)";
                        chkAnswer5.Visible = true;
                        chkAnswer5.Text = "E. It must be created inside of a class" +
                                          "or a structure";
                        break;
    
                    case 6: // Single Answers
                        txtQuestion.Text = "What is a contextual keyword?";
    
                        grpSingleChoice.Visible = true;
                        grpMultipleChoice.Visible = false;
    
                        rdoAnswer1.Text = "A. A contextual keyword is a word that acts as " +
                                          "a keyword depending on how it is used. For " +
                                          "example the word \"value\" is a keyword in a set " +
                                          "accessor of a property but, in a different " +
                                          "context, you can declare a variable and " +
                                          "name it \"value\"";
                        rdoAnswer2.Text = "B. A contextual keyword is one of the new " +
                                          "keywords that were introduced in C# 2.0, such " +
                                          "as \"generic\" or \"where\"";
                        rdoAnswer3.Text = "C. A contextual keyword is a word that is " +
                                          "a keyword in another language of the .NET " +
                                          "Framework and can be used as a keyword in a " +
                                          "C# application";
                        rdoAnswer4.Text = "D. A contextual keyword is a special word " +
                                          "used inside of an assembly to make a library " +
                                          "created in C# compatible with an assembly " +
                                          "created in another language of the .NET " +
                                          "Framework, such as Microsoft Visual Basic 2005";
    
                        rdoAnswer5.Visible = false;
                        break;
    
                    case 7: // Single Answers
                        txtQuestion.Text = "Imagine you have created a basic C# application " +
                                           "using only the built-in libraries of the .NET Framework. " +
                                           "If you want to distribute the application to a " +
                                           "friend, what is the minimum action you must " +
                                           "perform before delivering it";
    
                        grpSingleChoice.Visible = true;
                        grpMultipleChoice.Visible = false;
    
                        rdoAnswer1.Text = "A. You must supply the csc compiler to the friend";
                        rdoAnswer2.Text = "B. You must access the friend's computer, recompile " +
                                          "the application on that computer and install the " +
                                          "CLR.dll on that other computer";
                        rdoAnswer3.Text = "C. Simply make sure the .NET Frameword is installed " +
                                          "on the friend's computer. Then distribute your " +
                                          "executable";
                        rdoAnswer4.Text = "D. The only real requirement is to make sure that " +
                                          "the friend's computer is running either Microsoft " +
                                          "Windows 2000, Windows XP, or Windows Server 2003";
                        rdoAnswer5.Visible = false;
                        break;
    
                    case 8: // Single Answers
                        txtQuestion.Text = "How is the \"where\" keyword used??";
    
                        grpSingleChoice.Visible = true;
                        grpMultipleChoice.Visible = false;
    
                        rdoAnswer1.Text = "A. To put a constraint in a generic";
                        rdoAnswer2.Text = "B. To show a restriction in a \"for\" loop";
                        rdoAnswer3.Text = "C. To indicate when \"value\" becomes a keyword";
                        rdoAnswer4.Text = "D. To indicate that a class is \"sealed\"";
                        rdoAnswer5.Visible = false;
                        break;
    
                    case 9: // Multiple Answers
                        txtQuestion.Text = "What is wrong with the following code? " + Environment.NewLine +
                                           "(Choose two)" + Environment.NewLine + Environment.NewLine +
    "protected class Exercise" + Environment.NewLine +
    "{" + Environment.NewLine +
    "    public static generic()" + Environment.NewLine +
    "    {" + Environment.NewLine +
    "        double x = 120.50f;" + Environment.NewLine +
    "        double y;" + Environment.NewLine +
    "        return x + y;" + Environment.NewLine +
    "    }" + Environment.NewLine +
    "};";
                        grpSingleChoice.Visible = false;
                        grpMultipleChoice.Visible = true;
    
                        chkAnswer1.Text = "A. The letter f in the initialization of " +
                                          "the first variable must be replaced with d " +
                                          "otherwise it would create a compile error";
                        chkAnswer2.Text = "B. The method doesn't return an appropriate " +
                                          "value. The x + y return value must be cast " +
                                          "to double. " +
                                          "A solution would be return (double)(x + y);";
                        chkAnswer3.Text = "C. The variable y must be initialized " +
                                          "before being used";
                        chkAnswer4.Text = "D. The method doesn't have a return type";
                        chkAnswer5.Visible = true;
                        chkAnswer5.Text = "E. Because \"generic\" is a keyword, no " +
                                          "method can be named \"generic\"";
                        break;
    
                    case 10: // Single Answers
                        txtQuestion.Text = "Imagine you have created a file in " +
                                           "Notepad and saved it as \"Exercise.cs\". " +
                                           "If you want to compile the project from " +
                                           "the Command Prompt, what command would " +
                                           "you issue?";
    
                        grpSingleChoice.Visible = true;
                        grpMultipleChoice.Visible = false;
    
                        rdoAnswer1.Text = "A. csc Exercise.cs";
                        rdoAnswer2.Text = "B. CSharp Exercise.cs";
                        rdoAnswer3.Text = "C. msvs csc Exercise.cs";
                        rdoAnswer4.Text = "D. /csc Exercise.cs /target:executable";
                        rdoAnswer5.Visible = true;
                        rdoAnswer5.Text = "E. cs /Compile /Exercise.cs";
                        break;
    
                    case 11: // Single Answers
                        txtQuestion.Text = "What is the difference between " +
                                           "the \"else\" keyword and #else?";
    
                        grpSingleChoice.Visible = true;
                        grpMultipleChoice.Visible = false;
    
                        rdoAnswer1.Text = "A. The \"else\" keyword is used in combination with " +
                                          "the \"where\" keyword when creating a generic while " +
                                          "#else is a preprocessor";
                        rdoAnswer2.Text = "B. The \"else\" keyword is used in a conditional statement " +
                                          "with the \"if\" keyword but " +
                                          "#else is a preprocessor";
                        rdoAnswer3.Text = "C. There is no actual difference between \"else\" " +
                                          "and #else as long as \"else\" is used with \"if\" and " +
                                          "#else is used with #if";
                        rdoAnswer4.Text = "D. The \"else\" keyword is used in a static method " +
                                          "that returns a \"params\" value and " +
                                          "#else is a preprocessor";
                        rdoAnswer5.Visible = false;
                        break;
    
                    case 12: // Single Answers
                        txtQuestion.Text = "How do you define boxing?";
    
                        grpSingleChoice.Visible = true;
                        grpMultipleChoice.Visible = false;
    
                        rdoAnswer1.Text = "A. Boxing a value allows it to be cast to an Object type";
                        rdoAnswer2.Text = "B. Boxing is used in an array to specify the " +
                                          "value of an element";
                        rdoAnswer3.Text = "C. Boxing is a generic feature that indicates the " +
                                          "base class of a derived type";
                        rdoAnswer4.Text = "D. Boxing is a technique of differentiating between " +
                                          "a value type and a reference type";
                        rdoAnswer5.Visible = false;
                        rdoAnswer5.Text = "E. ";
                        break;
    
                    case 13: // Multiple Answers
                        txtQuestion.Text = "Which two of the following are keywords in C#?";
    
                        grpSingleChoice.Visible = false;
                        grpMultipleChoice.Visible = true;
    
                        chkAnswer1.Text = "A. internal";
                        chkAnswer2.Text = "B. fixed";
                        chkAnswer3.Text = "C. select";
                        chkAnswer4.Text = "D. final";
                        chkAnswer5.Visible = true;
                        chkAnswer5.Text = "E. asm";
                        break;
    
                    case 14: // Single Answers
                        txtQuestion.Text = "What is a read-only property?";
    
                        grpSingleChoice.Visible = true;
                        grpMultipleChoice.Visible = false;
    
                        rdoAnswer1.Text = "A. A read-only property is a property that defines " +
                                          "only a get accessor and no set accessor";
                        rdoAnswer2.Text = "B. A read-only property is a property that defines " +
                                          "only a set accessor and no get accessor";
                        rdoAnswer3.Text = "C. A read-only property is a property that defines " +
                                          "only a let accessor without a met accessor";
                        rdoAnswer4.Text = "D. A read-only property is a property that defines " +
                                          "only a met accessor without a let accessor";
    
                        rdoAnswer5.Visible = false;
                        break;
    
                    case 15: // Single Answers
                        txtQuestion.Text = "What is a write-only property?";
    
                        grpSingleChoice.Visible = true;
                        grpMultipleChoice.Visible = false;
    
                        rdoAnswer1.Text = "A. A write-only property is a property with only a get accessor";
                        rdoAnswer2.Text = "B. A write-only property is a property with only a set accessor";
                        rdoAnswer3.Text = "C. A write-only property is a property with only a let accessor";
                        rdoAnswer4.Text = "D. A write-only property is a property with only a met accessor";
    
                        rdoAnswer5.Visible = false;
                        break;
    
                    case 16: // Multiple Answers
                        txtQuestion.Text = "In what circumstances are the square " +
                                           "brackets ([]) used? (Choose three)";
    
                        grpSingleChoice.Visible = false;
                        grpMultipleChoice.Visible = true;
    
                        chkAnswer1.Text = "A. In static methods";
                        chkAnswer2.Text = "B. With arrays";
                        chkAnswer3.Text = "C. In the \"throw\" block of an exception handling";
                        chkAnswer4.Text = "D. In attributes";
    
                        chkAnswer5.Visible = true;
                        chkAnswer5.Text = "E. As indexers";
                        break;
    
                    case 17: // Multiple Answers
                        txtQuestion.Text = "Which ones of the following statements " +
                                           "apply to C#? (Choose three)";
    
                        grpSingleChoice.Visible = false;
                        grpMultipleChoice.Visible = true;
    
                        chkAnswer1.Text = "A. All structures are based on the Value structure";
                        chkAnswer2.Text = "B. Every C# application must contain at " +
                                          "least one class";
                        chkAnswer3.Text = "C. As in C and C++, a class can be derived " +
                                          "from a structure";
                        chkAnswer4.Text = "D. All classes are based on the object class";
    
                        chkAnswer5.Visible = true;
                        chkAnswer5.Text = "E. A structure may not declare a default constructor";
                        break;
    
                    case 18: // Single Answers
                        txtQuestion.Text = "What is the difference between a ref " +
                                           "and an out parameters?";
    
                        grpSingleChoice.Visible = true;
                        grpMultipleChoice.Visible = false;
    
                        rdoAnswer1.Text = "A. A variable passed as a ref argument " +
                                          "doesn't have to be initialized first but " +
                                          "an argument passed as out must primarily " +
                                          "be initialized";
                        rdoAnswer2.Text = "B. There is no actual difference between " +
                                          "both keywords. Each is used to pass an " +
                                          "argument by reference so the calling method " +
                                          "can alter it";
                        rdoAnswer3.Text = "C. An argument passed as ref must be " +
                                          "primarily initialized while a variable " +
                                          "passed as an out argument doesn't have " +
                                          "to be initialized first";
                        rdoAnswer4.Text = "D. When passing an argument as out, its " +
                                          "method must be declared as static but a method " +
                                          "that is passed a ref argument can return " +
                                          "any value";
                        rdoAnswer5.Visible = false;
                        break;
    
                    case 19: // Multiple Answers
                        txtQuestion.Text = "What are the differences between a class and a " +
                                           "structure? (Choose two)";
    
                        grpSingleChoice.Visible = false;
                        grpMultipleChoice.Visible = true;
    
                        chkAnswer1.Text = "A. Structures are considered value types while " +
                                          "classes are reference types";
                        chkAnswer2.Text = "B. All structures are derived from the Value " +
                                          "struct while all classes are based from the " +
                                          "Object class";
                        chkAnswer3.Text = "C. A structure may not declare a default " +
                                          "constructor but a class can contain a " +
                                          "default constructor";
                        chkAnswer4.Text = "D. When a class is passed to a method, a copy " +
                                          "of its value is made available to the method but " +
                                          "when a structure is passed to a method, a " +
                                          "reference of that structure is given to the method";
                        chkAnswer5.Visible = true;
                        chkAnswer5.Text = "A class can be created as generic but " +
                                          "a structure cannot";
                        break;
    
                    case 20: // Multiple Answers
                        txtQuestion.Text = "What are the two ways you can use the \"using\" keyword?";
    
                        grpSingleChoice.Visible = false;
                        grpMultipleChoice.Visible = true;
    
                        chkAnswer1.Text = "A. To show that the following section is a conditional statement";
                        chkAnswer2.Text = "B. To indicate an indexed property in an internal class";
                        chkAnswer3.Text = "C. To create an alias for a namespace or a type";
                        chkAnswer4.Text = "D. To define a scope at the end of which an object would be deleted";
                        chkAnswer5.Visible = true;
                        chkAnswer5.Text = "E. To specify when an if...else conditional statement " +
                                          "would contain a nested if...else statement";
                        break;
    
                    case 21: // Single Answers
                        txtQuestion.Text = "How can the \"extern\" keyword be used?";
    
                        grpSingleChoice.Visible = true;
                        grpMultipleChoice.Visible = false;
    
                        rdoAnswer1.Text = "A. The \"extern\" keyword is used when creating " +
                                          "a class in an assembly that would be used by a " +
                                          "language other than C#, such as Visual Basic";
                        rdoAnswer2.Text = "B. The \"extern\" keyword is used in a derived " +
                                          "generic class whose base class is not generic";
                        rdoAnswer3.Text = "C. The \"extern\" keyword in a structure " +
                                          "indicates that the structure is based on the " +
                                          "Object class instead of the ValueType structure";
                        rdoAnswer4.Text = "D. The \"extern\" keyword can be used to declare " +
                                          "a method that is defined in an external library";
                        rdoAnswer5.Visible = false;
                        break;
                }
            }
    
            void CheckAnswer(int qstNumber)
            {
                switch (qstNumber)
                {
    
                    case 1:
                        if (rdoAnswer1.Checked == true)
                        {
                            MessageBox.Show("-- Wrong Answer --\n" +
                                            "Arrays are not a new addition in C#. They have " +
                                            "been used in C# since the early implementations " +
                                            "of the language");
                        }
                        else if (rdoAnswer2.Checked == true)
                        {
                            MessageBox.Show("-- Wrong Answer --\n" +
                                            "The decimal, along with the double data types, " +
                                            "has been part of C# since the beginning.");
                        }
                        else if (rdoAnswer3.Checked == true)
                        {
                            MessageBox.Show("=-= Right Answer =-=\n" +
                                            "Generics have been added to support parameterized types in C#.");
                            Sum++;
                        }
                        else if (rdoAnswer4.Checked == true)
                        {
                            MessageBox.Show("-- Wrong Answer --\n" +
                                            "Unions, a feature of C and C++, are not used in C#");
                        }
                        else
                            MessageBox.Show("-- Wrong Answer --");
                        break;
    
                    case 2:
                        if ((chkAnswer2.Checked == true) && (chkAnswer3.Checked == true))
                        {
                            MessageBox.Show("=-= Correct =-=\n" +
                                            "abstract and fixed are keywords of the C# language.");
                            Sum++;
                        }
                        else
                            MessageBox.Show("-- Wrong Answer --\n" +
                                            "In C++/CLI, array is a keyword but not in C#.\n" +
                                            "nullptr is a keyword in C++/CLI, not in C#.\n" +
                                            "template is a keyword in C++, not a keyword in C#.");
                        break;
    
                    case 3:
                        if (rdoAnswer1.Checked == true)
                            MessageBox.Show("--- Wrong Answer ---\n" +
                                            "Since C# is case-sensitive, the word Class (with C in " +
                                            "uppercase) is not a keyword. This means that a class " +
                                            "or a variable can be named Class");
                        else if (rdoAnswer2.Checked == true)
                            MessageBox.Show("--- Wrong Answer ---\n" +
                                            "Although in C++ the creation of a class must " +
                                            "end with a semi-colon, this is only optional in C#");
                        else if (rdoAnswer3.Checked == true)
                            MessageBox.Show("--- Wrong Answer ---\n" +
                                            "The ShowMessage() doesn't have to be declared static");
                        else if (rdoAnswer4.Checked == true)
                        {
                            MessageBox.Show("=-= Correct Answer =-=\n" +
                                            "Although valid in C++, the \"::\" operator is not used in C#.");
                            Sum++;
                        }
                        else
                            MessageBox.Show("-- Wrong Answer --");
                        break;
    
                    case 4:
                        if (rdoAnswer1.Checked == true)
                        {
                            MessageBox.Show("=-= Wrong Answer=-=\n" +
                                            "The # symbol doesn't mean anything in the beginning of a string.");
                        }
                        else if (rdoAnswer2.Checked == true)
                        {
    
                            MessageBox.Show("=-= Right Answer=-=\n" +
                                            "If you start a string with the @ symbol, the escape sequences in " +
                                            "string would not be treated as such.");
                            Sum++;
                        }
    
                        else if (rdoAnswer3.Checked == true)
                        {
    
                            MessageBox.Show("-- Wrong Answer --\n" +
                                            "The & character is used in Microsoft Visual Basic to concatenate " +
                                            "a string but it doesn't play the same role in C#.");
                        }
                        else if (rdoAnswer4.Checked == true)
                        {
    
                            MessageBox.Show("-- Wrong Answer --\n" +
                                            "The ^ symbol is not in C++/CLI to create a handle. In C#, this " +
                                            "character is not used to ignore escape sequences in a string.");
                        }
                        else
                            MessageBox.Show("-- Wrong Answer --");
                        break;
    
                    case 5:
                        if ((chkAnswer3.Checked == true) && (chkAnswer5.Checked == true))
                        {
                            MessageBox.Show("=-= Right Answer =-=\n" +
                                            "C. Correct: The Main() method holds the start and end point of a program.\n" +
                                            "E. Correct: In C#, every method, including Main(), must be created in a class.");
                            Sum++;
    
                        }
                        else
                            MessageBox.Show("-- Wrong Answer --\n" +
                                            "A. Wrong: The Main() method doesn't have to always return void. It " +
                                            "can return an int\n" +
                                            "B. Wrong: Main()  doesn't have to be the first method of its class. " +
                                            "In fact, it can as well be the last." +
                                            "D. Wrong: The Main() method can be implemented without any argument at all " +
                                            "as in static void Main().");
                        break;
    
                    case 6:
                        if (rdoAnswer1.Checked == true)
                        {
                            MessageBox.Show("=-= Right Answer=-=\n" +
                                            "C# contextual keywords are get, partial, set, where, " +
                                            "yield, and value.");
                            Sum++;
                        }
                        else if (rdoAnswer2.Checked == true)
                            MessageBox.Show("-- Wrong Answer --\n" +
                                            "Contextual keywords were already available in the first " +
                                            "implementation of C#, not an addition to the language.");
                        else if (rdoAnswer3.Checked == true)
                            MessageBox.Show("-- Wrong Answer --\n" +
                                            "A contextual keyword has to do with its own language. It " +
                                            "has nothing to do with another language.");
                        else if (rdoAnswer4.Checked == true)
                        {
                            MessageBox.Show("=-= Wrong Answer =-=\n" +
                                            "Contextual keywords have nothing to do with assemblies, " +
                                            "libraries, the .NET Framework, or Microsoft Visual Studio.");
                        }
                        else
                            MessageBox.Show("-- Wrong Answer --");
                        break;
    
                    case 7:
                        if (rdoAnswer1.Checked == true)
                            MessageBox.Show("-- Wrong Answer --\n" +
                                            "Users of your application don't need the C# compiler " +
                                            "to execute your application.");
                        else if (rdoAnswer2.Checked == true)
                            MessageBox.Show("-- Wrong Answer --\n" +
                                            "Once your application has been compiled, you have an " +
                                            "executable. There is no reason to recompile an " +
                                            "application on the user's compiler.");
                        else if (rdoAnswer3.Checked == true)
                        {
                            MessageBox.Show("=-= Correct Answer =-=\n" +
                                            "If you used only the built-in libraries of the .NET " +
                                            "Framework when creating your application, to " +
                                            "distribute it, make sure the .NET Framework is installed " +
                                            "on the user's computer. If it's not, then you should " +
                                            "install it or have the user install it. Once the other " +
                                            "computer has the .NET Framework, you can install your " +
                                            "application's executable and it would work fine.");
                            Sum++;
                        }
                        else if (rdoAnswer4.Checked == true)
                            MessageBox.Show("-- Wrong Answer --\n" +
                                            "The operating system is not the major requirement for " +
                                            "your application to run on a computer.");
                        else
                            MessageBox.Show("-- Wrong Answer --");
                        break;
    
                    case 8:
                        if (rdoAnswer1.Checked == true)
                        {
                            MessageBox.Show("=-= Correct Answer =-=\n" +
                                            "\"where\" is a contextual keyword used in generics to " +
                                            "express a constraint.");
                            Sum++;
                        }
                        else if (rdoAnswer2.Checked == true)
                            MessageBox.Show("-- Wrong Answer --\n" +
                                            "The \"where\" contextual keyword and the \"for\" " +
                                            "keywords have nothing to do with each other.");
                        else if (rdoAnswer3.Checked == true)
                            MessageBox.Show("-- Wrong Answer --\n" +
                                            "The contextual keywords \"where\" and \"value\" don't " +
                                            "have much in common.");
                        else if (rdoAnswer4.Checked == true)
    
                            MessageBox.Show("-- Wrong Answer --\n" +
                                            "The \"where\" keyword doesn't specify whether a " +
                                            "class is sealed or not.");
                        else
                            MessageBox.Show("-- Wrong Answer --");
                        break;
    
                    case 9:
                        if ((chkAnswer3.Checked == true) && (chkAnswer4.Checked == true))
                        {
                            MessageBox.Show("=-= Correct Answer =-=\n" +
                                            "C. Before using a variable in C#, it must have " +
                                            "been initialized first.\n" +
                                            "D. This method doesn't specify its return type. " +
                                            "The static keyword is not considered a return type.");
                            Sum++;
                        }
                        else
                            MessageBox.Show("-- Wrong Answer --\n" +
                                            "A: A double variable can be initialized with " +
                                            "a value that ends with f or d. The f suffix " +
                                            "would indicate to the compiler that the " +
                                            "variable should be treated as a float.\n" +
                                            "B: The method returns an appropriate value.\n" +
                                            "E. In C#, \"generic\" is not a keyword. This " +
                                            "means that a variable, a class or a method " +
                                            "can be named \"generic\".");
                        break;
    
                    case 10:
                        if (rdoAnswer1.Checked == true)
                        {
                            MessageBox.Show("=-= Correct Answer =-=\n" +
                                            "The C# compiler of the .NET Framework " +
                                            "is named csc and you can use it at the " +
                                            "Command Prompt, followed by the name of " +
                                            "the file, to compile it.");
                            Sum++;
                        }
                        else if (rdoAnswer2.Checked == true)
                        {
                            MessageBox.Show("-- Wrong Answer --\n" +
                                            "The C# compiler is not named CSharp, and " +
                                            "there is no command named CSharp.");
                        }
                        else if (rdoAnswer3.Checked == true)
                        {
                            MessageBox.Show("-- Wrong Answer --\n" +
                                            "There is no such a compiler or parameter " +
                                            "named msvs. Therefore, this command line " +
                                            "is wrong.");
                        }
                        else if (rdoAnswer4.Checked == true)
                        {
                            MessageBox.Show("-- Wrong Answer --\n" +
                                            "The command line used to compile a project " +
                                            "doesn't start with a forward slash.");
                        }
                        else
                            MessageBox.Show("-- Wrong Answer --\n" +
                                            "That command line contains parameters that don't make sense.");
                        break;
    
                    case 11:
                        if (rdoAnswer1.Checked == true)
                        {
                            MessageBox.Show("-- Wrong Answer --\n" +
                                            "The word \"where\" is a contextual keyword and " +
                                            "it has nothing to do with the \"else\" keyword.");
                        }
                        else if (rdoAnswer2.Checked == true)
                        {
                            MessageBox.Show("=-= Correct Answer =-=\n" +
                                            "The \"if\" and the \"else\" keywords are used in " +
                                            "conditional statements. #else is used as a preprocessor.");
                            Sum++;
                        }
                        else if (rdoAnswer3.Checked == true)
                        {
                            MessageBox.Show("-- Wrong Answer --\n" +
                                            "There are many differences between \"else\" and #else.");
                        }
                        else if (rdoAnswer4.Checked == true)
                        {
                            MessageBox.Show("-- Wrong Answer --\n" +
                                            "The \"else\" keyword has nothing to do with either how " +
                                            "a static method behaves or what it returns. The " +
                                            "params keyword has nothing to do with #else.");
                        }
                        break;
    
                    case 12:
                        if (rdoAnswer1.Checked == true)
                        {
                            MessageBox.Show("=-= Correct Answer=-=\n" +
                                            "Boxing is equivalent to casting a value to the Object class.");
                            Sum++;
                        }
                        else if (rdoAnswer2.Checked == true)
                        {
                            MessageBox.Show("-- Wrong Answer --\n" +
                                            "Although it can be used with the value of an element " +
                                            "of an array, there is no primary relationship between " +
                                            "boxing and arrays.");
                        }
                        else if (rdoAnswer3.Checked == true)
                        {
                            MessageBox.Show("-- Wrong Answer --\n" +
                                            "Boxing has nothing to do with generics and/or inheritance.");
                        }
                        else if (rdoAnswer1.Checked == true)
                        {
                            MessageBox.Show("-- Wrong Answer --\n" +
                                            "Boxing is not particularly related to value or reference types.");
                        }
                        else
                            MessageBox.Show("-- Wrong Answer --");
                        break;
    
                    case 13:
                        if ((chkAnswer1.Checked == true) &&
                            (chkAnswer2.Checked == true))
                        {
                            MessageBox.Show("=-= Correct Answer=-=\n" +
                                            "A. The internal keyword is used to control the " +
                                            "the accessibility of a member of a class " +
                                            "outside of its assembly\n" +
                                            "B. The fixed keyword is used to inform the compiler " +
                                            "that the garbage collector should not take the " +
                                            "responsibility of deleting a variable.");
                            Sum++;
                        }
                        else
                        {
                            MessageBox.Show("-- Wrong Answer --\n" +
                                            "C. select is a keyword in Microsoft Visual Basic but " +
                                            "not in C#\n" +
                                            "D. final may be a keyword in Java and J# but is not " +
                                            "in C#\n" +
                                            "E. asm is a keyword in some implementations of C++ " +
                                            "but it's not a keyword in C#.");
                        }
                        break;
    
                    case 14:
                        if (rdoAnswer1.Checked == true)
                        {
                            MessageBox.Show("=-= Correct Answer =-=\n" +
                                            "A read-only property defines only a \"get\" " +
                                            "accessor.");
                            Sum++;
                        }
                        else if (rdoAnswer2.Checked == true)
                        {
                            MessageBox.Show("=-= Wrong Answer =-=\n" +
                                            "If a property defines a \"set\" accessor, " +
                                            "then it cannot be considered read-only.");
                        }
                        else if (rdoAnswer3.Checked == true)
                        {
                            MessageBox.Show("=-= Wrong Answer =-=\n" +
                                            "There is no such a thing as a let accessor in C#.");
                        }
                        else if (rdoAnswer4.Checked == true)
                        {
                            MessageBox.Show("=-= Wrong Answer =-=\n" +
                                            "There is no such a thing as a met accessor in C#.");
                        }
                        else
                            MessageBox.Show("-- Wrong Answer --");
                        break;
    
                    case 15:
                        if (rdoAnswer1.Checked == true)
                        {
                            MessageBox.Show("=-= Wrong Answer =-=\n" +
                                            "Once a property defines a \"get\" accessor, it cannot " +
                                            "be considered a write-only property anymore.");
                        }
                        else if (rdoAnswer2.Checked == true)
                        {
                            MessageBox.Show("=-= Correct Answer =-=\n" +
                                            "That's right. If a property defines only a \"set\" accessor, " +
                                            "it is referred to as a write-only property.");
                            Sum++;
                        }
                        else if (rdoAnswer3.Checked == true)
                        {
                            MessageBox.Show("=-= Wrong Answer =-=\n" +
                                            "There is no such a thing as a let accessor in C#.");
                        }
                        else if (rdoAnswer4.Checked == true)
                        {
                            MessageBox.Show("=-= Wrong Answer =-=\n" +
                                            "There is no such a thing as a met accessor in C#.");
                        }
                        else
                            MessageBox.Show("-- Wrong Answer --");
                        break;
    
                    case 16:
                        if ((chkAnswer2.Checked == true) &&
                            (chkAnswer4.Checked == true) &&
                            (chkAnswer5.Checked == true))
                        {
                            MessageBox.Show("=-= Correct Answer =-=\n" +
                                            "Square brackets \"[]\" are used" +
                                            "\n\tdefine an array" +
                                            "\n\tto implement an indexer" +
                                            "\n\tto specify an attribute.");
                            Sum++;
                        }
                        else
                            MessageBox.Show("-- Wrong Answer --\n" +
                                            "A. Square brackets have nothing particular " +
                                            "to do with static methods.\n" +
                                            "C. Square brackets have no relationship with " +
                                            "exception handling.");
                        break;
    
                    case 17:
                        if ((chkAnswer2.Checked == true) &&
                            (chkAnswer4.Checked == true) &&
                            (chkAnswer5.Checked == true))
                        {
                            MessageBox.Show("=-= Correct Answer =-=\n" +
                                            "B. Like in Java and J#, but unlike C++ and Visual Basic, " +
                                            "in C#, every application must have at least one class.\n" +
                                            "D. All classes are based on the object class.\n" +
                                            "E. Unlike in C++, in C#, you cannot create a default " +
                                            "constructor structure.");
                            Sum++;
                        }
                        else
                            MessageBox.Show("-- Wrong Answer --\n" +
                                            "A: There is no built-in Value structure in C#.\n" +
                                            "C. Unlike in C++, in C#, a class or a structure " +
                                            "cannot be derived from a structure.");
                        break;
    
                    case 18:
                        if (rdoAnswer1.Checked == true)
                        {
                            MessageBox.Show("-- Wrong Answer --\n" +
                                            "That's the exact opposite.");
                        }
                        else if (rdoAnswer2.Checked == true)
                        {
                            MessageBox.Show("-- Wrong Answer --\n" +
                                            "Both keywords are used to pass an argument by " +
                                            "reference but there are differences between them.");
                        }
                        else if (rdoAnswer3.Checked == true)
                        {
                            MessageBox.Show("=-= Correct Answer =-=\n" +
                                            "That's true: you don't have to primarily " +
                                            "initialize an argument that would be passed with " +
                                            "with the out keyword but a variable passed as a " +
                                            "ref argument must be initialized prior to passing " +
                                            "it. In both cases, the argument is passed by " +
                                            "reference.");
                            Sum++;
                        }
                        else if (rdoAnswer4.Checked == true)
                        {
                            MessageBox.Show("-- Wrong Answer --\n" +
                                            "Passing an argument with the out or the ref " +
                                            "keywords has nothing to do with the method being " +
                                            "static or not.");
                        }
                        else
                            MessageBox.Show("-- Wrong Answer --");
                        break;
    
                    case 19:
                        if ((chkAnswer1.Checked == true) && (chkAnswer3.Checked == true))
                        {
                            MessageBox.Show("=-= Correct: A and D=-=\n" +
                                            "A. A structure is referred to as a value type and, " +
                                            "in fact, every structure in C# is derived from " +
                                            "the ValueType class.\n" +
                                            "C. If you create your own structure in a C# " +
                                            "application, you cannot define a default " +
                                            "constructor for your structure.");
                            Sum++;
                        }
                        else
                            MessageBox.Show("-- Wrong Answer --\n" +
                                            "B. There is no such a class named Value in the .NET " +
                                            "Framework. Still, the built-in classes in C# are " +
                                            "based on the Object class.\n" +
                                            "D. To the contrary, a class is passed by reference but " +
                                            "a structure is passed by value.\n" +
                                            "E. A class or a structure can be created as a generic type.");
                        break;
    
                    case 20:
                        if( (chkAnswer3.Checked == true) &&
                            (chkAnswer4.Checked == true) )
                        {
                            MessageBox.Show("=-= Right Answer =-=\n" +
                                            "C. To create an alias for a namespace or a type\n" +
                                            "D. To define a scope at the end of which an object would be deleted.");
                            Sum++;
                        }
                        else
                        {
                            MessageBox.Show("-- Wrong Answer --\n" +
                                            "A. The \"using\" keyword is not necessarily used with " +
                                            "a conditional statement.\n" +
                                            "B. The \"using\" keyword has nothing particular to " +
                                            "do with an indexed property.\n" +
                                            "E. The \"using\" keyword has nothing to do with the " +
                                            "if...else conditional statement.");
                        }
                        break;
    
                    case 21:
                        if (rdoAnswer1.Checked == true)
                        {
                            MessageBox.Show("--- Wrong Answer ---\n" +
                                            "To indicate that a member of an assembly that would be " +
                                            "accessed or referenced outside of the assembly, you " +
                                            "usually used the public keyword.");
                        }
                        else if (rdoAnswer2.Checked == true)
                        {
                            MessageBox.Show("--- Wrong Answer ---\n" +
                                            "The \"extern\" keyword has nothing to do with " +
                                            "generic classes.");
                        }
                        else if (rdoAnswer3.Checked == true)
                        {
                            MessageBox.Show("--- Wrong Answer ---\n" +
                                            "This \"extern keyword has nothing to do with " +
                                            "specifying whether a class is based on the " +
                                            "Object class or or the ValueType structure.");
                        }
                        else if (rdoAnswer4.Checked == true)
                        {
                            MessageBox.Show("=-= Right Answer =-=\n" +
                                            "The \"extern\" keyword is used to specify that " +
                                            "a method is implemented externally. For example, " +
                                            "you would use the \"extern\" keyword when " +
                                            "declaring a Win32 function to use in your C# application.");
                            Sum++;
                        }
                        else
                            MessageBox.Show("-- Wrong Answer --\n" +
                                            "");
                        break;
                }
    
                if (CurrentNumberOfQuestions == (TotalNumberOfQuestions + 1))
                {
                    btnQuit_Click(null, null);
                }
            }
    
            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!";
                MessageBox.Show("Total Number of Questions: " +
                                QuestionCounter.ToString() +
                                "\nNumber of Correct Answers: " +
                                Sum.ToString() +
                                "\nSuccess Rate:                   " +
                                (average * 100).ToString("F") +
                                " %\n" + strCongratulations);
                Close();
            }
    
    }
  19. Return to the form
  20. Double-click the Check Answer button and implement its event as follows:
     
    private void btnCheckAnswer_Click(object sender, EventArgs e)
            {
                CurrentNumberOfQuestions++;
                CheckAnswer(CurrentQuestion);
    
                CurrentQuestion = (int)(lstNumbers[QuestionCounter++]);
    
                if (CurrentQuestion == 0)
                {
                    lblQuestion.Visible = false;
                    txtQuestion.Visible = false;
                    grpMultipleChoice.Visible = false;
                    grpSingleChoice.Visible = false;
                    MessageBox.Show("No question yet!");
                }
                else
                {
                    PresentQuestion(CurrentQuestion);
                }
            }
  21. Return to the form and double-click an unoccupied area of its body
  22. Implement the event as follows:
     
    private void QuestionAnswer_Load(object sender, EventArgs e)
    {
        QuestionCounter = 0;
        CurrentNumberOfQuestions = 0;
    
        lstNumbers = new ArrayList();
                Random rndNumber = new Random();
    
                int number = rndNumber.Next(1, 22);
                lstNumbers.Add(number);
                int count = 0;
    
                do
                {
                    number = rndNumber.Next(1, 22);
    
                    if (!lstNumbers.Contains(number))
                    {
                        lstNumbers.Add(number);
                    }
    
                    count++;
                } while (count <= 100);
    
                btnCheckAnswer_Click(sender, e);
            }
  23. Display the first form
  24. Double-click the Start button and implement its event as follows:
     
    private void btnStart_Click(object sender, EventArgs e)
    {
                QuestionAnswer frmAnswer = new QuestionAnswer();
                int numberOfQuestion = int.Parse(nudQuestions.Value.ToString());
    
                frmAnswer.TotalNumberOfQuestions = numberOfQuestion;
                frmAnswer.ShowDialog();
    }
  25. Display the first form again
  26. Double-click the Close button and implement its event as follows:
     
    private void btnClose_Click(object sender, EventArgs e)
    {
        Close();
    }
  27. Execute the application to see the result
 
 

Home Copyright © 2006 FunctionX, Inc.