Prashnikaप्रश्निका
Code
About
‹ All Computer Science notes

ISC Practicals and Theory - Quiz Evaluation (OOP)

Updated 21 Oct 2026

Quiz evaluation means checking the answers of every participant against an answer key, giving each one a score and then finding the highest score and the class average. This version uses a parameterised constructor Quiz(String k) that stores the key, and small methods that each do one job and return a value (isValid(), score(), unattempted(), wrongAnswers()); main creates the object and loops over the participants.

This note uses a typical set of limits (up to 20 questions and up to 10 participants) and the scoring rule below. An ISC question gives its own limits and marking, which change only the constants and the scoring statement in the program.

Definition

The answer key is a string of $n$ letters, each from A to D, one for each question. The answers of a participant are also $n$ characters: A to D, or X or - when the question is not attempted. The answer to question $i$ is compared with the key letter at the same position.

Scoring rule used here: +1 for a correct answer, 0 for a wrong answer, 0 for an unattempted answer. There is no negative marking.

For example, with key ABCDA and answers AB-XA, questions 1, 2 and 5 match (3 correct), questions 3 and 4 are unattempted and none is wrong, so the score is 3.

Algorithm

  1. main reads q, the number of questions, with a Scanner. If it is less than 1 or more than 20 it prints INVALID INPUT and the run stops.
  2. main reads the key k with next() and creates the object ob = new Quiz(k). The constructor stores the key in key and sets n to its length.
  3. If n is not equal to q, or ob.isValidKey() returns false (a letter outside A to D, checked with charAt), main prints INVALID INPUT and the run stops.
  4. main reads p; if it is less than 1 or more than 10 it prints INVALID INPUT and the run stops.
  5. For each participant main reads one line of letters into ans[i] and calls ob.isValid(ans[i]), which returns true only when the length is n and every character is A to D, X or -. If it returns false, main prints INVALID INPUT and the run stops. Lower-case letters are treated as invalid.
  6. For each participant main then finds:
    • score(s): counts the positions where s.charAt(i) equals key.charAt(i);
    • unattempted(s): counts the characters that are X or -;
    • wrongAnswers(s): returns n - unattempted(s) - score(s).
  7. main prints these counts, adds the score to sum and updates high, the index of the highest score. high changes only when a score is strictly greater, so on a tie the first participant stays as the topper.
  8. It prints the highest score, all participants who achieved it, the first of them, and the class average sum / p to 2 decimal places using printf("%.2f").

Program

All the checking and counting is done by methods of the class that take the answers as a parameter and return a value. The number of correct answers is the score, so Score and Correct print the same number.

import java.util.Scanner;

/* Class Quiz holds the answer key of a quiz and has one-job methods to check
   a string of answers and to count its correct, wrong and unattempted answers. */
public class Quiz
{
    String key;          // answer key: one letter from A to D for each question
    int n;               // number of questions = length of the key

    // Parameterised constructor: stores the answer key and finds the number of questions
    // Parameter k: the answer key
    Quiz(String k)
    {
        key = k;
        n = k.length();
    }

    // Returns true if the key has only the letters A to D
    boolean isValidKey()
    {
        for (int i = 0; i < n; i++)
        {
            char ch = key.charAt(i);
            if (ch < 'A' || ch > 'D')
            {
                return false;                  // letter outside A to D
            }
        }
        return true;
    }

    // Returns true if s has n characters, each from A to D, X or -
    // Parameter s: the answers of one participant
    boolean isValid(String s)
    {
        if (s.length() != n)
        {
            return false;                      // wrong number of answers
        }
        for (int i = 0; i < n; i++)
        {
            char ch = s.charAt(i);
            if ((ch < 'A' || ch > 'D') && ch != 'X' && ch != '-')
            {
                return false;                  // invalid letter
            }
        }
        return true;
    }

    // Returns the score: +1 for each correct answer, 0 for a wrong or unattempted one
    // Parameter s: the answers of one participant (already checked by isValid)
    int score(String s)
    {
        int c = 0;                             // number of correct answers
        for (int i = 0; i < n; i++)
        {
            if (s.charAt(i) == key.charAt(i))
            {
                c++;
            }
        }
        return c;
    }

    // Returns the number of unattempted questions (X or -)
    // Parameter s: the answers of one participant
    int unattempted(String s)
    {
        int c = 0;                             // number of unattempted answers
        for (int i = 0; i < n; i++)
        {
            char ch = s.charAt(i);
            if (ch == 'X' || ch == '-')
            {
                c++;
            }
        }
        return c;
    }

