ProgrammingVery hardProduct company OA
What is printed?
What is printed?
int buf[4], head = 0, count = 0;
for (int v = 1; v <= 6; v++) {
buf[(head + count) % 4] = v;
if (count < 4) count++;
else head = (head + 1) % 4;
}
printf("%d %d", buf[head], buf[(head + count - 1) % 4]);- A. 5 6
- B. 3 6
- C. 1 4
- D. 3 4
Correct answer: B. 3 6
This ring buffer keeps the last 4 values. 1 to 4 fill it; 5 overwrites slot 0 and moves head to 1; 6 overwrites slot 1 and moves head to 2. The buffer is 5 6 3 4, with the oldest value (3) at head and the newest (6) at slot (2 + 3) % 4 = 1.
More C Programming questions
- What is printed? c int st[10], top = -1; #define PUSH(x) (st[++top] = (x)) #define POP() (st[top--]) int main(void) { PUSH(3); PUSH(5)…
- A binary tree is stored with child indices (−1 for none). What does h(0) return? c int L[] = {1, 3, -1, -1, -1}, R[] = {2, 4, -1, -1, -1}…
- With the tree below, what does in(0) print? c char key[] = "ABCDE"; int L[] = {1, 3, -1, -1, -1}, R[] = {2, 4, -1, -1, -1}; void in(int n)…
- What is wrong with this code? c char s = malloc(5); strcpy(s, "hello");
- With 4-byte int, what is printed? c int a[3] = {7, 8, 9}; int (p)[3] = &a; printf("%d %d %zu", (p)[1], p + 1, sizeof p);
- What is printed? c int c = 0, calls = 0; void comb(int start, int n, int k) { calls++; if (k == 0) { c++; return; } for (int i = start; i…