ProgrammingHardProduct-company MCQ pattern
In C, with int i = 1, a[5];, which statement has undefined behaviour?
- A. i = i + 1;
- B. if (i++ && i++) i = 0;
- C. a[i] = i++;
- D. i++, i++;
Correct answer: C. a[i] = i++;
In a[i] = i++ the read of i inside a[i] and the side effect of i++ are unsequenced, which is undefined in C. && and the comma operator both put a sequence point between their operands, so the other two are well-defined.
More C Programming questions
- Compiled as C with 4-byte int, what is printed? c printf("%zu %zu", sizeof('A'), sizeof(char));
- What is printed? c int x = 1; { int x = 2; { x = 3; } printf("%d ", x); } printf("%d", x);
- With 4-byte int, what is printed? c int a[5]; printf("%td %td", (char )(a + 1) - (char )a, (char )(&a + 1) - (char )a);
- What is printed? c printf("%d", 010 + 0x10 + 10);
- What happens? c for (unsigned int i = 3; i >= 0; i--) printf("%u ", i);
- What is printed? c char s[] = "abc0def"; printf("%zu %zu", sizeof(s), strlen(s));