IAE Academy

APTITUDE PRACTICE · PROGRAMMING LOGIC

Loops and conditions: 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 this program print?

    int s = 0;
    for (int i = 1; i <= 5; i++)
        s = s + i;
    printf("%d", s);
    1. (A)

      10

    2. (B)

      15

    3. (C)

      21

    4. (D)

      5

    Answer and solution

    Answer: (B)

    1. The loop runs for i = 1, 2, 3, 4, 5.

    2. Each pass adds i to s, starting from s = 0.

    3. s = 1 + 2 + 3 + 4 + 5.

    4. The program prints 15.

  2. QUESTION 2

    What is printed by this pseudocode?

    x = 18
    if x > 15:
        x = x - 9
    if x > 8:
        x = x + 4
    print x
    1. (A)

      9

    2. (B)

      18

    3. (C)

      22

    4. (D)

      13

    Answer and solution

    Answer: (D)

    1. x = 18 and 18 > 15, so x becomes 18 - 9 = 9.

    2. Now x = 9 and 9 > 8, so the second condition is true.

    3. x becomes 9 + 4 = 13.

    4. The output is 13.

  3. QUESTION 3Type the answer

    What is printed by this pseudocode? In a loop written "for i = 1 to 20", i takes every value from 1 up to and including 20.

    count = 0
    for i = 1 to 20:
        if i mod 4 == 0:
            count = count + 1
    print count
    Answer and solution

    Answer:

    1. The condition is true when i is a multiple of 4.

    2. Since 20 is included, the multiples of 4 from 1 to 20 are 4, 8, 12, 16 and 20.

    3. count goes up once for each of them.

    4. The output is 5.

  4. QUESTION 4

    What is the final value of a?

    int a = 1, i = 0;
    while (i < 4) {
        a = a * 2 + 1;
        i++;
    }
    1. (A)

      15

    2. (B)

      31

    3. (C)

      16

    4. (D)

      63

    Answer and solution

    Answer: (B)

    1. The loop body runs 4 times, for i = 0, 1, 2, 3.

    2. After pass 1, a = 1*2 + 1 = 3. After pass 2, a = 7.

    3. After pass 3, a = 15. After pass 4, a = 31.

    4. The final value of a is 31.

  5. QUESTION 5

    What is the final value of r?

    int n = 2506, r = 0;
    while (n > 0) {
        r = r * 10 + n % 10;
        n = n / 10;
    }
    1. (A)

      2506

    2. (B)

      6502

    3. (C)

      6052

    4. (D)

      6250

    Answer and solution

    Answer: (C)

    1. Pass 1: digit 6, r = 6, n = 250.

    2. Pass 2: digit 0, r = 60, n = 25.

    3. Pass 3: digit 5, r = 605, n = 2.

    4. Pass 4: digit 2, r = 6052, n = 0 and the loop stops. The final value is 6052.

  6. QUESTION 6

    What is printed by this pseudocode?

    t = 0
    for i = 1 to 3:
        for j = i to 3:
            t = t + 1
    print t
    1. (A)

      6

    2. (B)

      3

    3. (C)

      9

    4. (D)

      5

    Answer and solution

    Answer: (A)

    1. The inner loop runs from j = i up to 3.

    2. For i = 1 it runs 3 times, for i = 2 it runs 2 times, for i = 3 it runs once.

    3. t counts the total passes: 3 + 2 + 1.

    4. The output is 6.

  7. QUESTION 7Type the answer

    What is the final value of x after this code runs?

    int x = 3, y = 5;
    while (x < y * 3) {
        x = x + y;
        y = y - 1;
    }
    Answer and solution

    Answer:

    1. Start: x = 3, y = 5. Check 3 < 15, true. Then x = 8, y = 4.

    2. Check 8 < 12, true. Then x = 12, y = 3.

    3. Check 12 < 9, false, so the loop stops.

    4. The final value of x is 12.

  8. QUESTION 8

    What is printed by this pseudocode?

    s = 0
    for i = 1 to 10:
        if i mod 3 == 0:
            continue
        if s > 20:
            break
        s = s + i
    print s
    1. (A)

      27

    2. (B)

      22

    3. (C)

      37

    4. (D)

      19

    Answer and solution

    Answer: (A)

    1. i = 1, 2 give s = 3. i = 3 is skipped. i = 4, 5 give s = 12.

    2. i = 6 is skipped. i = 7 gives s = 19.

    3. i = 8: s = 19 is not above 20, so s = 27. i = 9 is skipped.

    4. i = 10: s = 27 is above 20, so the loop breaks. The output is 27.

  9. QUESTION 9

    What does this program print?

    int f = 0, g = 1;
    for (int i = 0; i < 6; i++) {
        int t = f + g;
        f = g;
        g = t;
    }
    printf("%d", f);
    1. (A)

      5

    2. (B)

      13

    3. (C)

      8

    4. (D)

      21

    Answer and solution

    Answer: (C)

    1. Each pass sets f to the old g and g to the old f + g.

    2. After passes 1 to 3, (f, g) is (1, 1), (1, 2), (2, 3).

    3. After passes 4 to 6, (f, g) is (3, 5), (5, 8), (8, 13).

    4. The program prints f, which is 8.

  10. QUESTION 10Type the answer

    What is the final value of k?

    int k = 0;
    for (int i = 1; i <= 4; i++)
        for (int j = 1; j <= i; j++)
            if ((i + j) % 2 == 0)
                k += i * j;
    Answer and solution

    Answer:

    1. Only pairs with j <= i and i + j even are added.

    2. i = 1: (1,1) adds 1. i = 2: (2,2) adds 4. i = 3: (3,1) adds 3 and (3,3) adds 9.

    3. i = 4: (4,2) adds 8 and (4,4) adds 16.

    4. k = 1 + 4 + 3 + 9 + 8 + 16 = 41.

More practice

More programming logic: Functions and recursion · 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