CodeBlanks Logo Icon CodeBlanks

Quiz model

C# course · Intermediate · Lesson 42/116

Design the skeleton of Question and Quiz classes.

Project: Quiz application

In this 5-lesson project you will build a Quiz application. Each lesson adds one piece, and by the final lesson you will have a working app. You will use what you learned in previous groups: classes, static members and readonly.

The first lesson defines the Question class: it holds the question text (Text) and the expected answer (Answer). The next lessons expand this model with a Quiz class to add questions, play them and compute the score.

The Question class

A Question object represents a single question. Two fields are enough: public string Text; holds the question text, public string Answer; holds the expected answer.

Because the fields are public, they are accessible directly: q.Text, q.Answer. They can also be set with an object initializer: new Question { Text = "Capital?", Answer = "Ankara" }. The next lessons build on this simple model.

Exercise Define the Quiz model

Define the Question class with public string Text; and public string Answer; fields. In Main, call Console.WriteLine("Quiz ready");.

Show example solution
Define Question as `class Question { public string Text; public string Answer; }`. In Main write `Console.WriteLine("Quiz ready");`.
Example solution:
using System;

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

class Program
{
    static void Main()
    {
        Console.WriteLine("Quiz ready");
    }
}

Test yourself

  1. Which fields should the Question class hold?

    • Id and CreatedAt
    • Text and Score
    • Text and Answer
    • Title and Author
    Show answer

    Text and Answer — Correct. `Text` holds the question text, `Answer` the expected answer.

  2. Fill in the blank: Question field

    class Question
    {
        public ____;
        public string Answer;
    }
    Show answer
    class Question
    {
        public string Text;
        public string Answer;
    }
  3. Match: Question concepts

    Text
    question text
    Answer
    expected answer
    public
    openly accessible
    new Question
    creates a new question

All lessons in Intermediate