Previously asked Capgemini questions
Problems candidates reported from recent Capgemini tests, rewritten with worked solutions (memory-based).
1. What is printed?
Integer s = 0, i = 0, n = 127, r
while (n > 0)
r = n mod 10
s = s + (8 ^ i) * r // 8 raised to the power i
i = i + 1
n = n / 10 // integer division
end while
print s- 27
- 87
- 187
- 120
Answer
B. 87
How to solve: Trace the pseudocode code line by line, keeping a small table of every variable's value, and print exactly what each output statement prints.
- Digits from the right are 7, 2, 1 with weights 8⁰, 8¹, 8²: 7 + 16 + 64 = 87.
- (It reads 127 as an octal number.)
Shortcut: Integer division drops the fraction (7 / 2 = 3 in C/Java; use 7 // 2 in Python), and % gives the remainder.
2. What is printed (no spaces between numbers)?
Integer n1 = 0, n2 = 1, n3 = 1, count = 1, limit = 6
while (count <= limit)
count = count + 1
print n3
n1 = n2
n2 = n3
n3 = n1 + n2
end while- 123581321
- 112358
- 12358
- 1235813
Answer
D. 1235813
How to solve: Trace the pseudocode code line by line, keeping a small table of every variable's value, and print exactly what each output statement prints.
- The loop runs 6 times and prints 1, 2, 3, 5, 8, 13, the Fibonacci numbers starting from the third term.
- So the answer is 1235813.
Shortcut: Count the loop iterations carefully: check the starting value, the condition and the update; off-by-one errors are the usual trap.
3. What is the value of counter at the end?
Integer number = 2630, t = 2630, d, counter = 0
while (t > 0)
d = t mod 10
if (d != 0 and number mod d == 0)
counter = counter + 1
end if
t = t / 10
end while
print counter- 2
- 4
- 3
- 1
Answer
D. 1
How to solve: Trace the pseudocode code line by line, keeping a small table of every variable's value, and print exactly what each output statement prints.
- The digits are 0, 3, 6 and 2.
- Zero is skipped
- 2630 is not divisible by 3 or 6 but is divisible by 2
- Counter = 1.
Shortcut: Count the loop iterations carefully: check the starting value, the condition and the update; off-by-one errors are the usual trap.
4. What does mul(56, 876) return?
Function mul(Integer a, Integer b)
Integer t = 0
while (b != 0)
t = t + a
b = b - 1
end while
return t- 490563
- 49056
- 4905
- 49065
Answer
B. 49056
How to solve: Trace the pseudocode code line by line, keeping a small table of every variable's value, and print exactly what each output statement prints.
- It adds 56 to t 876 times, which is multiplication: 56 × 876 = 49056.
Shortcut: Count the loop iterations carefully: check the starting value, the condition and the update; off-by-one errors are the usual trap.
5. What does fun(8, 9) return?
Function fun(Integer a, Integer b)
if (a < b)
return fun(b, a)
else if (b != 0)
return a + fun(a, b - 1)
else
return 0
end if- 65
- 72
- 56
- 88
Answer
B. 72
How to solve: Trace the pseudocode code line by line, keeping a small table of every variable's value, and print exactly what each output statement prints.
- Fun(8, 9) swaps to fun(9, 8), which adds 9 eight times: 9 × 8 = 72.
Shortcut: Count the loop iterations carefully: check the starting value, the condition and the update; off-by-one errors are the usual trap.
6. What is printed?
Integer m = 9, n = 6
m = m + 1
n = n - 1
m = m + n
if (m > n)
print m
else
print n
end if- 5
- 10
- 15
- 6
Answer
C. 15
How to solve: Trace the pseudocode code line by line, keeping a small table of every variable's value, and print exactly what each output statement prints.
- M becomes 10, n becomes 5, then m = 10 + 5 = 15.
- Since 15 > 5, it prints 15.
7. What is printed?
Integer f = 6, g = 9, sum = 0
if (g > f)
for (n = f; n < g; n = n + 1)
sum = sum + n
end for
else
print "Error"
end if
print sum- 30
- 6
- 15
- 21
Answer
D. 21
How to solve: Trace the pseudocode code line by line, keeping a small table of every variable's value, and print exactly what each output statement prints.
- The loop adds 6, 7 and 8 (it stops before 9): sum = 21.
Shortcut: Count the loop iterations carefully: check the starting value, the condition and the update; off-by-one errors are the usual trap.
8. What does this C program print?
#include <stdio.h>
int main() {
static int i = 5;
if (--i) {
printf("%d ", i);
main();
}
return 0;
}- Infinite loop
- 4 4 4 4
- 4 3 2 1
- 5 4 3 2 1
Answer
C. 4 3 2 1
How to solve: Trace the pseudocode code line by line, keeping a small table of every variable's value, and print exactly what each output statement prints.
- A static variable keeps its value across the recursive calls of main: it prints 4, 3, 2, 1 and stops when --i makes it 0.
- So the answer is 4 3 2 1.
Shortcut: i++ uses the old value and then adds 1; ++i adds 1 first. In a loop condition or a print, that one-step difference changes the output.