ProgrammingVery hardCognizant GenC Next · TCS Digital
What will be the output of the following pseudocode? (code(c) is the ASCII code: 'A' = 65, 'a' = 97)
What will be the output of the following pseudocode? (code(c) is the ASCII code: 'A' = 65, 'a' = 97)
Function cmp(String a, String b)
Integer i = 0
while (i < length(a) AND i < length(b))
if (a[i] != b[i])
return code(a[i]) - code(b[i])
end if
i = i + 1
end while
return length(a) - length(b)
End Function
Print cmp("apple", "app"), cmp("apply", "apple"), cmp("Zoo", "apple")- A. 0 20 -7
- B. 2 4 -7
- C. 2 20 -7
- D. 2 20 25
Correct answer: C. 2 20 -7
apple extends app, so the length difference 2 is returned. apply vs apple first differ at y vs e: 121 − 101 = 20. 'Z' (90) vs 'a' (97) gives −7: upper-case letters come before lower-case ones. Output 2 20 -7.
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 a[8] = {3, 1, 4, 1, 5, 9, 2, 6} Integer best = a[0], c = 1 for (each i from 1…
- What will be the output of the following pseudocode? Integer a[6] = {100, 4, 200, 1, 3, 2} Set s = all values of a Integer best = 0, len…
- What will be the output of the following pseudocode? (Arrays are indexed from 0) Integer m[4][4] = {{1, 4, 7, 11}, {2, 5, 8, 12}, {3, 6…
- What will be the output of the following pseudocode? Integer a[7] = {4, 5, 2, 10, 8, 6, 9} Stack st = empty for (each i from 0 to 6) while…
- What will be the output of the following pseudocode? (+ joins strings) Queue q = {"1"} Integer k = 0 String s while (k < 6) s = dequeue(q)…
- What will be the output of the following pseudocode? Integer a[6] = {1, 2, 1, 3, 2, 1} Map cnt = empty Integer pairs = 0 for (each i from…