ProgrammingModerateTCS NQT · Accenture
Let x = 0. For i from 1 to 10: if i mod 3 == 0 then x = x + i, otherwise x = x - 1. What is printed?
Let x = 0. For i from 1 to 10: if i mod 3 == 0 then x = x + i, otherwise x = x - 1. What is printed?
x = 0
for i = 1 to 10
if i mod 3 == 0 then x = x + i else x = x - 1
end for
Print x- A. -25
- B. -4
- C. 11
- D. 18
Correct answer: C. 11
The running value goes -1, -2, 1, 0, -1, 5, 4, 3, 12, 11, so the answer is 11. Subtracting the multiples of 3 flips their sign, adding 1 instead of i loses the weight, and skipping the else branch ignores the decrements.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- A rope of length L is cut into two parts of lengths floor(L/2) and L - floor(L/2) whenever its length is 2 or more; the cost of a cut is…
- Jobs each take one unit of time and carry a deadline: 1, 1, 2, 3, 4, 4 (the last slot the job may use; slots are 1, 2, 3, ...). The…
- Events (start, end) are attended one per day, and an event may be attended on any day from start to end inclusive. Events: (1,2), (1,2)…
- Seven tasks with durations 7, 5, 4, 3, 2, 2, 1 are assigned to two identical machines. Tasks are taken in decreasing duration order, and…
- Array a = [2, 5, 3, 5]. For every pair of positions i < j, count the pairs where a[i] is at most a[j] (a[i] <= a[j]). What is the printed…
- Array a = [4, -2, 5, -1, 3]. Exactly k = 2 negations are applied: each one chooses the currently smallest element and replaces it by its…