ProgrammingModerateAccenture · Capgemini
What is printed?
What is printed?
int up(int a, int b) { return (a + b - 1) / b; }
int main(void) { printf("%d %d %d", up(10, 3), up(9, 3), up(0, 3)); }- A. 4 4 1
- B. 3 3 0
- C. 4 3 1
- D. 4 3 0
Correct answer: D. 4 3 0
(a + b − 1) / b is the integer ceiling of a / b for non-negative a and positive b: 12 / 3 = 4, 11 / 3 = 3, and 2 / 3 = 0. Plain a / b would give 3 for the first. '4 4 1' imagines it always rounds up by one.
More C Programming questions
- What is printed? c int t = 0; for (unsigned i = 0; i < 16; i++) for (unsigned x = i; x; x &= x - 1) t++; printf("%d", t);
- What is printed? c void s1(int a, int b) { int t = a; a = b; b = t; } void s2(int a, int b) { int t = a; a = b; b = t; } int main(void) {…
- Why does this fail to compile in C? c void f(int n) { static int k = n; }
- Which line correctly allocates space for a copy of a string pointed to by const char s?
- What is printed? c int a = 3, b = 0; int r = (a > b) + (a && b) 2 + (a || b) 4 + !b 8; printf("%d", r);
- What is printed? c int a[] = {5, 1, 4, 2, 3}; int lo = a, hi = a + 4; while (lo < hi) { if (lo > hi) lo++; else hi--; } printf("%td %d"…