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

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

Choose one option.
Show answer & explanation
Answer: C. Undefined behavior (right shift by negative value)

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:

  1. 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)
  2. 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)
  3. Main printf:

    • Prints values of a and d (both uninitialized garbage due to missing returns)

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.