ProgrammingHardHCL · Tech Mahindra pattern
Assume 32-bit int. What is printed?
Assume 32-bit int. What is printed?
int arr[3] = {1, 2, 3};
int total = 0;
int i;
for (i = 0; i < 3; i++)
total = total + arr[i];
printf("%d %d", total, i);- A. 6 2
- B. 6 3
- C. 3 3
- D. 6 0
Correct answer: B. 6 3
The loop sums arr's elements: 1+2+3=6. i is declared OUTSIDE the loop here, so it retains its value after the loop exits: the loop's own increment runs one more time after the last true iteration (i becomes 3), and then the condition i<3 fails, exiting with i still at 3.
More C Programming questions
- Assume a 64-bit LP64 ABI. What is printed? c int createZeroed(int n) { int arr = calloc(n, sizeof(int)); return arr; } int main(void) {…
- Assume 32-bit int. What is printed? c int x = 5; int y = x-- - --x; printf("%d %d", x, y);
- 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 a = 10, b = 3; printf("%d %d %f", a / b, a % b, (float)a / b); }
- What is printed? c int main(void) { char ch = 'A'; int code = ch + 1; printf("%d %c", code, (char)code); }
- What is printed? c int main(void) { int n = 17; int isPrime = 1; for (int i = 2; i i <= n; i++) { if (n % i == 0) { isPrime = 0; break; }…