C Programming: Essential MCQs for Interviews & Competitive Exams

C Programming: Essential MCQs for Interviews & Competitive Exams

C Programming: Essential MCQs for Interviews & Competitive Exams

Question 1: Identifying Pointer Issues in C Functions

Consider the following three C functions:

P1:

int * g (void) {
    int x = 10;
    return (&x);
}

P2:

int * g (void) {
    int * px;
    *px = 10;
    return px;
}

P3:

int * g (void) {
    int *px;
    px = (int *) malloc (sizeof(int));
    *px = 10;
    return px;
}

Which of the above three functions is likely to cause problems with pointers?

  • (a) Only P3
  • (b) Only P1 and P3
  • (c) Only P1 and P2
  • (d) P1, P2, and P3

Answer: (c)

Explanation:

  • In P1, x is a local variable, and returning its address causes undefined behavior as x ceases to exist once the function returns.
  • In P2, px is an uninitialized pointer, and assigning a value to *px results in undefined behavior.
  • P3 is correct as it allocates memory using malloc(), ensuring the pointer remains valid after the function returns.

Question 2: Static Variable Behavior in Function Calls

What is the value of j at the end of the execution of the following C program?

int incr(int i) {
    static int count = 0;
    count = count + i;
    return (count);
}

int main() {
    int i, j;
    for (i = 0; i <= 4; i++)
        j = incr(i);
}

Options:

  • (a) 10
  • (b) 4
  • (c) 6
  • (d) 7

Answer: (a)

Explanation:

  • count is a static variable, meaning its value persists across function calls.
  • Iterations:
    • incr(0) → count = 0
    • incr(1) → count = 1
    • incr(2) → count = 3
    • incr(3) → count = 6
    • incr(4) → count = 10

Question 3: Memory Requirement for a C Structure

Consider the following declaration:

struct {
    short s[5];
    union {
        float y;
        long z;
    } u;
} t;

Assume:

  • short = 2 bytes
  • float = 4 bytes
  • long = 8 bytes

What is the total memory requirement for variable t, ignoring alignment?

  • (a) 22 bytes
  • (b) 14 bytes
  • (c) 18 bytes
  • (d) 10 bytes

Answer: (c)

Explanation:

  • short s[5] requires 10 bytes (5 × 2 bytes).
  • The union u takes 8 bytes (maximum of float and long).
  • Total memory required: 10 + 8 = 18 bytes.

Question 4: Counting Tokens in a C Statement

How many tokens are present in the following statement?

printf("i = %d, &i = %x", i, &i);

Options:

  • (a) 3
  • (b) 26
  • (c) 10
  • (d) 21

Answer: (c)

Explanation: Tokens in C include keywords, identifiers, constants, operators, string literals, and separators. The above statement consists of 10 tokens:

  1. printf
  2. (
  3. "i = %d, &i = %x" (string literal)
  4. ,
  5. i
  6. ,
  7. &
  8. i
  9. )
  10. ;

Question 5: Understanding Pointer Arrays in C

What does the following declaration define?

struct node {
    int i;
    float j;
};

struct node *s[10];

Options:

  • (a) An array, each element of which is a pointer to a structure of type node.
  • (b) A structure of 2 fields, each field being a pointer to an array of 10 elements.
  • (c) A structure of 3 fields: an integer, a float, and an array of 10 elements.
  • (d) An array, each element of which is a structure of type node.

Answer: (a)

Explanation:

  • s[10] is an array of 10 elements.
  • Each element is a pointer to a struct node.
  • This means s stores addresses of node structures rather than the structures themselves.

Conclusion

These C programming questions cover essential topics like pointers, memory allocation, static variables, structures, and tokens. Understanding these concepts is crucial for technical interviews and competitive exams.

C Programming: Essential MCQs for Interviews & Competitive Exams