Marks Array with 40 Students
The school's result program stores marks of 40 students whose roll numbers are 1 to 40. It declares int marks[40]; and stores each mark with marks[roll] = mark;. It then computes the total with for (i = 1; i <= 40; i++) sum = sum + marks[i]; and finds the highest mark with a similar loop. Absent students are entered as 0. Names are stored in a separate string array; a search compares a typed name with name[i] using == in Java-style pseudocode. The teacher reports three symptoms: the total is wrong, the program crashes when it reaches roll 40, and a search for “Rina” fails while “rina” is found. The teacher wants a corrected array design and a check for absentees before the highest mark is reported.
1. Explain the array indexing fault. Why does the program crash at roll 40, and why is the total wrong even before that? Give a corrected declaration and loop.
Read the worked answer
In a declaration int marks[40], the valid indices are 0 to 39. The program writes marks[40] for roll 40, which is outside the array; that is the crash (or silent corruption in some languages). The loop i <= 40 also reads marks[40], repeating the out-of-bounds access.
The total is wrong even before the crash because marks[0] is never used by the roll-number scheme. Roll 1 is stored at marks[1], roll 39 at marks[39], and roll 40 is out of range. The sum loop starts at 1, so it does not add a mark at index 0, but the deeper problem is that the array has only 40 slots for roll numbers 1–40, which need 41 slots if index 0 is ignored.
Corrected option A, one-based storage: int[] marks = new int[41]; for (int roll = 1; roll <= 40; roll++) marks[roll] = input; for (int i = 1; i <= 40; i++) sum += marks[i];
Corrected option B, zero-based storage: int[] marks = new int[40]; marks[roll - 1] = mark; for (int i = 0; i < 40; i++) sum += marks[i];
Either is acceptable if used consistently; mixing the two schemes is what caused the fault.
2. How should absent students be represented so that the highest mark is not distorted? Rewrite the highest-mark logic to handle absence correctly.
Read the worked answer
Using 0 for absent is ambiguous: a student who genuinely scored 0 and a student who did not sit the paper look the same. It also distorts an average if absences are included as zero. A separate boolean array is clearer: boolean[] absent = new boolean[41]; int[] marks = new int[41];
Alternatively, use a sentinel such as -1 for absent, because a valid mark cannot be negative. The highest-mark logic should skip absent entries and should also handle the case where every student is absent:
int highest = -1; for (int i = 1; i <= 40; i++) { if (!absent[i] && marks[i] > highest) { highest = marks[i]; } } if (highest == -1) print("No student appeared"); else print(highest);
If the school insists on 0 for absent, the program must at least keep a separate absence flag; otherwise the highest mark may be reported as 0 when all present students scored 0, or an absent student may be counted incorrectly in the total.
3. Why does the name search fail for “Rina” but succeed for “rina”? Explain the correction in Java-style pseudocode.
Read the worked answer
In Java, == on strings compares object references, not the characters. Two strings with the same letters may be different objects, so == can return false even when the text matches. The search should use .equals() or .equalsIgnoreCase().
The second symptom, “Rina” failing while “rina” succeeds, suggests the stored value may be “rina” and the comparison is case-sensitive. Using .equals() would still fail for “Rina” against “rina”; .equalsIgnoreCase() handles the case difference. If exact case matters, the program should say so and ask the user to match it, but for a school name search, case-insensitive matching is usually better.
Pseudocode correction: for (int i = 0; i < names.length; i++) { if (names[i] != null && names[i].equalsIgnoreCase(typedName)) { found = i; break; } }
For reliable matching, also trim leading and trailing spaces from the typed name before comparing.