Given `int v[6] = {10, 20, 30, 40, 50, 60};`, what is `v[2] + v[5]`?
- A) 70
- B) 90
- C) 80
- D) 100
Answer & Explanation: B) 90 — C++ indexing starts at 0: `v[2]=30` and `v[5]=60`, so `30 + 60 = 90`.
High-school CS prep paper (100 pts): 0-based C++ array bounds (`0..n-1`), flag variable verification (`ok = 1`), adjacent pair loop limits `i < n - 1`, and binary search `O(log₂ n)` step bounds.
Theory Snapshot: `int v[5]` has valid indices `0, 1, 2, 3, 4` (never touch `v[5]`). Comparing `v[i]` with `v[i+1]` requires `i < n - 1`. Binary search requires a sorted array and runs in O(log₂ n) comparisons.
Answer & Explanation: B) 90 — C++ indexing starts at 0: `v[2]=30` and `v[5]=60`, so `30 + 60 = 90`.
Answer & Explanation: C) 10–11 steps (since 2¹⁰ = 1,024) — Since `2^10 = 1,024`, binary search halves the range in only 10–11 steps (`O(log₂ n)`).
int a[4] = {5, 12, 8, 20};
int m = a[0];
for (int i = 1; i < 4; i++) {
if (a[i] > m) m = a[i];
}
cout << m;
Answer & Explanation: 20 — `m` updates 5 → 12 → 12 → 20, printing the array maximum `20`.
Answer & Explanation: B) False — False! Binary Search strictly requires a sorted array so it knows which half to discard.
Answer & Explanation: n-1 — If `i` reached `n - 1`, `v[i + 1]` would read out-of-bounds `v[n]`. Stop strictly at `i < n - 1`.
| Section | Evaluated Competency | Points |
|---|---|---|
| Part I (2 × 18 pts) | 0-based indexing & O(log₂ n) binary search | 36p |
| Part II (2 × 18 pts) | Array maximum trace & sorted precondition | 36p |
| Part III (18 pts) | Preventing out-of-bounds `v[i+1]` with `i < n - 1` | 18p |
| Ex officio | Baseline points | 10p |
| TOTAL (Grade = Score / 10) | 100p | |