What is the output of this program?
Qualcomm technical mcq question, verified with a worked answer. Free to practise - no sign-up.
int main()
{
#define h 6
if (h > k)
{
printf("%d", k && (2, 4, 6));
}
else if (!n)
{
printf("%d", h * k);
}
else
{
printf("%d", printf("%d", (8, 9, 7)));
}
return 0;
}
What is the output of this program?
Show answer & explanation
Answer: B. 0
The program has undefined behavior because variables k and n are declared but not initialized. However, in most environments, uninitialized global/static variables default to 0, and uninitialized local variables have indeterminate values. Assuming k = 0 and n = 0, the condition h > k (6 > 0) is true, so the first branch executes: printf("%d", k && (2, 4, 6)). The comma operator evaluates left-to-right and returns the rightmost value (6), so k && 6 becomes 0 && 6, which is 0 due to short-circuit AND evaluation.
Step-by-step Derivation:
Step-by-step execution:
his defined as 6 via macro.- Variables
kandnare undeclared (undefined behavior), typically assumed to be 0 in most environments. - First condition:
if (h > k)→if (6 > 0)→ true. - Execute first branch:
printf("%d", k && (2, 4, 6)). - Comma operator in
(2, 4, 6)evaluates to 6 (rightmost value). - Expression
k && 6→0 && 6. - AND operator short-circuits:
0 && anythingis always 0. printf("%d", 0)outputs: 0