What is the output of fun1()?
Qualcomm technical mcq question, verified with a worked answer. Free to practise - no sign-up.
int fun(char b, char c)
{
b = c = 1;
printf("%d\n", b >> -1);
printf("%d\n", c >> -1);
}
int fun1(char b, char c)
{
b = b << 1;
c = b = 1 << 1;
printf("%d\n", c);
}
What is the output of fun1()?
Show answer & explanation
Answer: A. 2
In fun1(), the expression c = b = 1 << 1 is evaluated right-to-left: 1 << 1 equals 2, then b = 2, then c = 2. The printf("%d\n", c) outputs the single integer value 2. The first statement b = b << 1 has no effect on the final output since b is reassigned later. Note: fun() itself contains undefined behavior due to right-shifting by a negative count (b >> -1), but the question asks only about fun1()'s output.
Step-by-step Derivation:
Step-by-step execution of fun1(char b, char c):
b = b << 1;— Shifts b left by 1 bit (but b is later overwritten, so this has no lasting effect)c = b = 1 << 1;— Evaluated right-to-left:1 << 1= 2 (shift 1 left by 1 position: binary 01 → 10)b = 2(assign 2 to b)c = 2(assign 2 to c)
printf("%d\n", c);— Prints the value of c, which is 2
Output: 2