ProgrammingHardAccenture · Cognizant pattern
What will be the output of the following pseudocode (capitalizing the first letter of each word, manually)?
What will be the output of the following pseudocode (capitalizing the first letter of each word, manually)?
String s = "hello world foo"
String result = ""
Boolean capitalizeNext = true
for (each ch in s)
if (capitalizeNext and ch != ' ')
result = result + toUpperCase(ch)
capitalizeNext = false
else
result = result + ch
end if
if (ch == ' ')
capitalizeNext = true
end if
end for
Print result- A. helloworldfoo
- B. Hello World Foo
- C. Hello Foo World
- D. World Hello Foo
Correct answer: B. Hello World Foo
The flag capitalizeNext is set after each space and consumed at the start of the next word, correctly capitalizing the first letter of every word: 'Hello World Foo'.
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 (maximum sum of any 4 consecutive elements, sliding window)? (Arrays are indexed from…
- What will be the output of the following pseudocode (reversing an array in groups of size k)? (Arrays are indexed from 0.) Integer arr[8]…
- What will be the output of the following pseudocode? (A square matrix's main diagonal is mat[i][i]; its anti-diagonal is mat[i][n-1-i]. If…
- This function uses a counter that PERSISTS across calls (a static/global variable), starting at 3. What does each of the 7 calls print, in…
- What will be the output of the following pseudocode? Function isEven(Integer n) if (n == 0) return true end if return isOdd(n - 1) end…
- What will be the output of the following pseudocode? Integer c = 0 for (each i from 1 to 12) for (each j from i + 1 to 12) if ((i + j) mod…