OA. free
Free
Qualcomm Embedded Systems & Hardware Embedded Systems & Hardware Medium

What will be the output of this program?

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

#include <stdio.h>

int main()
{
    int a = 10, *j;
    void *k;
    j = & k a;
    j++;
    k++;
    printf("%n %d %u %j", j, k);
    return 0;
}

What will be the output of this program?

Choose one option.
Show answer & explanation
Answer: C. Garbage value Garbage value

The program has multiple compilation errors: (1) j = & k a; is syntactically invalid (appears to be OCR corruption; should be j = &a;), (2) the format string %n %d %u %j is invalid—%n writes output length to a pointer (unsafe) and %j is not a standard format specifier. Assuming the code were corrected to j = &a; and printf("%d %d", j, k);, j would print the address of a (unpredictable), and k is uninitialized and incremented without initialization, yielding garbage values. The original code does not compile, but if forced to run, would produce undefined behavior and garbage output.

Step-by-step Derivation:
Step-by-step analysis:

  1. Syntax Error: The line j = & k a; is malformed. Likely intended as j = &a; based on context.

  2. Format String Issues: printf("%n %d %u %j", j, k); has invalid specifiers:

    • %n writes to a pointer (security risk, dangerous)
    • %j is non-standard (not recognized in C)
    • Missing third and fourth arguments for the format string
  3. Undefined Behavior:

    • Even if corrected to j = &a; printf("%d %d", j, k);
    • j++ increments address of a (pointer arithmetic)
    • k is uninitialized; k++ operates on garbage
    • Printing uninitialized pointer k yields garbage
    • Printing computed pointer j (address of a + 1) is unpredictable
  4. Conclusion: Code does not compile cleanly; output is undefined/garbage.