ProgrammingModerateAccenture · Capgemini
What will be the output of the following pseudocode?
What will be the output of the following pseudocode?
String s = " the quick brown fox "
Integer words = 0
Boolean inWord = false
for (each i from 0 to length(s) - 1)
if (s[i] == ' ')
inWord = false
else if (not inWord)
inWord = true
words = words + 1
end if
end for
Print words- A. 9
- B. 3
- C. 4
- D. 5
Correct answer: C. 4
A word is counted only at its first letter (a non-space after a space or at the start), so repeated spaces do not matter: the, quick, brown, fox = 4. Counting spaces + 1 would give a wrong answer.
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 val[7] = {1000, 500, 100, 50, 10, 5, 1} String sym[7] = {"M", "D", "C", "L"…
- What will be the output of the following pseudocode? (code(c) gives a character's code; 'A' to 'Z' have consecutive codes) String a =…
- What will be the output of the following pseudocode? (Strings are indexed from 0) String s = "BANANAS" Integer best = 1, l, r, n = 7 for…
- What will be the output of the following pseudocode? (& is AND, | is OR, << and >> are shifts; 15 = 0000 1111 and 240 = 1111 0000) Integer…
- What will be the output of the following pseudocode? Integer a[7] = {4, 1, 2, 1, 2, 5, 4} Integer x = 0 for (each i from 0 to 6) x = x ^…
- What will be the output of the following pseudocode? Integer a = 29, b = 15, x, c = 0 x = a ^ b while (x > 0) c = c + (x & 1) x = x >> 1…