Question 41
MediumWhat does the following pseudocode print?
Integer a[8] = {1, 2, 2, 3, 3, 3, 4, 4}
Integer i, c = 1
for (each i from 1 to 7)
if (a[i] != a[i - 1])
c = c + 1
end if
end for
Print c
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 does the following pseudocode print?
Integer a[8] = {1, 2, 2, 3, 3, 3, 4, 4}
Integer i, c = 1
for (each i from 1 to 7)
if (a[i] != a[i - 1])
c = c + 1
end if
end for
Print c
8
4
3
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[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}
Integer i, j, s = 0
for (each i from 0 to 2)
for (each j from 0 to 2)
if (i > j)
s = s + a[i][j]
end if
end for
end for
Print s
19
11
45
15
Sign up free to see the answer and a step-by-step explanation.
What is the output of this pseudocode?
Integer a[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}
Integer b[3][3], i, j
for (each i from 0 to 2)
for (each j from 0 to 2)
b[j][2 - i] = a[i][j]
end for
end for
Print b[0][0], b[0][2], b[2][0]
1 3 7
7 9 1
7 1 9
3 1 9
Sign up free to see the answer and a step-by-step explanation.
What is the output of this pseudocode?
Integer a[6] = {0, 1, 0, 1, 1, 0}
Integer i, z = 0
for (each i from 0 to 5)
if (a[i] == 0)
a[z] = 0
z = z + 1
end if
end for
for (each i from z to 5)
a[i] = 1
end for
Print a[0], a[2], a[3], a[5]
1 1 0 0
0 0 1 1
0 1 0 1
0 0 0 1
Sign up free to see the answer and a step-by-step explanation.
What does the following pseudocode print?
Integer a[5] = {7, 3, 9, 1, 6}
Integer i, pos = 0
for (each i from 1 to 4)
if (a[i] < a[pos])
pos = i
end if
end for
Print pos, a[pos]
0 7
4 6
3 1
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[6] = {1, 2, 3, 4, 5, 6}
Integer i, t = a[0]
for (each i from 0 to 4)
a[i] = a[i + 1]
end for
a[5] = t
Print a[0], a[5]
2 6
6 1
2 1
1 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, 7, 11, 15, 20}
Integer i = 0, j = 4, target = 22
while (i < j)
if (a[i] + a[j] == target)
break
else if (a[i] + a[j] < target)
i = i + 1
else
j = j - 1
end if
end while
Print a[i], a[j]
7 15
11 11
2 20
7 20
Sign up free to see the answer and a step-by-step explanation.
What is the output of this pseudocode?
Integer a[5] = {3, -2, 5, -1, 4}
Integer best = a[0], cur = a[0], i
for (each i from 1 to 4)
cur = max(a[i], cur + a[i])
best = max(best, cur)
end for
Print best
9
8
5
12
Sign up free to see the answer and a step-by-step explanation.
What does the following pseudocode print?
Integer a[5] = {10, 20, 30, 40, 50}
Integer i, s = 0
for (each i from 4 down to 0)
s = s * 2 + a[i] / 10
end for
Print s
15
31
129
57
Sign up free to see the answer and a step-by-step explanation.
What does the following pseudocode print?
Integer a[6] = {5, 3, 5, 1, 3, 5}
Integer i, j, c, best = 0, val
for (each i from 0 to 5)
c = 0
for (each j from 0 to 5)
if (a[j] == a[i]) c = c + 1
end for
if (c > best)
best = c
val = a[i]
end if
end for
Print val, best
3 2
1 1
5 2
5 3
Sign up free to see the answer and a step-by-step explanation.