ProgrammingHardProduct-company MCQ pattern
What is printed?
What is printed?
/* 4-byte int */
struct V { int a[3]; };
void f(struct V v) { v.a[0] = 9; }
void g(int a[3]) { a[0] = 9; }
int main(void) {
struct V v = {{1, 2, 3}};
int w[3] = {1, 2, 3};
f(v);
g(w);
printf("%d %d %zu", v.a[0], w[0], sizeof(struct V));
}- A. 1 9 12
- B. 9 9 12
- C. 1 1 12
- D. 1 9 8
Correct answer: A. 1 9 12
A struct is passed by value: f receives a full copy, including the array inside it, so the caller's v.a[0] stays 1. A bare array parameter is really a pointer (int a[3] means int *a), so g writes into the caller's w. Wrapping an array in a struct is the C idiom for copying it by value; sizeof(struct V) is 12.
More C Programming questions
- What is printed? c int n = 10, c = 0; int k = (n + 3) / 4; switch (n % 4) { case 0: do { c++; case 3: c++; case 2: c++; case 1: c++; }…
- What is printed? c int calls = 0; int ways(int n) { calls++; if (n < 0) return 0; if (n == 0) return 1; return ways(n - 1) + ways(n - 3) +…
- What is printed? c int moves = 0; void h(int n, char from, char via, char to) { if (n == 0) return; h(n - 1, from, to, via); if (++moves…
- What is printed? c for (unsigned n = 5; n < 8; n++) printf("%u ", n ^ (n >> 1)); unsigned g = 13, b = 0; for (; g; g >>= 1) b ^= g…
- What is printed? c int walk(int n) { static int calls = 0; calls++; if (n > 1) { walk(n / 2); walk(n / 3); } return calls; } int…
- What is printed? c int calls = 0; int A(int m, int n) { calls++; if (m == 0) return n + 1; if (n == 0) return A(m - 1, 1); return A(m - 1…