ProgrammingHardProduct-company MCQ pattern
What is printed?
What is printed?
unsigned char x = 0x1D, r = 0;
for (int i = 0; i < 8; i++) {
r = (r << 1) | (x & 1);
x >>= 1;
}
printf("%#x", r);- A. 0xd1
- B. 0x8b
- C. 0xb1
- D. 0xb8
Correct answer: D. 0xb8
Each pass shifts r left and copies x's lowest bit in, then drops that bit from x, so the 8 bits come out in reverse order. 0x1D is 0001 1101; reversed it is 1011 1000 = 0xb8. 0xd1 just swaps the two hex digits, which is a different operation. %#x prints the 0x prefix.
More C Programming questions
- What is printed? c int a = 2, b = 3, c = 4; printf("%d %d %d", a + b c % 5 - -a, a < b < c, c > b > a);
- What is printed? c char w[] = {"red", "green", "blue"}; char p = w + 1; printf("%c %s %c %zu", p, (p + 1) + 2, ((p - 1) + 1), strlen(p[1])…
- What is printed? c struct { unsigned a : 3; unsigned b : 5; } f = {0, 0}; f.a = 9; f.b = 33; printf("%u %u ", f.a, f.b); f.a += 7; f.b =…
- What is printed? c / 32-bit unsigned int / unsigned np2(unsigned v) { v--; v |= v >> 1; v |= v >> 2; v |= v >> 4; v |= v >> 8; v |= v >>…
- What is printed? c / 8-bit unsigned char, 32-bit int / unsigned char c = 0x80; printf("%d %d %d %d", c << 1, (unsigned char)(c << 1), ~c…
- What is printed? c char s[] = "a,b,,c"; int n = 0; for (char t = strtok(s, ","); t; t = strtok(NULL, ",")) n++; printf("%d %zu %c", n…