    // Returns the number of wrong answers = questions - unattempted - correct
    // Parameter s: the answers of one participant
    int wrongAnswers(String s)
    {
        return n - unattempted(s) - score(s);
    }

    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);   // reads all the input
        System.out.print("Enter the number of questions (n): ");
        int q = sc.nextInt();                  // number of questions
        if (q < 1 || q > 20)
        {
            System.out.println("INVALID INPUT");
            return;                            // the run stops here
        }
        System.out.print("Enter the answer key (" + q + " letters from A to D): ");
        String k = sc.next();                  // answer key typed by the user
        Quiz ob = new Quiz(k);
        if (ob.n != q || !ob.isValidKey())
        {
            System.out.println("INVALID INPUT");
            return;
        }
        System.out.print("Enter the number of participants (p): ");
        int p = sc.nextInt();                  // number of participants
        if (p < 1 || p > 10)
        {
            System.out.println("INVALID INPUT");
            return;
        }
        String ans[] = new String[p];          // answers of every participant
        for (int i = 0; i < p; i++)
        {
            System.out.print("Enter the answers of participant " + (i + 1)
                + " (" + q + " characters, A to D, X or - if not attempted): ");
            ans[i] = sc.next();
            if (!ob.isValid(ans[i]))
            {
                System.out.println("INVALID INPUT");
                return;
            }
        }
        int sum = 0;                           // total of all scores
        int high = 0;                          // index of the first participant with the highest score
        int marks[] = new int[p];              // score of every participant
        System.out.println();
        for (int i = 0; i < p; i++)
        {
            marks[i] = ob.score(ans[i]);
            System.out.println("Participant " + (i + 1) + ": Score = " + marks[i]
                + ", Correct = " + marks[i] + ", Wrong = " + ob.wrongAnswers(ans[i])
                + ", Unattempted = " + ob.unattempted(ans[i]));
            sum = sum + marks[i];
            if (marks[i] > marks[high])
            {
                high = i;                      // strictly greater, so the first one stays on a tie
            }
        }
        System.out.println("Highest score: " + marks[high]);
        System.out.print("Achieved by participant(s):");
        for (int i = 0; i < p; i++)
        {
            if (marks[i] == marks[high])
            {
                System.out.print(" " + (i + 1));
            }
        }
        System.out.println();
        System.out.println("Topper (first one on a tie): participant " + (high + 1));
        System.out.printf("Class average: %.2f\n", (double) sum / p);
    }
}

Variable description table

VariableData TypePurpose
keyStringData member that stores the answer key
nintData member for the number of questions (length of the key)
kStringParameter of the constructor and the key typed by the user in main
sStringParameter of isValid(), score(), unattempted() and wrongAnswers(): the answers of one participant
iintLoop counter for the character position or the participant
chcharThe key letter or answer letter being checked
cintCounter in score() and unattempted() for correct or unattempted answers
scScannerReads the numbers, the key and the answers typed by the user
qintNumber of questions typed by the user
obQuizObject of the class created in main
pintNumber of participants
ansString[]Answers of every participant: ans[i] is participant i + 1
sumintTotal of the scores of all participants
highintIndex of the first participant with the highest score
marksint[]Score of every participant
argsString[]Command-line arguments of main (not used)

Sample input and output

Sample 1

Enter the number of questions (n): 5
Enter the answer key (5 letters from A to D): ABCDA
Enter the number of participants (p): 3
Enter the answers of participant 1 (5 characters, A to D, X or - if not attempted): AB-XA
Enter the answers of participant 2 (5 characters, A to D, X or - if not attempted): ABCDA
Enter the answers of participant 3 (5 characters, A to D, X or - if not attempted): BBBBB

Participant 1: Score = 3, Correct = 3, Wrong = 0, Unattempted = 2
Participant 2: Score = 5, Correct = 5, Wrong = 0, Unattempted = 0
Participant 3: Score = 1, Correct = 1, Wrong = 4, Unattempted = 0
Highest score: 5
Achieved by participant(s): 2
Topper (first one on a tie): participant 2
Class average: 3.00

Sample 2

Enter the number of questions (n): 3
Enter the answer key (3 letters from A to D): ABC
Enter the number of participants (p): 3
Enter the answers of participant 1 (3 characters, A to D, X or - if not attempted): ABX
Enter the answers of participant 2 (3 characters, A to D, X or - if not attempted): ABC
Enter the answers of participant 3 (3 characters, A to D, X or - if not attempted): ABC

Participant 1: Score = 2, Correct = 2, Wrong = 0, Unattempted = 1
Participant 2: Score = 3, Correct = 3, Wrong = 0, Unattempted = 0
Participant 3: Score = 3, Correct = 3, Wrong = 0, Unattempted = 0
Highest score: 3
Achieved by participant(s): 2 3
Topper (first one on a tie): participant 2
Class average: 2.67

Sample 3

Enter the number of questions (n): 3
Enter the answer key (3 letters from A to D): ABC
Enter the number of participants (p): 1
Enter the answers of participant 1 (3 characters, A to D, X or - if not attempted): AEC
INVALID INPUT

Sample 4

Enter the number of questions (n): 21
INVALID INPUT

One value per line

Dry run

Take the first sample: $q = 5$, key ABCDA, $p = 3$, answers AB-XA, ABCDA and BBBBB. The constructor sets n = 5. For participant 1 (s = "AB-XA"), score(s) works like this:

is.charAt(i)key.charAt(i)Equal?c
0AAyes1
1BByes2
2-Cno2
3XDno2
4AAyes3

So score(s) returns 3. unattempted(s) counts - and X and returns 2, and wrongAnswers(s) returns $5 - 2 - 3 = 0$. In the same way participant 2 gets score 5 and participant 3 (BBBBB) gets score 1 with 4 wrong. The loop in main then runs:

imarks[i]summarks[i] > marks[high]high
0333 > 3 is false0
1585 > 3 is true1
2191 > 5 is false1

The highest score is marks[1] = 5, achieved by participant 2 alone, and the class average is $9 \div 3 = 3.00$, as in the first sample run.

Variations

  1. Negative marking "Award 1 mark for a correct answer and deduct a fraction of a mark for a wrong one; no marks are deducted for an unattempted question. Display the final score of each participant."
  2. Rank list "Display the participants in descending order of their scores."
  3. Question-wise analysis "For every question, find how many participants answered it correctly."