ProgrammingHardTCS NQT · Accenture pattern
What will be the output of the following pseudocode? (Arrays are indexed from 0.)
What will be the output of the following pseudocode? (Arrays are indexed from 0.)
Integer arr[6] = {1, 2, 3, 4, 5, 6}
for (each i from 0 to 5)
swap(arr[i], arr[5 - i])
end for
Print arr[0], arr[5]- A. 3 4
- B. 1 6
- C. 6 1
- D. 4 3
Correct answer: B. 1 6
The loop runs over the WHOLE array. The first three swaps reverse it, and the last three swap the same pairs back, so it ends up unchanged: 1 and 6. Stopping halfway (i from 0 to 2) would reverse it.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- How many times is fun called in total when fun(6) is evaluated? Function fun(Integer n) if (n <= 2) return 1 end if return fun(n - 1) +…
- What will be the output of the following pseudocode? Integer c = 0, j for (each i from 1 to 10) j = i while (j <= 10) c = c + 1 j = j + i…
- What will be the output of the following pseudocode? (& is bitwise AND, | is bitwise OR, ^ is bitwise XOR, << and >> are left and right…
- What will be the output of the following pseudocode? (& is bitwise AND, | is bitwise OR, ^ is bitwise XOR, << and >> are left and right…
- How many numbers are printed when fun(5) is called? Function fun(Integer n) if (n <= 0) return end if fun(n - 1) Print n fun(n - 2) End…
- What will be the output? (A static variable keeps its value between calls and is initialised only once. The two calls in Print are…