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?
Show answer & explanation
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:
Syntax Error: The line
j = & k a;is malformed. Likely intended asj = &a;based on context.Format String Issues:
printf("%n %d %u %j", j, k);has invalid specifiers:%nwrites to a pointer (security risk, dangerous)%jis non-standard (not recognized in C)- Missing third and fourth arguments for the format string
Undefined Behavior:
- Even if corrected to
j = &a; printf("%d %d", j, k); j++increments address ofa(pointer arithmetic)kis uninitialized;k++operates on garbage- Printing uninitialized pointer
kyields garbage - Printing computed pointer
j(address ofa+ 1) is unpredictable
- Even if corrected to
Conclusion: Code does not compile cleanly; output is undefined/garbage.