ProgrammingHardCognizant GenC Next · Infosys SP
What will be the output of the following pseudocode? (code(c) gives a character's code; 'A' to 'Z' have consecutive codes)
What will be the output of the following pseudocode? (code(c) gives a character's code; 'A' to 'Z' have consecutive codes)
String a = "AD", b = "BC"
Integer sa = 0, sb = 0
for (each i from 0 to 1)
sa = sa + code(a[i])
sb = sb + code(b[i])
end for
if (sa == sb)
Print "ANAGRAM"
else
Print "DIFFERENT"
end if- A. DIFFERENT
- B. ANAGRAM
- C. ANAGRAMDIFFERENT
- D. Nothing is printed
Correct answer: B. ANAGRAM
The test only compares the sums of the character codes. Since A + D = B + C (codes are consecutive: 65 + 68 = 66 + 67), it prints ANAGRAM even though the words are not anagrams. Equal sums are necessary but not sufficient.
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? (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…
- What will be the output of the following pseudocode? (integers are two's complement; ~ is bitwise NOT; >> is an arithmetic shift that…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division) Integer n = 19, steps = 0, s…