ProgrammingHardTCS NQT · Accenture
What is printed?
What is printed?
#include <stdio.h>
void swap(int **a, int **b) {
int *t = *a;
*a = *b;
*b = t;
}
int main(void) {
int x = 1, y = 2;
int *p = &x, *q = &y;
swap(&p, &q);
printf("%d %d", *p, *q);
}- A. 2 1
- B. 1 2
- C. 2 2
- D. 1 1
Correct answer: A. 2 1
swap exchanges the pointers themselves, so p now points at y and q at x. Dereferencing gives 2 and then 1.
More C Programming questions
- What is printed? c #include <stdio.h> void rev(int a, int l, int r) { while (l < r) { int t = a[l]; a[l] = a[r]; a[r] = t; l++; r--; } }…
- What is printed? c #include <stdio.h> int main(void) { static const unsigned char t[16] = {0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4}; unsigned x =…
- How many times does the value change between neighbours in {1, 1, 2, 2, 2, 3, 1}? Compute with this program. c #include <stdio.h> int…
- How many numbers from 1 to 100 are divisible by 4 but not by 3? Compute with this program. c #include <stdio.h> int main(void) { int c =…
- What is printed by this memoised recursion? The static memo table starts with every entry zero, and calls counts each call to f. c…
- What is printed? c #include <stdio.h> int main(void) { int arr[2 3 + 1]; printf("%zu", sizeof arr / sizeof arr[0]); }