DBMS Interview Questions – Part 1
Calculate a student's score on a 20-question quiz with positive marks for correct answers and negative marks for incorrect ones.
Understand the Problem
Problem Statement
You are tasked with creating an automated grader for a 20-question multiple-choice quiz on Database Management Systems (DBMS). The quiz has a specific scoring rule:
- For each correct answer, a student gets 4 points.
- For each incorrect answer, a student loses 1 point (i.e., gets -1 point).
- If a question is not attempted (represented by the character 'N'), it gets 0 points.
You will be given a string of 20 characters representing a student's answers. The correct answers for the 20 questions are fixed. Your program must calculate and print the total score based on the provided answers.
Constraints
- The input will be a single string of exactly 20 characters.
- Each character in the input string will be one of the uppercase letters: 'A', 'B', 'C', 'D', or 'N'.
- The number of questions is always 20.
Examples
CBACACBCBCCCBCDACCDA80The student answered all 20 questions correctly. Each correct answer is worth 4 points. Therefore, the total score is 20 * 4 = 80.
BBBBBBBBBBBBBBBBBBBB5The student answered 5 questions correctly (questions 2, 7, 9, 12, and 14). This gives 5 * 4 = 20 points. The remaining 15 questions were answered incorrectly. This gives 15 * (-1) = -15 points. The total score is 20 - 15 = 5.
CBANACBCBCCCBCNACCDA72The student answered 18 questions correctly, earning 18 * 4 = 72 points. Two questions (4 and 16) were not attempted ('N'), earning 0 points for each. There were no incorrect answers. The total score is 72 + 0 = 72.
Solution
#include <stdio.h>
#include <string.h>
int main() {
char correct_answers[] = "CBACACBCBCCCBCDACCDA";
char student_answers[21];
int score = 0;
// Read the student's answers
scanf("%20s", student_answers);
for (int i = 0; i < 20; i++) {
if (student_answers[i] == 'N') {
// No points for not attempted questions
continue;
} else if (student_answers[i] == correct_answers[i]) {
// +4 for a correct answer
score += 4;
} else {
// -1 for an incorrect answer
score -= 1;
}
}
printf("%d\n", score);
return 0;
}The C solution implements the grading logic using basic string and loop operations.
- Includes: We include
stdio.hfor input/output functions (scanf,printf) andstring.hthough it's not strictly necessary for this logic but good practice for string manipulations. - Data Storage: A character array
correct_answersis initialized with the solution key. Another arraystudent_answersof size 21 (20 for answers + 1 for null terminator) is declared to store user input. An integerscoreis initialized to 0. - Input:
scanf("%20s", student_answers)reads exactly 20 characters from the standard input into thestudent_answersarray. - Processing Loop: A
forloop iterates 20 times, fromi = 0to19. - Scoring Logic: Inside the loop, it first checks if the answer is
'N'. If so, it usescontinueto skip to the next iteration. Otherwise, it checks if the student's answer matches the correct one. If they match, 4 is added toscore. If they don't, 1 is subtracted. - Output: Finally,
printf("%d\n", score)prints the final calculated score followed by a newline.