ProgrammingHardProduct-company MCQ pattern
What is printed?
What is printed?
char s[] = "abcdef";
char *p = s + 2;
p[1] = 'X';
printf("%s %s %c %zu", s, p, *(p - 2), strlen(p + 4));- A. abcXef cXef a 0
- B. abXdef Xdef a 0
- C. abcXef cXef c 0
- D. abcXef cXef a 2
Correct answer: A. abcXef cXef a 0
p points at s[2] ('c'), so p[1] is s[3]: the array becomes "abcXef" and printing from p gives "cXef". *(p - 2) is s[0] = 'a'. p + 4 is s + 6, the terminating '\0', so strlen is 0. Writing through p is fine because s is a modifiable array, not a string literal.
More C Programming questions
- What is printed? c int f(int n) { if (n <= 0) return 0; return n % 2 ? f(n - 1) + n : f(n / 2) 2; } int main(void) { printf("%d %d"…
- What is printed? c unsigned char x = 0x1D, r = 0; for (int i = 0; i < 8; i++) { r = (r << 1) | (x & 1); x >>= 1; } printf("%#x", r);
- 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 >>…