ProgrammingHardTCS NQT · Infosys
int a = -7, m = 5; What is ((a % m) + m) % m, the non-negative residue of a modulo m?
- A. 3
- B. -2
- C. 8
- D. 4
Correct answer: A. 3
In C, % keeps the sign of the dividend, so -7 % 5 is -2. Adding 5 gives 3, and reducing again keeps 3. Using the raw -2 is the usual mistake in competitive code.
More C Programming questions
- unsigned char c = 200; int r = (c c) >> 8; What is r?
- unsigned char c = 0xF0; printf("%d", ~c); prints what?
- int x = 7; int a = &x, b = &x; a ^= b; b ^= a; a ^= b; What is x afterwards?
- int a[] = {5, 1, 4}; int p = a; p++ += 10; printf("%d %d", a[0], p); prints what?
- struct P { int id; int score; }; An array must be sorted by score descending, with ties broken by id ascending, using qsort. Which…
- For a = {2, 4, 2, 2, 6, 4}, how many index pairs i < j satisfy a[i] == a[j]?