ProgrammingVery hardTCS NQT · Infosys
What is printed? Assume unsigned int is 32 bits.
What is printed? Assume unsigned int is 32 bits.
#include <stdio.h>
int main(void) {
unsigned long long n = 4294967290ULL;
unsigned b = 10;
unsigned a = (unsigned)n;
printf("%u", (a + b - 1) / b);
}- A. 429496729
- B. 0
- C. 429496730
- D. 1
Correct answer: B. 0
a + b - 1 = 4294967299, which wraps modulo 2^32 to 3, and 3 / 10 = 0. The intended ceiling division is wrong after the overflow.
More C Programming questions
- What is printed? c #include <stdio.h> int main(void) { printf("%u", 0x80000000u 3u); }
- What is printed by sizeof? c #include <stdio.h> int main(void) { char t[] = "a0b0c"; printf("%zu", sizeof t); }
- What is printed? c #include <stdio.h> int main(void) { int a[] = {10, 20, 30}; int p[] = {a, a + 1}; printf("%d", p[1] + (p + 2)); }
- What is printed? c #include <stdio.h> int main(void) { printf("%d", 1 << 30 >> 28); }
- What is printed? A sorted int array {1, 3, 5, 7} is searched with bsearch for the key 6. c #include <stdio.h> #include <stdlib.h> int…
- How many subsets of {1, 2, 3, 4, 5} sum to exactly 10? Compute with this program. c #include <stdio.h> int main(void) { int c = 0; for…