ProgrammingHardTCS NQT · Infosys pattern
Assume a 64-bit LP64 ABI. What is printed?
Assume a 64-bit LP64 ABI. What is printed?
char str[] = "hello";
char *p = str;
while (*p != '\0') {
*p = *p - 32;
p++;
}
printf("%s", str);- A. hello (unchanged)
- B. HELLO (all uppercase)
- C. Undefined behaviour: modifying a string literal
- D. Compile-time error
Correct answer: B. HELLO (all uppercase)
char str[] = "hello"; creates a genuine, modifiable local array initialised with the string's contents (unlike char *str = "hello"; which points at a read-only string literal). Subtracting 32 from each lowercase ASCII letter converts it to uppercase; the loop walks and modifies every character until the null terminator, turning str into HELLO.
More C Programming questions
- Assume a 64-bit LP64 ABI. What happens when this runs? c char str = "hello"; str[0] = 'H'; printf("%s", str);
- Assume a 64-bit LP64 ABI. What is printed? c struct Flags { unsigned isActive : 1; unsigned isAdmin : 1; unsigned level : 6; }; struct…
- Assume a 64-bit LP64 ABI. What is printed? c union Data { int i; char c; }; union Data d; d.i = 0x12345678; printf("%x", (unsigned…
- Assume a 64-bit LP64 ABI. What is printed? c struct Point { int x, y; }; void tryModify(struct Point p) { p.x = 999; } int main(void) {…
- Assume a 64-bit LP64 ABI. What is printed? c struct Rect { struct { int x, y; } topLeft; struct { int x, y; } bottomRight; }; struct Rect…
- Assume a 64-bit LP64 ABI. What is printed? c typedef struct { int code; char message[20]; } Error; Error makeError(int c, const char msg)…