ProgrammingHardInfosys · Accenture pattern
What will be the output of the following pseudocode? (Strings are indexed from 0; swap exchanges two characters.)
What will be the output of the following pseudocode? (Strings are indexed from 0; swap exchanges two characters.)
String s = "ABCDEF"
Integer n = length(s)
for (i = 0 to n / 2 - 1)
if (i mod 2 == 0)
swap(s[i], s[n - 1 - i])
end if
end for
Print s- A. FEDCBA
- B. FBCDEA
- C. FBDCEA
- D. AEDCBF
Correct answer: C. FBDCEA
i runs 0, 1, 2. i = 0 swaps A and F → FBCDEA; i = 1 is odd, skipped; i = 2 swaps C and D → FBDCEA. A full reversal (FEDCBA) would need the swap at i = 1 too; FBCDEA stops after the first swap.
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? Integer c = 0 for (i = 1 to 5) for (j = i to 5) if ((i + j) mod 3 == 0) c = c + 1 end…
- What will be the output of the following pseudocode? (/ is integer division; calls counts every call of f.) Integer calls = 0 Function…
- What will be the output of the following pseudocode? (Arrays are indexed from 0.) Integer a[5] = {1, 2, 3, 4, 5}, t for (i = 0 to 4) t =…
- What will be the output of the following pseudocode? (Arrays are indexed from 0.) Integer a[5] = {5, 1, 8, 2, 3}, c = 0 for (i = 0 to 3)…
- What will be the output of the following pseudocode? (A static variable is initialised once and keeps its value between calls; operands…
- What will be the output of the following pseudocode? (^ is bitwise XOR, & is AND, >> is a right shift.) Integer x = 29, y = 30 Integer gx…