ProgrammingModerateAccenture · Cognizant GenC Next
What will be the output of the following pseudocode?
What will be the output of the following pseudocode?
Function isSub(String a, String b) // is b a subsequence of a?
Integer j = 0
for (each i from 0 to length(a) - 1)
if (j < length(b) AND a[i] == b[j]) j = j + 1
end for
return j == length(b)
End Function
Print isSub("ABCDE", "ACE"), isSub("ABCDE", "AEC"), isSub("AXBXC", "ABC")- A. true false true
- B. true true true
- C. true false false
- D. false false true
Correct answer: A. true false true
A greedy single pass matches b's letters in order. ACE appears in order in ABCDE (true); AEC does not, since C comes before E (false); A, B, C appear in order in AXBXC with gaps (true). Output true false true.
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? String s = "ABBACA" Stack st = empty for (each ch in s) if (st not empty AND top(st)…
- What will be the output of the following pseudocode? Integer a[6] = {1, 2, 3, 4, 5, 6} Integer i = 0, j = 5 while (i < j) if (a[i] mod 2…
- What will be the output of the following pseudocode? Integer a[5] = {0, 1, 0, 3, 12}, k = 0 for (each i from 0 to 4) if (a[i] != 0)…
- What will be the output of the following pseudocode? (Arrays are indexed from 0) Integer a[6] = {-1, 0, 1, 2, -1, -4} sort a // {-4, -1…
- What will be the output of the following pseudocode? Integer a[3] = {5, -3, 5} Integer total = 0, curMax = 0, bestMax = a[0], curMin = 0…
- What will be the output of the following pseudocode? Integer n = 5, spaces = 0, stars = 0 for (each i from 1 to n) for (each j from 1 to n…