ProgrammingHardIBM · Deloitte pattern
Assume a 64-bit LP64 ABI. What is printed?
Assume a 64-bit LP64 ABI. What is printed?
struct Inventory {
char name[16];
int quantity;
double price;
};
void restock(struct Inventory *item, int amount) {
item->quantity += amount;
}
int main(void) {
struct Inventory stock[2] = {
{"Widget", 10, 2.5},
{"Gadget", 5, 9.9}
};
restock(&stock[0], 20);
restock(&stock[1], -2);
printf("%d %d", stock[0].quantity, stock[1].quantity);
}- A. 10 5
- B. 30 3
- C. 20 -2
- D. Undefined behaviour: restock cannot be called twice
Correct answer: B. 30 3
Each restock() call modifies the specific struct it's given the address of, through item->quantity += amount. stock[0].quantity: 10+20=30. stock[1].quantity: 5+(-2)=3.
More C Programming questions
- Assume 32-bit int. What is printed? c #define SWAP(a, b, type) do { type t = a; a = b; b = t; } while (0) int main(void) { int x = 1, y =…
- Assume 32-bit int. What is printed? c int x = 5, y = 3; int result = x++ - ++y; printf("%d %d %d", x, y, result);
- What is printed? c int main(void) { int a = 1, b = 2, c = 3, d = 4; int result = (a < b) ? ((c < d) ? 1 : 2) : ((c < d) ? 3 : 4)…
- What is printed? c int main(void) { int scores[5] = {85, 92, 78, 95, 88}; int max = scores[0]; for (int i = 1; i < 5; i++) { if (scores[i]…
- Assume a 64-bit LP64 ABI. What is printed? c struct Pair { int first; int second; }; struct Pair makePair(int a, int b) { return (struct…
- What is printed? c int main(void) { int n = 6; int result = 1; for (int i = n; i > 1; i--) result = i; printf("%d", result); }