Question 1
EasyWhat will be printed by the following pseudocode?
Integer a = 0
for (each i from 1 to 5)
a = a + i
end for
Print a
Enter a number as the answer.
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 = 0
for (each i from 1 to 5)
a = a + i
end for
Print a
Enter a number as the answer.
Answer: 15
The loop adds 1 + 2 + 3 + 4 + 5 to a, giving 15.
What will be the output of the following pseudocode?
Integer sum = 0
for (each i from 1 to 5)
sum = sum + i
end for
Print sum
5
10
15
20
Answer: C
The loop adds 1 + 2 + 3 + 4 + 5 = 15.
What will be the output of the following pseudocode?
Integer count = 0
for (each i from 1 to 10)
if (i mod 3 == 0)
count = count + 1
end if
end for
Print count
3
10
4
2
Answer: A
Multiples of 3 between 1 and 10: 3, 6 and 9.
What will be the output of the following pseudocode?
Integer f = 1
for (each i from 1 to 4)
f = f * i
end for
Print f
120
24
10
4
Answer: B
f = 1 × 2 × 3 × 4 = 24 (4 factorial).
What will be the output of the following pseudocode?
Integer n = 0
while (n < 20)
n = n + 6
end while
Print n
24
30
18
20
Answer: A
n takes the values 6, 12, 18, 24; the loop stops once n is no longer less than 20.
What will be the output of the following pseudocode?
Integer x = 1
for (each i from 1 to 3)
x = x * 2
end for
Print x
6
16
4
8
Answer: D
x doubles three times: 1 → 2 → 4 → 8.
What will be the output of the following pseudocode?
Integer a = 10
while (a > 0)
a = a - 3
end while
Print a
1
-3
-2
0
Answer: C
a goes 10, 7, 4, 1, −2; the loop stops when a is no longer positive.
What will be the output of the following pseudocode?
Integer c = 0
for (each i from 1 to 4)
for (each j from 1 to 3)
c = c + 1
end for
end for
Print c
7
4
12
3
Answer: C
The inner statement runs 3 times for each of the 4 outer iterations: 12.
What is the output of this pseudocode?
Integer s = 0
for (each i from 1 to 10)
if (i mod 2 == 0)
s = s + i
end if
end for
Print s
25
30
55
20
Answer: B
Only the even values 2, 4, 6, 8 and 10 are added: 30.
What will be printed by the following pseudocode?
Integer n = 4567, count = 0
while (n > 0)
n = n / 10
count = count + 1
end while
Print count
4567
3
4
5
Answer: C
Integer division by 10 removes one digit each time: 456, 45, 4, 0. Four passes, one per digit.