ProgrammingHardAccenture · Cognizant pattern
What is printed?
What is printed?
char s[] = "abcdef";
char *p = s, *q = s + strlen(s) - 1;
while (p < q)
*p++ = *q--;
printf("%s", s);- A. fedcba
- B. fedabc
- C. feddef
- D. abccba
Correct answer: C. feddef
There is no temporary, so this copies instead of swapping: s[0] = 'f', s[1] = 'e', s[2] = 'd', then p = 3 and q = 2 stop the loop. The back half keeps d e f, giving feddef.
More C Programming questions
- What is printed? c struct A { int a[2]; }; struct B { int p; }; int v = 5; struct A x = {{1, 2}}, y; struct B m = {&v}, n; y = x; n = m…
- With 4-byte int and 8-byte double aligned on 8 bytes, what is sizeof(union U)? c union U { int i; double d; char c[10]; };
- What is printed? c int nextid(void) { static int id = 0; return ++id; } int main(void) { int a = nextid(), b = nextid(), c = nextid()…
- What is printed? c int x = 2; switch (x) { case 1: printf("1"); case 2: printf("2"); case 3: printf("3"); break; default: printf("D"); }
- What is printed? c if (-1 < 1u) printf("yes"); else printf("no");
- What is printed? c int a = 0, b = 0; int x = (++a > 5) && (++b > 0); int y = (++a > 0) || (++b > 0); printf("%d %d %d %d", a, b, x, y);