Home

Arrays and Delegates: Multiple-Choice Question Exercise

 

Description

An array of delegates is a technique of passing an array of methods, rather than an array of primitive types or an array of classes, to a method. To illustrate it, we create a short multiple-choice-question (MCQ) exercise. Each question is handled by a method of a class and that method returns a character. A delegate that returns a character is also created. To prepare the MCQ, we create an array of delegates where each element of the array is one of the methods that hold the questions. That is, each member of the array is meant to represent one of the methods. The method can then be called, through the members of the array, to ask a particular question, and display the answer.

 

Practical Learning Practical Learning: Using an Array of Delegates

  1. Start Microsoft Visual C# and create a new Console Application named MultipleChoiceQuestion1
  2. Change the Program.cs file as follows:
     
    using System;
    
    namespace MultipleChoiceQuestion1
    {
        public class Program
        {
            enum TMCQuestion { One, Two, Three, Four, Five };
            delegate char Question();
    
            static char Sequence()
            {
                char Answer;
    
                Console.WriteLine("Which sequence of numbers does not appear ");
                Console.WriteLine("to follow a recognizable order?");
                Console.WriteLine("(a)   3   9  27  33");
                Console.WriteLine("(b)   3   6   9  12");
                Console.WriteLine("(c)   2   4   6   8");
                Console.WriteLine("(d) 102 204 408 816");
                Console.Write("Your Answer? ");
                Answer = char.Parse(Console.ReadLine());
    
                return Answer;
            }
    
            static char Expression()
            {
                char Response;
    
                Console.WriteLine(
    		"Select the best expression to complete the empty space");
                Console.WriteLine(
    		"When ... drugs to a business address, traffickers often ");
                Console.WriteLine("omit a recipient name");
                Console.WriteLine("(a) to send");
                Console.WriteLine("(b) senders");
                Console.WriteLine("(c) sending");
                Console.WriteLine("(d) dealing");
                Console.Write("Your Answer? ");
                Response = char.Parse(Console.ReadLine());
    
                return Response;
            }
    
            static char Sentence()
            {
                char Answer;
    
                Console.WriteLine("Even ... there are 76,000 lawyers in that city, ");
                Console.WriteLine("it is still a small community");
                Console.WriteLine("(a) although");
                Console.WriteLine("(b) though");
                Console.WriteLine("(c) for");
                Console.WriteLine("(d) since");
                Console.Write("Your Answer? ");
                Answer = char.Parse(Console.ReadLine());
    
                return Answer;
            }
    
            static char WrongWord()
            {
                char Wrong;
    
                Console.WriteLine("Select the wrong word that would complete ");
                Console.WriteLine("the sentence");
                Console.WriteLine("For this type of business, revenue gains are ...");
                Console.WriteLine("(a) limited");
                Console.WriteLine("(b) scarce");
                Console.WriteLine("(c) limitless");
                Console.WriteLine("(d) claiming");
                Console.Write("Your Answer? ");
                Wrong = char.Parse(Console.ReadLine());
    
                return Wrong;
            }
    
            static char Right()
            {
                char Sentence;
    
                Console.WriteLine("Select the right sentence");
                Console.WriteLine("(a) The company is expecting to reducing inventory,");
                Console.WriteLine("    control cost, and efficiency improvement.");
                Console.WriteLine("(b) The company expects to reduce inventory,");
                Console.WriteLine("    control cost, and improve efficiency.");
                Console.WriteLine("(c) The company expects to reduce inventory,");
                Console.WriteLine("    control cost, and improving efficiency.");
                Console.WriteLine("(d) The company is expecting to reducing inventory,");
                Console.WriteLine("    controlling cost, and efficiency improvement.");
                Console.Write("Your Answer? ");
                Sentence = char.Parse(Console.ReadLine());
    
                return Sentence;
            }
    
            static int Main()
            {
                const int NumberOfQuestions = 5;
                char[] Answer = new char[NumberOfQuestions];
    
                Question[] MCQ = new Question[NumberOfQuestions];
    
                MCQ[0] = new Question(Sequence);
                MCQ[1] = new Question(Expression);
                MCQ[2] = new Question(Sentence);
                MCQ[3] = new Question(WrongWord);
                MCQ[4] = new Question(Right);
    
                for (int i = 0; i < NumberOfQuestions; i++)
                {
                    Console.WriteLine("Question {0}", i + 1);
                    Answer[i] = MCQ[i]();
                    ValidateAnswer(i + 1, Answer[i]);
                    Console.WriteLine();
                }
    
                return 0;
            }
    
            static void ValidateAnswer(int QstNbr, char Ans)
            {
                switch (QstNbr)
                {
                    case 1:
                        if (Ans == 'a' || Ans == 'A')
                            Console.WriteLine("Right Answer");
                        else
                        {
                            Console.WriteLine("Wrong Answer - The right answer was 'a'");
                            Console.WriteLine("(a) Starting at 3, 3*3=9 and 3*9=27");
                        Console.WriteLine("    There is no obvious way to determine 33");
                        }
                        break;
    
                    case 2:
                        if (Ans == 'c' || Ans == 'C')
                            Console.WriteLine("Right answer");
                        else
                            Console.WriteLine("Wrong Answer - The right answer was 'c'");
                        break;
    
                    case 3:
                        if (Ans == 'b' || Ans == 'B')
                            Console.WriteLine("Right answer");
                        else
                            Console.WriteLine("Wrong Answer - The right answer was 'b'");
                        break;
    
                    case 4:
                        if (Ans == 'd' || Ans == 'D')
                            Console.WriteLine("Right answer");
                        else
                            Console.WriteLine("Wrong Answer - The right answer was 'd'");
                        break;
    
                    case 5:
                        if (Ans == 'b' || Ans == 'B')
                            Console.WriteLine("Right answer");
                        else
                            Console.WriteLine("Wrong Answer - The right answer was 'b'");
                        break;
    
                    default:
                        Console.WriteLine("Invalid Answer");
                        break;
                }
            }
        }
    }
  3. Execute the application and test it. Here is an example:
     
    Question 1
    Which sequence of numbers does not appear
    to follow a recognizable order?
    (a)   3   9  27  33
    (b)   3   6   9  12
    (c)   2   4   6   8
    (d) 102 204 408 816
    Your Answer? a
    Right Answer
    
    Question 2
    Select the best expression to complete the empty space
    When ... drugs to a business address, traffickers often
    omit a recipient name
    (a) to send
    (b) senders
    (c) sending
    (d) dealing
    Your Answer? d
    Wrong Answer - The right answer was 'c'
    Question 3
    
    Even ... there are 76,000 lawyers in that city,
    it is still a small community
    (a) although
    (b) though
    (c) for
    (d) since
    Your Answer? b
    Right answer
    Question 4
    
    Select the wrong word that would complete
    the sentence
    For this type of business, revenue gains are ...
    (a) limited
    (b) scarce
    (c) limitless
    (d) claiming
    Your Answer? d
    Right answer
    Question 5
    
    Select the right sentence
    (a) The company is expecting to reducing inventory,
        control cost, and efficiency improvement.
    (b) The company expects to reduce inventory,
        control cost, and improve efficiency.
    (c) The company expects to reduce inventory,
        control cost, and improving efficiency.
    (d) The company is expecting to reducing inventory,
        controlling cost, and efficiency improvement.
    Your Answer? a
    Wrong Answer - The right answer was 'b'
    
    Press any key to continue . . .
  4. Close the DOS window
 

 


Previous Copyright © 2008-2016, FunctionX, Inc. Next