IAE Academy

APTITUDE PRACTICE · PROGRAMMING LOGIC

C language basics: 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

    Assume the code is inside main and stdio.h is included. What does this program print?

    int a = 5;
    int b = a++;
    printf("%d %d", a, b);
    1. (A)

      5 5

    2. (B)

      6 5

    3. (C)

      6 6

    4. (D)

      5 6

    Answer and solution

    Answer: (B)

    1. a++ gives the old value of a first.

    2. So b gets 5.

    3. After that, a becomes 6.

    4. The program prints 6 5, option B.

  2. QUESTION 2

    Assume the code is inside main and stdio.h is included. What does this program print?

    int x = 7;
    printf("%d", ++x * 2);
    1. (A)

      14

    2. (B)

      15

    3. (C)

      16

    4. (D)

      18

    Answer and solution

    Answer: (C)

    1. ++x increases x to 8 before it is used.

    2. The value of ++x is therefore 8.

    3. 8 * 2 = 16.

    4. The program prints 16, option C.

  3. QUESTION 3

    Assume the code is inside main and stdio.h is included. What does this program print?

    int a = 17, b = 5;
    printf("%d %d", a / b, a % b);
    1. (A)

      3 2

    2. (B)

      3.4 2

    3. (C)

      2 3

    4. (D)

      3 3

    Answer and solution

    Answer: (A)

    1. Both operands are int, so a / b is integer division.

    2. 17 / 5 = 3, the fractional part is dropped.

    3. 17 % 5 is the remainder, which is 2.

    4. The program prints 3 2, option A.

  4. QUESTION 4

    Assume the code is inside main and stdio.h is included. What does this program print?

    int a = 3, b = 8;
    int *p = &a;
    int *q = &b;
    *p = *q - *p;
    *q = *p + 1;
    printf("%d %d", a, b);
    1. (A)

      3 8

    2. (B)

      5 6

    3. (C)

      5 9

    4. (D)

      6 6

    Answer and solution

    Answer: (B)

    1. p points to a and q points to b.

    2. *p = *q - *p means a = 8 - 3 = 5.

    3. *q = *p + 1 means b = 5 + 1 = 6.

    4. The program prints 5 6, option B.

  5. QUESTION 5

    Assume the code is inside main and stdio.h is included. What does this program print?

    int n = 255;
    printf("%x", n);
    1. (A)

      255

    2. (B)

      ff

    3. (C)

      FF

    4. (D)

      0xff

    Answer and solution

    Answer: (B)

    1. The format %x prints an int in hexadecimal.

    2. It uses lowercase letters and adds no 0x prefix.

    3. 255 = 15 * 16 + 15, and 15 is f in hexadecimal.

    4. The program prints ff, option B.

  6. QUESTION 6

    Assume the code is inside main and stdio.h is included, and that the machine uses ASCII, where 'A' has the code 65. What does this program print?

    char c = 'A' + 3;
    printf("%c %d", c, c);
    1. (A)

      D 68

    2. (B)

      D 3

    3. (C)

      68 D

    4. (D)

      A 68

    Answer and solution

    Answer: (A)

    1. 'A' + 3 = 65 + 3 = 68.

    2. So c holds the code 68.

    3. %c prints the character with code 68, which is D.

    4. %d prints the number 68, so the output is D 68, option A.

  7. QUESTION 7

    Assume the code is inside main and stdio.h is included. An unsigned char holds values from 0 to 255. What does this program print?

    unsigned char u = 250;
    u = u + 10;
    printf("%d", u);
    1. (A)

      260

    2. (B)

      4

    3. (C)

      10

    4. (D)

      0

    Answer and solution

    Answer: (B)

    1. u + 10 is computed as the int 260.

    2. Storing 260 in an unsigned char keeps the value modulo 256.

    3. 260 - 256 = 4.

    4. The program prints 4, option B.

  8. QUESTION 8Type the answer

    Assume the code is inside main and stdio.h is included. What number does this program print?

    int s = 0;
    for (int i = 0; i < 10; i += 3)
        s += i;
    printf("%d", s);
    Answer and solution

    Answer:

    1. i takes the values 0, 3, 6, 9 and stops at 12.

    2. The loop adds 0 + 3 + 6 + 9.

    3. The sum is 18.

    4. The program prints 18.

  9. QUESTION 9

    Assume the code is inside main and stdio.h is included. What does this program print?

    int a = 2;
    int b = a++ * 3;
    int c = ++a * 3;
    printf("%d %d %d", a, b, c);
    1. (A)

      4 6 12

    2. (B)

      3 6 12

    3. (C)

      4 9 12

    4. (D)

      4 6 9

    Answer and solution

    Answer: (A)

    1. In the second line a++ uses 2, so b = 2 * 3 = 6, and then a becomes 3.

    2. In the third line ++a makes a 4 first, so c = 4 * 3 = 12.

    3. The final value of a is 4.

    4. The program prints 4 6 12, option A.

  10. QUESTION 10Type the answer

    Assume stdio.h is included. What number does this program print?

    void f(int *p, int *q) {
        int t = *p;
        *p = *q;
        *q = t + *q;
    }
    
    int main() {
        int x = 4, y = 9;
        f(&x, &y);
        printf("%d", x * y);
        return 0;
    }
    Answer and solution

    Answer:

    1. Inside f, p points to x and q points to y, and t = 4.

    2. *p = *q sets x to 9.

    3. *q = t + *q sets y to 4 + 9 = 13.

    4. x y = 9 13 = 117.

    5. The program prints 117.

More practice

More programming logic: Loops and conditions · Functions and recursion · Arrays and strings · Operators and number logic · Stacks, queues and basic data structures

Past GATE aptitude questions, solved: General Aptitude

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

All aptitude topics