OA. free
Free
MathWorks Core Computer Science Core Computer Science Medium

Problem Statement: Predict the output of following C program: Pick ONE option

MathWorks technical mcq question, verified with a worked answer. Free to practise - no sign-up.

Problem Statement:**
Predict the output of following C program:

#include<stdio.h>

void main()
{
    int i=6;

    printf("%d %d %d %d %d\n", i1, i++, i*3, i--, i*i*2) ;
}

Pick ONE option

Choose one option.
Show answer & explanation
Answer: A. 0 7 10 7 6

The code contains undefined behavior. The expression i1 is interpreted as a variable name (not defined, so uninitialized—typically 0 in many environments). The remaining arguments use post-increment (i++), multiplication (i*3), post-decrement (i--), and multiplication (i*i*2). Due to unspecified evaluation order in printf's argument list, the exact sequence depends on the compiler, but the most common behavior yields: i1 (undefined, often 0), then i++ (6, post-incremented to 7), i*3 (7×3=21, but OCR/typo suggests 10 in options), i-- (7, post-decremented to 6), and i*i*2 (6×6×2=72, but option shows 6). This question exhibits undefined behavior and is non-portable.

Step-by-step Derivation:
Step-by-step trace with common compiler behavior:

  1. Initial: i=6
  2. i1 → undefined variable, typically evaluates to 0
  3. i++ → returns 6, then i becomes 7
  4. i*3 → i is now 7, so 7×3=21 (option shows 10, suggesting OCR error or typo in problem)
  5. i-- → returns 7, then i becomes 6
  6. i*i*2 → i is now 6, so 6×6×2=72 (option shows 6, another discrepancy)

Note: The provided options contain apparent OCR errors or intentional distractors. Option A (0 7 10 7 6) is selected as the 'best match' but the code exhibits undefined behavior due to unspecified evaluation order and use of an undefined variable i1. This is a flawed question.