Question 71
HardWhat will be the output of the following pseudocode?
Integer a[5] = {1, 2, 3, 4, 5}
Integer b[5], i
for (each i from 0 to 4)
b[(i + 2) mod 5] = a[i]
end for
Print b[0], b[1], b[2]
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 the output of the following pseudocode?
Integer a[5] = {1, 2, 3, 4, 5}
Integer b[5], i
for (each i from 0 to 4)
b[(i + 2) mod 5] = a[i]
end for
Print b[0], b[1], b[2]
1 2 3
4 5 1
5 1 2
3 4 5
Sign up free to see the answer and a step-by-step explanation.
What is the output of this pseudocode?
Integer a[6] = {5, 4, 3, 2, 1, 0}
Integer i, c = 0
for (each i from 0 to 5)
while (a[i] != i)
Integer t = a[a[i]]
a[a[i]] = a[i]
a[i] = t
c = c + 1
end while
end for
Print c
5
3
15
6
Sign up free to see the answer and a step-by-step explanation.
What will be printed by the following pseudocode?
Integer a[5] = {2, 4, 1, 3, 5}
Integer i, j, inv = 0
for (each i from 0 to 4)
for (each j from i + 1 to 4)
if (a[i] > a[j]) inv = inv + 1 end if
end for
end for
Print inv
3
2
5
4
Sign up free to see the answer and a step-by-step explanation.
What will be printed by the following pseudocode?
Integer a[6] = {1, -2, 3, 10, -4, 7}
Integer i, cur = 0, best = a[0], start = 0, bs = 0, be = 0
for (each i from 0 to 5)
if (cur <= 0)
cur = a[i]
start = i
else
cur = cur + a[i]
end if
if (cur > best)
best = cur; bs = start; be = i
end if
end for
Print best, bs, be
13 2 3
16 2 5
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] = {1, 2, 3, 4, 5}
Integer pre[6], i
pre[0] = 0
for (each i from 1 to 5)
pre[i] = pre[i - 1] + a[i - 1]
end for
Print pre[5] - pre[2], pre[4] - pre[1]
9 12
12 10
15 10
12 9
Sign up free to see the answer and a step-by-step explanation.
What will be printed by the following pseudocode?
Integer a[4][4], i, j, s = 0
for (each i from 0 to 3)
for (each j from 0 to 3)
a[i][j] = i * 4 + j
end for
end for
for (each i from 0 to 3)
s = s + a[i][i] + a[i][3 - i]
end for
Print s
60
120
48
30
Sign up free to see the answer and a step-by-step explanation.
What will be printed by the following pseudocode?
Integer a[7] = {1, 1, 2, 2, 2, 3, 3}
Integer i, w = 1
for (each i from 1 to 6)
if (a[i] != a[w - 1])
a[w] = a[i]
w = w + 1
end if
end for
Print w, a[w - 1]
7 3
4 3
3 3
3 2
Sign up free to see the answer and a step-by-step explanation.
What will be the output of the following pseudocode?
Integer a[6] = {9, 4, 7, 1, 8, 3}
Integer i, m1 = -1, m2 = -1
for (each i from 0 to 5)
if (a[i] > m1)
m2 = m1; m1 = a[i]
else if (a[i] > m2)
m2 = a[i]
end if
end for
Print m1 * m2
81
72
63
56
Sign up free to see the answer and a step-by-step explanation.
What does the following pseudocode print?
Integer a[5] = {2, 3, 1, 1, 4}
Integer i, far = 0, ok = 1
for (each i from 0 to 4)
if (i > far)
ok = 0
end if
if (i + a[i] > far)
far = i + a[i]
end if
end for
Print ok, far
0 8
1 5
1 4
1 8
Sign up free to see the answer and a step-by-step explanation.
What is the output of this pseudocode?
Integer a[6] = {1, 0, 2, 0, 3, 0}
Integer i, w = 0
for (each i from 0 to 5)
if (a[i] != 0)
Integer t = a[w]; a[w] = a[i]; a[i] = t
w = w + 1
end if
end for
Print a[0], a[1], a[2], a[3], w
0 0 0 1 3
1 2 3 0 6
1 2 3 0 3
1 0 2 0 3
Sign up free to see the answer and a step-by-step explanation.
16 0 5
15 0 5