ProgrammingHardTCS NQT · Infosys pattern
Assume a 64-bit LP64 ABI. What is printed?
Assume a 64-bit LP64 ABI. What is printed?
struct Pair { int a, b; };
struct Pair swapPair(struct Pair p) {
int tmp = p.a;
p.a = p.b;
p.b = tmp;
return p;
}
int main(void) {
struct Pair p1 = {1, 2};
struct Pair p2 = swapPair(p1);
printf("%d %d %d %d", p1.a, p1.b, p2.a, p2.b);
}- A. 2 1 1 2
- B. 1 2 2 1
- C. 2 1 2 1
- D. 1 2 1 2
Correct answer: B. 1 2 2 1
swapPair receives p BY VALUE (a copy of p1), swaps its own local copy's fields, and returns that modified copy; p1 itself is completely untouched (1,2), while the returned p2 holds the swapped values (2,1).
More C Programming questions
- Assume a 64-bit LP64 ABI. What is printed? c struct Employee { char name[20]; int id; float salary; }; struct Employee e1 = {"Alice", 101…
- Assume a 64-bit LP64 ABI. What is printed? c struct Point { int x, y; }; struct Point make(int a, int b) { struct Point p = {a, b}; return…
- Assume a 64-bit LP64 ABI. What is printed? c typedef struct { int x; int y; } Point; Point p1 = {1, 2}; Point p2 = {1, 2}; printf("%d"…
- Assume a 64-bit LP64 ABI. What is printed? c struct Node { int value; struct Node next; }; struct Node a = {1, NULL}; struct Node b = {2…
- Assume a 64-bit LP64 ABI. What is printed? c struct Point { int x, y; }; struct Point origin = {0, 0}; struct Point p1 = &origin; struct…
- Assume a 64-bit LP64 ABI. What is printed? c struct Config { int verbose : 1; int debug : 1; int level : 4; }; struct Config cfg = {0}…