ProgrammingModerateProduct-company MCQ pattern
What is printed?
What is printed?
printf("%d %d %d %d", -17 / 5, -17 % 5, 17 / -5, 17 % -5);- A. -4 3 -4 -3
- B. -3 2 -3 -2
- C. -3 -2 -3 2
- D. -3 -2 -4 2
Correct answer: C. -3 -2 -3 2
Since C99, integer division truncates toward zero, and a % b has the sign of a so that (a / b)·b + a % b == a: −17 / 5 = −3, −17 % 5 = −2; 17 / −5 = −3, 17 % −5 = 2. Python's floor division gives −4, 3, −4, −3 instead.
More C Programming questions
- What is printed? c int i = 5; sizet s = sizeof(i++); printf("%zu %d", s, i); / 4-byte int /
- What is printed? c char names[] = {"Ann", "Bob", "Cy"}; printf("%c %s %zu", names[1][0], names[2], strlen(names[0]));
- What is printed? c int n = 40; / binary 101000 / printf("%d %d %d", n & (n - 1), n & -n, (n & (n - 1)) == 0);
- What is printed? c int a[] = {3, 1, 4, 1, 5}; int sum(int n) { return n == 0 ? 0 : a[n - 1] + sum(n - 1); } int main(void) { printf("%d"…
- Assume a 64-bit LP64 ABI (4-byte int, 8-byte pointers and double, natural alignment). What is printed? c int a[3][4] = {{1, 2, 3, 4}, {5…
- Assume 32-bit int and two's complement. What is printed? c unsigned char a = 0x0F; unsigned char b = ~a; printf("%d %d %d %d %zu", ~a ==…