ProgrammingVery hardLTIMindtree · Cognizant GenC
What will be the output of the following pseudocode?
What will be the output of the following pseudocode?
String s = "{[()]}(()"
Stack st
Integer pops = 0, i
Boolean ok = true
for (i = 0; i < length(s); i = i + 1)
if (s[i] is an opening bracket)
push s[i]
else if (st not empty AND top(st) matches s[i])
pop
pops = pops + 1
else
ok = false
break
end if
end for
if (ok AND st is empty)
Print "YES", pops, size(st)
else
Print "NO", pops, size(st)
end if- A. YES 4 0
- B. NO 3 1
- C. NO 4 1
- D. NO 4 0
Correct answer: C. NO 4 1
The first six characters match fully (3 pops); then '(' '(' ')' pop one more, leaving one '(' unmatched: 4 pops and 1 element left, so the result is NO 4 1.
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[5] = {2, 5, 9, 14, 20}, i for (i = 1; i < 5; i = i + 1) a[i] = a[i] - a[i…
- What will be the output of the following pseudocode? Integer x = 0, i for (i = 1; i <= 10; i = i + 1) x = ((x << 1) ^ i) & 15 end for…
- What will be the output of the following pseudocode? Integer w[5][5], i, j for (i = 0; i < 5; i = i + 1) for (j = 0; j < 5; j = j + 1) if…
- What will be the output of the following pseudocode? String A = "AXYT", B = "AYZX" Integer L[5][5] = all 0, i, j for (i = 1; i <= 4; i = i…
- What will be the output of the following pseudocode? Integer n = 9, steps = 0 while (n != 1) if (n mod 2 == 0) n = n / 2 else n = 3 n + 1…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division.) Integer x, y Set x = 17, y =…