IAE Academy

APTITUDE PRACTICE · PROGRAMMING LOGIC

Functions and recursion: aptitude questions

10 questions for the aptitude round of campus placement tests, each with its answer and working. Try each one before you open the answer.

Companies do not publish their test papers; these questions are ours.

The questions

In the pseudocode, for i = a to b runs i from a up to b, with b included.

  1. QUESTION 1

    What does f(4) return?

    int f(int n) {
        if (n == 0) return 1;
        return n * f(n - 1);
    }
    1. (A)

      10

    2. (B)

      16

    3. (C)

      24

    4. (D)

      120

    Answer and solution

    Answer: (C)

    1. f(0) = 1.

    2. f(1) = 1 f(0) = 1, f(2) = 2 1 = 2.

    3. f(3) = 3 * 2 = 6.

    4. f(4) = 4 * 6 = 24. The answer is 24.

  2. QUESTION 2

    What does s(4096) return?

    int s(int n) {
        if (n == 0) return 0;
        return n % 10 + s(n / 10);
    }
    1. (A)

      15

    2. (B)

      18

    3. (C)

      19

    4. (D)

      20

    Answer and solution

    Answer: (C)

    1. Each call adds the last digit and passes on the rest.

    2. s(4096) = 6 + s(409) = 6 + 9 + s(40).

    3. s(40) = 0 + s(4) = 0 + 4 + s(0) = 4.

    4. Total = 6 + 9 + 0 + 4 = 19. The answer is 19.

  3. QUESTION 3

    What does p(3, 4) return?

    int p(int b, int e) {
        if (e == 0) return 1;
        return b * p(b, e - 1);
    }
    1. (A)

      12

    2. (B)

      64

    3. (C)

      81

    4. (D)

      243

    Answer and solution

    Answer: (C)

    1. The function multiplies b once for each step down from e to 0.

    2. p(3, 0) = 1, p(3, 1) = 3, p(3, 2) = 9.

    3. p(3, 3) = 27, p(3, 4) = 81. The answer is 81.

  4. QUESTION 4Type the answer

    The function below is called as fib(5). Counting that first call, how many calls to fib are made in total?

    int fib(int n) {
        if (n <= 1) return n;
        return fib(n - 1) + fib(n - 2);
    }
    Answer and solution

    Answer:

    1. Let c(n) be the number of calls made by fib(n). Then c(0) = c(1) = 1.

    2. For n >= 2, c(n) = 1 + c(n-1) + c(n-2).

    3. c(2) = 3, c(3) = 1 + 3 + 1 = 5, c(4) = 1 + 5 + 3 = 9.

    4. c(5) = 1 + 9 + 5 = 15. The answer is 15.

  5. QUESTION 5

    What is printed by show(5)?

    void show(int n) {
        if (n > 0) {
            printf("%d ", n);
            show(n - 2);
            printf("%d ", n);
        }
    }
    1. (A)

      5 3 1 3 5

    2. (B)

      5 3 1 1 3 5

    3. (C)

      1 3 5 5 3 1

    4. (D)

      5 3 1 -1 1 3 5

    Answer and solution

    Answer: (B)

    1. show(5) prints 5, then calls show(3).

    2. show(3) prints 3, then calls show(1). show(1) prints 1, then calls show(-1), which prints nothing.

    3. On the way back, show(1) prints 1, show(3) prints 3, show(5) prints 5.

    4. Output: 5 3 1 1 3 5. The answer is B.

  6. QUESTION 6

    What does g(84, 36) return?

    int g(int a, int b) {
        if (b == 0) return a;
        return g(b, a % b);
    }
    1. (A)

      6

    2. (B)

      12

    3. (C)

      18

    4. (D)

      36

    Answer and solution

    Answer: (B)

    1. g(84, 36) calls g(36, 84 % 36) = g(36, 12).

    2. g(36, 12) calls g(12, 36 % 12) = g(12, 0).

    3. b is 0, so g(12, 0) returns 12. The answer is 12.

  7. QUESTION 7

    What does pw(2, 9) return?

    int pw(int b, int e) {
        if (e == 0) return 1;
        int h = pw(b, e / 2);
        if (e % 2 == 0) return h * h;
        return h * h * b;
    }
    1. (A)

      256

    2. (B)

      512

    3. (C)

      1024

    4. (D)

      18

    Answer and solution

    Answer: (B)

    1. The chain of exponents is 9, 4, 2, 1, 0. pw(2, 0) = 1.

    2. pw(2, 1): odd, so 1 1 2 = 2. pw(2, 2): even, so 2 * 2 = 4.

    3. pw(2, 4): even, so 4 * 4 = 16.

    4. pw(2, 9): odd, so 16 16 2 = 512. The answer is 512.

  8. QUESTION 8Type the answer

    What does h(9875) return?

    int h(int n) {
        if (n < 10) return n;
        return h(n / 10 + n % 10);
    }
    Answer and solution

    Answer:

    1. h(9875) = h(987 + 5) = h(992).

    2. h(992) = h(99 + 2) = h(101).

    3. h(101) = h(10 + 1) = h(11).

    4. h(11) = h(1 + 1) = h(2), and 2 < 10 so it returns 2. The answer is 2.

  9. QUESTION 9

    What does f(15) return?

    int f(int n) {
        if (n > 20) return n - 5;
        return f(f(n + 8));
    }
    1. (A)

      10

    2. (B)

      16

    3. (C)

      18

    4. (D)

      21

    Answer and solution

    Answer: (B)

    1. f(15) = f(f(23)). Here f(23) = 23 - 5 = 18, so f(15) = f(18).

    2. f(18) = f(f(26)). f(26) = 21, so f(18) = f(21).

    3. f(21) = 21 - 5 = 16, since 21 > 20.

    4. So f(18) = 16 and f(15) = 16. The answer is 16.

  10. QUESTION 10

    What does f(7) return?

    int f(int n) {
        if (n <= 2) return n;
        return f(n - 1) + f(n - 3);
    }
    1. (A)

      7

    2. (B)

      10

    3. (C)

      12

    4. (D)

      15

    Answer and solution

    Answer: (B)

    1. Base values: f(0) = 0, f(1) = 1, f(2) = 2.

    2. f(3) = f(2) + f(0) = 2. f(4) = f(3) + f(1) = 3.

    3. f(5) = f(4) + f(2) = 5. f(6) = f(5) + f(3) = 7.

    4. f(7) = f(6) + f(4) = 7 + 3 = 10. The answer is 10.

More practice

More programming logic: Loops and conditions · Arrays and strings · Operators and number logic · Stacks, queues and basic data structures · C language basics

Past GATE aptitude questions, solved: General Aptitude

Every topic with its answers, to print or keep: one PDF (1.9 MB)

All aptitude topics