ProgrammingHardHCL · Tech Mahindra pattern

Assume a 64-bit LP64 ABI. What is printed?

Assume a 64-bit LP64 ABI. What is printed?
int *filterEven(int *arr, int n, int *outCount) {
    static int result[10];
    int count = 0;
    for (int i = 0; i < n; i++)
        if (arr[i] % 2 == 0) result[count++] = arr[i];
    *outCount = count;
    return result;
}
int main(void) {
    int nums[6] = {1, 2, 3, 4, 5, 6};
    int count;
    int *evens = filterEven(nums, 6, &count);
    printf("%d:", count);
    for (int i = 0; i < count; i++) printf("%d ", evens[i]);
}
  1. A. 3:2 4 6
  2. B. 6:1 2 3 4 5 6
  3. C. 3:1 3 5
  4. D. 0:

Correct answer: A. 3:2 4 6

The function uses a static array (safely returnable, since it's not a local/automatic variable) to collect even numbers: 2, 4, 6 (3 of them). count=3 is written through outCount, and the returned pointer correctly lets main iterate them.

More C Programming questions

Exams that ask this topic