Exams › GATE › Technical
Consider the following C program: ```c #include <stdio.h> int main() { int a[] = {2, 4, 6, 8, 10}; int i, sum = 0, *b = a + 4; for (i = 0; i < 5; i++) sum = sum + (*b - i) - *(b - i); printf("%d\n", sum); return 0; } ``` The output of the above C program is __________.
- 0
- 2
- 4
- 6
Correct answer: 0
Solution
Here `b = a + 4`, so `*b = a[4] = 10`. For each `i`, the term becomes `(10 - i) - a[4-i]`, and the five terms are `0, 1, 2, 3, 4` minus the corresponding array values `10, 8, 6, 4, 2`, which sum to zero overall. Hence the program prints `0`.
Related GATE Technical questions
- #include <stdio.h> void foo(int *p, int x){ *p = x; } int main(){ int *z; int a = 20, b = 25; z = &a; foo(z, b); printf("%d", a); return 0; } The output of the given C program is _________.
- Choose the correct option to fill ?1 and ?2 so that the program below prints an input string in reverse order. Assume that the input string is terminated by a newline character. ```c void reverse(void){ int c; if(?1) reverse(); ?2 } main(){ printf("Enter Text"); printf("\n"); reverse(); printf("\n"); } ```
- What does the following program print? ```c #include <stdio.h> void f(int *p, int *q){ p = q; *p = 2; } int i = 0, j = 1; int main(){ f(&i, &j); printf("%d %d\n", i, j); return 0; } ```
- Consider the following program in C language: #include <stdio.h> main() { int i; int *pi = &i; scanf("%d", pi); printf("%d\n", i + 5); } Which one of the following statements is TRUE?
- What is printed by the following ANSI C program? ```c #include<stdio.h> int main(int argc, char *argv[]) { int x = 1, z[2] = {10, 11}; int *p = NULL; p = &x; *p = 10; p = &z[1]; *(&z[0] + 1) += 3; printf("%d, %d, %d\n", x, z[0], z[1]); return 0; } ```
- Consider the following C function definition. ```c int fX(char *a){ char *b = a; while (*b) b++; return b - a; } ``` Which of the following statements is/are TRUE?
⚔️ Practice GATE Technical free + battle 1v1 →