Grade Program at the Boundary
A school's Class X result program stores the total of five subjects (maximum 500) in an integer variable total. It computes int p = total / 5; and then uses: if (p > 60) grade = "B2"; else if (p >= 30) grade = "C"; else grade = "D". The result policy says B2 requires an aggregate of at least 60%, C requires at least 30%, and a candidate must also have at least 30% in every subject. Three students are discussed. Rina has total 302 and passed every subject. Samir has total 305 and passed every subject. Joy has total 299 and failed one subject with 28 marks. The teacher notices that Rina received C while Samir received B2, and Joy also received C. The head teacher asks for a dry run of the existing code and a corrected decision structure, not merely a changed symbol.
1. Dry-run the existing code for Rina, Samir and Joy. Explain why Rina and Samir receive different grades, and whether Joy's grade is correct under the policy.
Read the worked answer
Rina: total = 302; integer division 302 / 5 = 60 (the .4 is discarded). Test p > 60 is false, so the code falls to p >= 30 and gives C. Rina's true aggregate is 302 / 500 = 60.4%, which meets “at least 60%”, so B2 is the policy grade.
Samir: total = 305; 305 / 5 = 61. Test 61 > 60 is true, so the code gives B2. This is correct, but only because truncation still left a value above 60.
Joy: total = 299; 299 / 5 = 59. Test 59 > 60 is false; 59 >= 30 is true, so the code gives C. This is wrong because Joy scored 28 in one subject. The policy requires at least 30% in every subject, so Joy must be marked Fail (or D under the school's label) regardless of the aggregate.
The difference between Rina and Samir is caused by integer truncation plus the strict > test: totals from 300 to 304 all truncate to 60 and are wrongly denied B2.
2. Rewrite the decision logic correctly. Your answer should address both the percentage calculation and the subject-pass requirement.
Read the worked answer
Two separate faults must be fixed.
Do not truncate the aggregate. Use real division, or compare the total directly: double p = total / 5.0; Or: if (total >= 300) ... for the 60% boundary.
Check the subject-pass condition before awarding a pass grade. A candidate with any subject mark below 30 is Fail.
A corrected structure in pseudocode:
if (any subject mark < 30) grade = "Fail" else if (total >= 450) // 90% grade = "A1" else if (total >= 400) // 80% grade = "A2" else if (total >= 350) // 70% grade = "B1" else if (total >= 300) // 60% grade = "B2" else if (total >= 150) // 30% grade = "C" else grade = "D"
Using total >= 300 avoids floating-point comparison and makes the 60% boundary exact. The important point is that the subject-fail check is not an afterthought; it overrides the aggregate.
3. Suggest a small set of test cases that would have exposed the faults. Explain what each case is testing.
Read the worked answer
Test at the boundary and just below it:
- Total 299, all subjects passed: should be C, not B2. Tests the lower side of the 60% boundary.
- Total 300, all subjects passed: should be B2. Tests the exact boundary and exposes the strict > test.
- Total 302, all subjects passed: should be B2. Tests integer truncation between 300 and 304.
- Total 305, all subjects passed: should be B2. Confirms the code still works above the boundary.
- Total 400, one subject 29: should be Fail, not A2. Tests that the subject-pass rule overrides a high aggregate.
- Total 149, all subjects passed: should be D; total 150 should be C. Tests the 30% aggregate boundary.
A good test set includes exact boundaries, values just below them, and a case where two rules conflict. Testing only “average” students such as total 250 would not reveal either fault.