ProgrammingEasyTCS NQT · Infosys pattern
What is printed?
What is printed?
char s[] = "Hello";
s[0] = 'J';
s[4] = '\0';
printf("%s %zu", s, sizeof(s));- A. Jello 6
- B. Jell 5
- C. Jello 5
- D. Jell 6
Correct answer: D. Jell 6
s is a modifiable array of 6 chars (5 letters + '\0'). Changing s[0] gives "Jello", and writing '\0' at index 4 ends the string early, so %s prints "Jell". sizeof(s) is the array size fixed at declaration, still 6; only strlen would change.
More C Programming questions
- What is printed? c int i = 5; printf("%d ", i++); printf("%d", ++i);
- What is printed? c printf("%.2f %d %d", 7 / 2.0, 7 / 2, 7 % 2);
- What is printed? c int x = 5; void f(void) { int x = 1; x++; } void g(void) { x++; } int main(void) { f(); g(); g(); printf("%d", x); }
- What is printed? c char s = "programming"; int c = 0; while (s) if (s++ == 'm') c++; printf("%d %c", c, (s - 1));
- What is printed? c unsigned x = 0xF0; / 32-bit unsigned / printf("%u %u %u %u", x >> 4, x << 1, x & 0x3C, x | 0x0F);
- What is printed? c int s = 0, i; for (i = 0; i < 10; i++) { if (i % 3) continue; s += i; } printf("%d %d", s, i);