Question 51
MediumWhat will be printed by the following pseudocode?
Integer a[4] = {1, 2, 3, 4}
Integer b[4], i
b[0] = 1
for (each i from 1 to 3)
b[i] = b[i - 1] * a[i - 1]
end for
Print b[3]
Popular companies
Prepare company by company
Hiring pattern, syllabus, past questions and mocks for each company.
All companiesPractice by subject
Pseudocode and Output Prediction
What will be printed by the following pseudocode?
Integer a[4] = {1, 2, 3, 4}
Integer b[4], i
b[0] = 1
for (each i from 1 to 3)
b[i] = b[i - 1] * a[i - 1]
end for
Print b[3]
4
2
24
6
Sign up free to see the answer and a step-by-step explanation.
What is the output of this pseudocode?
Integer a[5] = {9, 4, 7, 2, 8}
Integer i, cnt = 0
for (each i from 1 to 3)
if (a[i] < a[i - 1] and a[i] < a[i + 1])
cnt = cnt + 1
end if
end for
Print cnt
2
0
1
3
Sign up free to see the answer and a step-by-step explanation.
What will be printed by the following pseudocode?
Integer a[5] = {1, 2, 3, 4, 5}
Integer i
for (each i from 0 to 4)
a[i] = a[4 - i]
end for
Print a[0], a[1], a[3], a[4]
5 4 3 2
5 4 4 5
1 2 4 5
5 4 2 1
Sign up free to see the answer and a step-by-step explanation.
What is the output of this pseudocode?
Integer a[6] = {1, 4, 2, 5, 3, 6}
Integer even = 0, odd = 0, i
for (each i from 0 to 5)
if (a[i] mod 2 == 0)
even = even + a[i]
else
odd = odd + a[i]
end if
end for
Print even - odd
3
21
-3
12
Sign up free to see the answer and a step-by-step explanation.
What will be the output of the following pseudocode?
Integer a[] = {0, 1, 0, 3, 12}
Integer k = 0
for (each i from 0 to 4)
if (a[i] != 0)
a[k] = a[i]
k = k + 1
end if
end for
for (each i from k to 4)
a[i] = 0
end for
Print a[0], a[1], a[2], a[3], a[4]
1 3 12 0 0
12 3 1 0 0
1 0 3 12 0
0 0 1 3 12
Sign up free to see the answer and a step-by-step explanation.
What will be the output of the following pseudocode?
Integer m[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}
Integer s = 0
for (each i from 0 to 2)
s = s + m[i][2 - i]
end for
Print s
15
12
18
9
Sign up free to see the answer and a step-by-step explanation.
What will be the output of the following pseudocode?
Integer a[] = {-2, 1, -3, 4, -1, 2, 1, -5, 4}
Integer best = a[0], cur = a[0]
for (each i from 1 to 8)
cur = max(a[i], cur + a[i])
best = max(best, cur)
end for
Print best
6
4
5
7
Sign up free to see the answer and a step-by-step explanation.
What will be the output of the following pseudocode?
Integer a[] = {1, 3, 5, 7, 9, 11}
Integer lo = 0, hi = 5, steps = 0, mid
while (lo <= hi)
steps = steps + 1
mid = (lo + hi) / 2 // integer division
if (a[mid] == 11)
break
else if (a[mid] < 11)
lo = mid + 1
else
hi = mid - 1
end if
end while
Print steps
3
6
Sign up free to see the answer and a step-by-step explanation.
What will be the output of the following pseudocode?
Integer a[] = {5, 2, 4, 6, 1, 3}
for (each i from 1 to 5)
Integer key = a[i], j = i - 1
while (j >= 0 AND a[j] > key)
a[j + 1] = a[j]
j = j - 1
end while
a[j + 1] = key
end for
Print a[0], a[5]
6 1
1 5
5 3
1 6
Sign up free to see the answer and a step-by-step explanation.
What will be the output of the following pseudocode?
Integer a[] = {1, 2, 3, 4}
Integer p[5]
p[0] = 0
for (each i from 0 to 3)
p[i + 1] = p[i] + a[i]
end for
Print p[4] - p[1]
7
6
9
10
Sign up free to see the answer and a step-by-step explanation.
2
4