ProgrammingHardTCS NQT · Infosys
What is printed?
What is printed?
#include <stdio.h>
int main(void) {
int a[] = {1, 2, 3, 4, 5};
for (int i = 0; i + 1 < 5; i += 2) {
int t = a[i];
a[i] = a[i + 1];
a[i + 1] = t;
}
for (int i = 0; i < 5; i++) printf("%d ", a[i]);
}- A. 2 1 4 3 5
- B. 2 1 3 4 5
- C. 1 2 3 4 5
- D. 5 4 3 2 1
Correct answer: A. 2 1 4 3 5
The loop swaps the pairs (0,1) and (2,3) and stops before index 4 because i + 1 is not less than 5 there, so the last element stays put.
More C Programming questions
- What is printed? c #include <stdio.h> int f(int n) { return n <= 1 ? 0 : 1 + f(n / 2); } int main(void) { printf("%d", f(10)); }
- What is printed? Assume uint16t is 16 bits. c #include <stdint.h> #include <stdio.h> int main(void) { uint16t a = 0xFFF0, b = 0x20…
- What is printed? c #include <stdio.h> long long f(int n) { return n == 0 ? 1 : 2 f(n - 1) + 1; } int main(void) { printf("%lld", f(10)); }
- What is printed? c #include <stdio.h> int main(void) { printf("%d", 1000 >> 3); }
- What is printed? c #include <stdio.h> int main(void) { int c = 0; switch (3) { default: c += 1; case 1: c += 10; } printf("%d", c); }
- What is printed? c #include <stdio.h> int main(void) { printf("%d", 1 ? 2 : 3 ? 4 : 5); }