ProgrammingHardAmazon OA · Cognizant GenC Next
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[7] = {1, 2, 3, 4, 5, 6, 7}
reverse(arr, 0, 2) // reverses arr[0..2]
reverse(arr, 3, 6)
reverse(arr, 0, 6)
Print arr[0], arr[3], arr[6]- A. 4 7 3
- B. 5 1 4
- C. 7 4 1
- D. 3 6 2
Correct answer: A. 4 7 3
Reversing the first 3, then the last 4, then the whole array is the reversal trick for a LEFT rotation by 3: {4, 5, 6, 7, 1, 2, 3}. So arr[0], arr[3], arr[6] = 4 7 3. 5 1 4 is a right rotation by 3.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- What will be the output of the following pseudocode? (the static s keeps its value between calls; in f(n - 1) + s the call is evaluated…
- What will be the output of the following pseudocode? Integer c = 0 for (each i from 1 to 6) for (each j from 1 to 6) if ((i j) mod 4 == 0)…
- What will be the output of the following pseudocode? (Strings are indexed from 0; + joins strings) String s = "AAABCCDAA", r = "" Integer…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division) Function f(Integer n) if (n ==…
- What will be the output of the following pseudocode? (ref means the argument is passed by reference; other parameters are copies)…
- What will be the output of the following pseudocode? (/ is integer division) Stack st = empty for each token t in "6 2 3 + - 3 8 2 / + "…