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
  1. 27
  2. 87
  3. 187
  4. 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.

  1. Digits from the right are 7, 2, 1 with weights 8⁰, 8¹, 8²: 7 + 16 + 64 = 87.
  2. (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
  1. 123581321
  2. 112358
  3. 12358
  4. 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.

  1. The loop runs 6 times and prints 1, 2, 3, 5, 8, 13, the Fibonacci numbers starting from the third term.
  2. 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
  1. 2
  2. 4
  3. 3
  4. 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.

  1. The digits are 0, 3, 6 and 2.
  2. Zero is skipped
  3. 2630 is not divisible by 3 or 6 but is divisible by 2
  4. 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
  1. 490563
  2. 49056
  3. 4905
  4. 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.

  1. 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
  1. 65
  2. 72
  3. 56
  4. 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.

  1. 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
  1. 5
  2. 10
  3. 15
  4. 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.

  1. M becomes 10, n becomes 5, then m = 10 + 5 = 15.
  2. 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
  1. 30
  2. 6
  3. 15
  4. 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.

  1. 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;
}
  1. Infinite loop
  2. 4 4 4 4
  3. 4 3 2 1
  4. 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.

  1. 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.
  2. 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.

Capgemini test pattern and mock tests