CS FundamentalsHardGATE CS
This loop sums an array:
This loop sums an array:
i = 0; s = 0
while i < n:
s = s + a[i]
i = i + 1Which assertion is a loop invariant (true every time the condition i < n is tested) that, with the exit condition, proves s is the sum of all n elements?- A. s = a[0] + … + a[i] and 0 ≤ i < n
- B. s = a[0] + … + a[i − 1] and 0 ≤ i ≤ n
- C. s = a[0] + … + a[n − 1]
- D. i < n
Correct answer: B. s = a[0] + … + a[i − 1] and 0 ≤ i ≤ n
Initially i = 0 and the empty sum is 0; each iteration adds a[i] and then increments i, so s always holds the first i elements and 0 ≤ i ≤ n. At exit i = n, giving the full sum. Including a[i] is off by one, and i < n is false on exit.
Practise more Software Engineering, Git and Cloud Basics questionsSoftware Engineering, Git and Cloud Basics: notes and key points
More Software Engineering, Git and Cloud Basics questions
- Two identical servers run in parallel (the system works while at least one does). Each fails independently with an exponential failure…
- A team fits the Goel–Okumoto reliability-growth model μ(t) = a(1 − e^(−bt)) to its test data, with a = 120 expected total faults and b =…
- Why does the classic hardware 'bathtub' failure curve not fit software?
- A static analyser is described as SOUND for null-pointer dereferences. What does that guarantee?
- A three-way merge compares five separate regions of a file. Base / ours / theirs: R1 a / a / a; R2 b / B1 / b; R3 c / c / C2; R4 d / D1 /…
- A project uses Conventional Commits with automated semantic versioning. Since v1.4.2 the commits are 'fix(api): handle empty body'…