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
mainreadsq, the number of questions, with aScanner. If it is less than 1 or more than 20 it printsINVALID INPUTand the run stops.mainreads the keykwithnext()and creates the objectob = new Quiz(k). The constructor stores the key inkeyand setsnto its length.- If
nis not equal toq, orob.isValidKey()returnsfalse(a letter outside A to D, checked withcharAt),mainprintsINVALID INPUTand the run stops. mainreadsp; if it is less than 1 or more than 10 it printsINVALID INPUTand the run stops.- For each participant
mainreads one line of letters intoans[i]and callsob.isValid(ans[i]), which returnstrueonly when the length isnand every character is A to D, X or -. If it returnsfalse,mainprintsINVALID INPUTand the run stops. Lower-case letters are treated as invalid. - For each participant
mainthen finds:score(s): counts the positions wheres.charAt(i)equalskey.charAt(i);unattempted(s): counts the characters that areXor-;wrongAnswers(s): returnsn - unattempted(s) - score(s).
mainprints these counts, adds the score tosumand updateshigh, the index of the highest score.highchanges only when a score is strictly greater, so on a tie the first participant stays as the topper.- It prints the highest score, all participants who achieved it, the first of them, and the class average
sum / pto 2 decimal places usingprintf("%.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
| Variable | Data Type | Purpose |
|---|---|---|
key | String | Data member that stores the answer key |
n | int | Data member for the number of questions (length of the key) |
k | String | Parameter of the constructor and the key typed by the user in main |
s | String | Parameter of isValid(), score(), unattempted() and wrongAnswers(): the answers of one participant |
i | int | Loop counter for the character position or the participant |
ch | char | The key letter or answer letter being checked |
c | int | Counter in score() and unattempted() for correct or unattempted answers |
sc | Scanner | Reads the numbers, the key and the answers typed by the user |
q | int | Number of questions typed by the user |
ob | Quiz | Object of the class created in main |
p | int | Number of participants |
ans | String[] | Answers of every participant: ans[i] is participant i + 1 |
sum | int | Total of the scores of all participants |
high | int | Index of the first participant with the highest score |
marks | int[] | Score of every participant |
args | String[] | 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.00Sample 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.67Sample 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 INPUTSample 4
Enter the number of questions (n): 21
INVALID INPUTDry 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:
| i | s.charAt(i) | key.charAt(i) | Equal? | c |
|---|---|---|---|---|
| 0 | A | A | yes | 1 |
| 1 | B | B | yes | 2 |
| 2 | - | C | no | 2 |
| 3 | X | D | no | 2 |
| 4 | A | A | yes | 3 |
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:
| i | marks[i] | sum | marks[i] > marks[high] | high |
|---|---|---|---|---|
| 0 | 3 | 3 | 3 > 3 is false | 0 |
| 1 | 5 | 8 | 5 > 3 is true | 1 |
| 2 | 1 | 9 | 1 > 5 is false | 1 |
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
- 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."
- Rank list "Display the participants in descending order of their scores."
- Question-wise analysis "For every question, find how many participants answered it correctly."