ProgrammingVery hardTCS NQT · Accenture
How many of these statements are true when x = 255? Count them with this program.
How many of these statements are true when x = 255? Count them with this program.
#include <stdint.h>
#include <stdio.h>
int main(void) {
uint8_t x = 255;
int c = 0;
if ((uint8_t)(x + 1) > x) c++;
if ((uint8_t)~x == 255 - x) c++;
if (x * 1 == x) c++;
printf("%d", c);
}- A. 3
- B. 1
- C. 0
- D. 2
Correct answer: D. 2
For x = 255 the first test fails because the sum wraps to 0, and the other two hold. So two of the three statements are true at this value of x.
More C Programming questions
- What is printed? c #include <stdio.h> int main(void) { unsigned a = 3; int b = -5; printf("%d", (a + b) > 0); }
- What is printed? c #include <stdio.h> #include <string.h> int main(void) { sizet n = 0; printf("%d", n - 1 > 100); }
- What is printed? c #include <stdio.h> int pal(const char s, int n) { int l = 0, r = n - 1; while (l < r) { if (s[l] != s[r]) return 0…
- What is printed? c #include <stdio.h> int main(void) { int m[3][5]; for (int i = 0; i < 3; i++) for (int j = 0; j < 5; j++) m[i][j] = i 5…
- What is printed? c #include <stdio.h> void swap(int a, int b) { int t = a; a = b; b = t; } int main(void) { int x = 1, y = 2; int p = &x…
- What is printed? c #include <stdio.h> void rev(int a, int l, int r) { while (l < r) { int t = a[l]; a[l] = a[r]; a[r] = t; l++; r--; } }…