Practice Question and Answer
8Q: What would be the output of the following program? main() { struct emp { char *n; int age; }; struct emp e1 = { "Dravid", 23}; struct emp e2 = e1; strupr (e2.n); printf ("\n%s",e1.n); } 900 05b5cc7dde4d2b419777515d4
5b5cc7dde4d2b419777515d4- Show AnswerHide Answer
- Workspace
- SingleChoice
Answer :
Explanation :
DRAVID When a structure is assigned, passed, or returned, the copying is done monolithically. This means that the copies of any pointer fields will point to the same place as the original. In other words, anything pointed to is not copied. Hence, on changing the name through e2.n it automatically changed e1.n
Q: How would you check whether the contents of two structure variables are same or not? 1810 05b5cc7dde4d2b419777515d3
5b5cc7dde4d2b419777515d3- Show AnswerHide Answer
- Workspace
- SingleChoice
Answer :
Explanation :
struct emp { char n[20]; int age; }; main() { struct emp e1 = {"Dravid", 23}; struct emp e2; scanf ("%s %d",e2.n, & e2.age); if( structcmp (e1,e2) ==0) printf ("The structures are equal"); else printf ("The structures are unequal"); } structcmp ( struct emp x, struct emp y) { if (strcmp (x.n,y.n) ==0) if (x.age == y.age) return (0); return (1); } In short, if you nee to compare two structures, you'll have to write your own function to do so which carries out the comparison field by field.
Q: Point out the error, if any, in the following program. #include "stdio.h" main() { unsigned char; FILE *fp; fp = fopen ("trail", "r"); while (( ch = getc (fp)) ! = EOF) printf ("%c", ch); fclose (fp); } 1488 05b5cc7dde4d2b419777515d2
5b5cc7dde4d2b419777515d2- Show AnswerHide Answer
- Workspace
- SingleChoice
Answer :
Explanation :
EOF has been defined as #define EOF -1 n the file "stdio.h" and an unsigned char ranges from 0 to 255 hence when EOF is read from the file it cannot be accommodated in ch. Solution is to declare ch as an int.
Q: Point out the error, if any, in the following program. # include "stdio.h" main() { FILE *fp; char str[80]; fp = fopen ("trail", "r"); while (!feof (fp)) { fgets (str, 80, fp); puts (str); } fclose (fp); } 821 05b5cc7dde4d2b419777515d1
5b5cc7dde4d2b419777515d1- Show AnswerHide Answer
- Workspace
- SingleChoice
Answer :
Explanation :
The last line from the file "trial" would be read twice. To avoid this, ues: While ( fgets (str, 80, fp) ! = NULL) puts (str);
Q: What would be the output of the following program? /* sample.c */ main ( int argc, char **argv ) { argc = argc - (argc -1); printf ("%s", argv[argc - 1]); } 1566 05b5cc7dde4d2b419777515d0
5b5cc7dde4d2b419777515d0- Show AnswerHide Answer
- Workspace
- SingleChoice
Answer :
Explanation :
C: \SAMPLE.EXE
Q: If the following program (myprog) is run from the command line as myprog 1 2 3 what would be the output? main(int argc, char *argv[]) { int i, j = 0; for (i = 0; i < argc ; i++) j = j + atoi ( argv[i]); printf ("%d", j); } 1722 05b5cc7dde4d2b419777515cb
5b5cc7dde4d2b419777515cb- 1123false
- 26true
- 3Errorfalse
- 4"123"false
- Show AnswerHide Answer
- Workspace
- SingleChoice
Answer : 2. "6"
Explanation :
Answer: B) 6 Explanation: When atoi() tries to convert argv[0] to a number it cannot do so (argv[0] being a file name) and hence returns a zero.
Q: What would be the output of the following program ? main() { unsigned int a = oxffff; ~a; printf ("%x", a); } 1573 05b5cc7dde4d2b419777515c6
5b5cc7dde4d2b419777515c6- 1fffftrue
- 20000false
- 300fffalse
- 4None of the abovefalse
- Show AnswerHide Answer
- Workspace
- SingleChoice
Answer : 1. "ffff"
Explanation :
Answer: A) ffff Explanation:
Q: In the following code can we declare a new typedef name emp even though struct employee has not been completely defined while using typedef? < Yes / No> typedef struct employee *ptr; struct employee { char name[20]; int age; ptr next; }; 1778 05b5cc7dde4d2b419777515c5
5b5cc7dde4d2b419777515c5- Show AnswerHide Answer
- Workspace
- SingleChoice
Answer :
Explanation :
Yes

