Exams › GATE › Technical
Consider the following C program: ```c #include <stdio.h> void stringcopy(char *, char *); int main(){ char a[30] = "@#Hello World!"; stringcopy(a, a + 2); printf("%s\n", a); return 0; } void stringcopy(char *s, char *t) { while (*t) *s++ = *t++; } ``` Which ONE of the following will be the output of the program?
- @#Hello World!
- Hello World!
- ello World!
- Hello World!!
Correct answer: ello World!
Solution
The function copies characters from `a + 2` into `a`, so the first two characters '@#' are overwritten by 'H' and 'e', and the remaining characters shift left. Because the source and destination overlap, the resulting string becomes 'ello World!'.
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?
- 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 __________.
- 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; } ```
⚔️ Practice GATE Technical free + battle 1v1 →