Question 1
EasyWhat will be the output of the following pseudocode?
Integer a[5] = {4, 8, 15, 16, 23}
Print a[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] = {4, 8, 15, 16, 23}
Print a[2]
16
8
15
4
Answer: C
Indexing starts at 0: a[0] = 4, a[1] = 8, a[2] = 15.
What will be the output of the following pseudocode?
Integer a[] = {3, 7, 1, 9, 4}
Integer s = 0
for (each i from 0 to 4)
s = s + a[i]
end for
Print s
25
24
20
9
Answer: B
The loop adds every element: 3 + 7 + 1 + 9 + 4 = 24.
What will be the output of the following pseudocode?
Integer a[] = {3, 7, 1, 9, 4}
Integer m = a[0]
for (each i from 1 to 4)
if (a[i] > m)
m = a[i]
end if
end for
Print m
4
7
9
3
Answer: C
m keeps the largest value seen, ending at 9.
What will be the output of the following pseudocode?
Integer a[] = {2, 5, 8, 11}
Integer c = 0
for (each i from 0 to 3)
if (a[i] mod 2 == 0)
c = c + 1
end if
end for
Print c
3
4
2
1
Answer: C
a[i] mod 2 == 0 picks the even elements, 2 and 8, so c = 2.
What will be the output of the following pseudocode?
Integer a[4]
for (each i from 0 to 3)
a[i] = i * i
end for
Print a[3]
4
9
16
3
Answer: B
The loop stores i × i, so a = {0, 1, 4, 9} and a[3] = 9.
What will be the output of the following pseudocode?
Integer a[] = {5, 10, 15}
a[1] = a[0] + a[2]
Print a[1]
15
25
20
10
Answer: C
a[1] is overwritten with a[0] + a[2] = 5 + 15 = 20.
What will be the output of the following pseudocode?
Integer a[] = {1, 2, 3, 4, 5}
Integer i = 0
while (i < 5 AND a[i] != 4)
i = i + 1
end while
Print i
4
2
5
3
Answer: D
This is linear search: 4 is at index 3.
What will be printed by the following pseudocode?
Integer a[] = {5, 2, 8, 1}
Print a[0] + a[3]
6
3
10
13
Answer: A
a[0] is 5 and a[3] is 1 (indices start at 0).
What will be printed by the following pseudocode?
Integer a[] = {3, 6, 9, 12}, s = 0
for (each i from 0 to 3)
s = s + a[i]
end for
Print s
30
27
21
12
Answer: A
The loop adds every element: 3 + 6 + 9 + 12 = 30.
What is the output of this pseudocode?
Integer a[] = {1, 5, 3, 7, 9}, count = 0
for (each i from 0 to 4)
if (a[i] > 4)
count = count + 1
end if
end for
Print count
2
21
3
4
Answer: C
5, 7 and 9 are greater than 4.