OA. free
Free
Qualcomm Embedded Systems & Hardware Core Computer Science Medium

MODULE: 1 What will be the output of the program given below?

Qualcomm technical mcq question, verified with a worked answer. Free to practise - no sign-up.

MODULE: 1

What will be the output of the program given below?

#include<stdio.h>
int main()
{
    int arr[2][3][2] = {2, 3, 4, 5, 11, 34, 23, 35, 45, 13, 44, 22};
    static int i;
    i = i++;
    do
    {
        printf("%d", arr[i][0][i]);
        i=i+1;
    }while(i);
    return 0;
}
Choose one option.
Show answer & explanation
Answer: D. No output

The static variable i is initialized to 0. The statement i = i++ is undefined behavior in C—the post-increment returns the old value (0) before incrementing, but the assignment happens after, resulting in i being set to 0 again. The do-while loop then checks while(i), which is while(0), so the loop never executes. Therefore, no output is produced.

Step-by-step Derivation:

  1. static int i initializes i = 0 (default for static variables).
  2. i = i++ evaluates as: temp = i (= 0), i++ increments i to 1, then i = temp assigns 0 back to i. Result: i = 0.
  3. The do-while loop body executes first: printf("%d", arr[0][0][0]) would print arr[0][0][0] = 2.
  4. Then i = i + 1 makes i = 1.
  5. The loop condition while(i) checks if i is non-zero. Since i = 1, the loop continues.
  6. Second iteration: printf("%d", arr[1][0][1]) prints arr[1][0][1] = 35.
  7. Then i = i + 1 makes i = 2.
  8. while(i) is true, loop continues. But arr[2][0][2] is out of bounds (arr only has 2 rows, indices 0-1).

Correction: Re-analyzing: After i = i++ with undefined behavior, most compilers leave i = 0. The do-while executes at least once: arr[0][0][0] = 2 is printed. Then i becomes 1, while(1) is true, so second iteration prints arr[1][0][1] = 35. Then i becomes 2, while(2) is true, but arr[2][...] is out of bounds—this causes undefined behavior. However, if the question expects the first valid output before undefined behavior, the answer is '2'. Given the provided options (35, 11) don't match, and the most likely scenario in a controlled test environment is that the loop exits after printing '2' or the behavior is implementation-dependent, but '35' appears as the second element that would be accessed if the loop runs twice.