CodeBlanks Logo Icon CodeBlanks

Running the quiz

C# course · Intermediate · Lesson 44/116

Learn to ask questions in turn with the Run method.

Playing the quiz

In this lesson we add a Run method to the Quiz class. This method takes a string[] of answers, iterates each question, compares the answer and prints correct/wrong.

The loop for (int i = 0; i < questions.Count; i++) walks each question. questions[i].Text gives the question text, questions[i].Answer the expected answer. The comparison answers[i] == questions[i].Answer checks the answer. If correct, print "Correct"; otherwise print "Wrong".

Array parameter

The Run method takes a string[] parameter; this array holds one answer per question. Call: quiz.Run(new[] { "Ankara" }).

new[] { ... } creates a new array. The array length must match the question count; otherwise answers[i] may fail. In this lesson the answers are sent from code; in a real app you could read them with Console.ReadLine().

Exercise Play the quiz

Add a public void Run(string[] answers) method to Quiz. In its body use a for loop to walk the questions; print questions[i].Text, then if answers[i] == questions[i].Answer print "Correct", otherwise "Wrong". In Main, add one question and call quiz.Run(new[] { "Ankara" }).

Show example solution
Add `public void Run(string[] answers) { for (int i = 0; i < questions.Count; i++) { Console.WriteLine(questions[i].Text); if (answers[i] == questions[i].Answer) { Console.WriteLine("Correct"); } else { Console.WriteLine("Wrong"); } } }` to Quiz. In Main write `var quiz = new Quiz(); quiz.AddQuestion(new Question { Text = "Capital?", Answer = "Ankara" }); quiz.Run(new[] { "Ankara" });`.
Example solution:
using System;
using System.Collections.Generic;

class Question
{
    public string Text;
    public string Answer;
}

class Quiz
{
    private List<Question> questions = new List<Question>();

    public void AddQuestion(Question q)
    {
        questions.Add(q);
    }

    public int Count()
    {
        return questions.Count;
    }

    public void Run(string[] answers)
    {
        for (int i = 0; i < questions.Count; i++)
        {
            Console.WriteLine(questions[i].Text);
            if (answers[i] == questions[i].Answer)
            {
                Console.WriteLine("Correct");
            }
            else
            {
                Console.WriteLine("Wrong");
            }
        }
    }
}

class Program
{
    static void Main()
    {
        var quiz = new Quiz();
        quiz.AddQuestion(new Question { Text = "Capital?", Answer = "Ankara" });
        quiz.Run(new[] { "Ankara" });
    }
}

Test yourself

  1. Match: Run method parts

    for
    question loop
    answers[i]
    student answer
    questions[i]
    question record
    ==
    comparison
  2. What should the Run method do?

    • Show only the first question.
    • Print nothing.
    • Shuffle the questions.
    • Iterate the questions, compare answers and print correct/wrong.
    Show answer

    Iterate the questions, compare answers and print correct/wrong. — Correct. Run walks all questions and compares answers.

  3. Fill in the blank: for loop

    for (____; i < questions.Count; i++)
    Show answer
    for (int i = 0; i < questions.Count; i++)

All lessons in Intermediate