MCQ
Qualcomm technical mcq question, verified with a worked answer. Free to practise - no sign-up.
char d;
a = fun(b, c);
d = fun1(b, c);
printf("%d\t%d", a, d);
return 0;
}
int fun(char b, char c)
{
b = c = 1;
printf("%d\t", b >> -1);
printf("%d\t", c >> -1);
}
int fun1(char b, char c)
{
b = b << 1;
c = c = 1 << 1;
printf("%d\n",c);
}
**MCQ
Show answer & explanation
The code exhibits undefined behavior because b >> -1 and c >> -1 perform right shifts by negative amounts, which is not defined in C. According to the C standard, shifting by a negative value is undefined behavior. Additionally, both fun() and fun1() lack explicit return statements, causing them to return garbage values. The original options are incomplete/incorrect representations of this undefined behavior.
Step-by-step Derivation:
Step-by-step analysis:
Function
fun(b, c)execution:- Assigns
b = c = 1(both become 1) - Executes
b >> -1: RIGHT SHIFT BY NEGATIVE VALUE → UNDEFINED BEHAVIOR - Executes
c >> -1: RIGHT SHIFT BY NEGATIVE VALUE → UNDEFINED BEHAVIOR - No explicit return statement (returns garbage/uninitialized value)
- Prints: (undefined) (undefined) (undefined)
- Assigns
Function
fun1(b, c)execution:- Assigns
b = b << 1(b was 0, now 0) - Assigns
c = (1 << 1) = 2(left shift: 1 << 1 = 2) - Prints: 2
- No explicit return statement (returns garbage/uninitialized value)
- Assigns
Main printf:
- Prints values of
aandd(both uninitialized garbage due to missing returns)
- Prints values of
Key Issue: Right-shifting by a negative value (>> -1) is undefined behavior in C. This makes the entire program's output unpredictable. The provided options appear to be from a buggy assessment or assume a specific compiler behavior that violates the C standard.