Here is a listing of advanced C Objective Questions on “Line Input & Output” along with answers, explanations and/or solutions:
1. The syntax of fgets is char *fgets(char *line, int maxline, FILE *fp). Which is true for fgets?
a) Returns line on success
b) On end of file or error it returns NULL
c) Nothing
d) Both returns line on success & On end of file or error it returns NULL
Answer: d
Clarification: None.
2. fputs() function writes a string to a file that only ends with a newline.
a) True
b) False
c) Depends on the standard
d) Depends on the compiler
Answer: b
Clarification: None.
3. What will be the output of the following C code?
-
#include -
#include -
int main()
-
{ -
char line[3];
-
fgets(line, 3, stdin);
-
printf("%dn", strlen(line));
-
return 0;
-
}
a) 3
b) 1
c) Any length since line did not end with null character
d) Depends on the standard
Answer: b
Clarification: None.
4. What will be the output of the following C code?
-
#include -
#include -
int main()
-
{ -
char line[3];
-
FILE *fp;
-
fp = fopen("newfile.txt", "r");
-
while (fgets(line, 3, fp))
-
fputs(line, stdout);
-
return 0;
-
}
a) Compilation error
b) Infinite loop
c) Segmentation fault
d) No.of lines present in file newfile
Answer: c
Clarification: None.
5. What will be the output of the following C code if 2 characters is typed by the user?
-
#include -
#include -
int main()
-
{ -
char line[3];
-
fgets(line, 3, stdin);
-
printf("%dn", line[2]);
-
return 0;
-
}
a) Compilation error
b) Undefined behaviour
c) 0
d) 10(ascii value of newline character)
Answer: c
Clarification: None.
6. fputs() adds newline character.
a) True
b) False
c) Depends on the standard
d) Undefined behaviour
Answer: b
Clarification: None.
7. puts() function adds newline character.
a) True
b) False
c) Depends on the standard
d) Undefined behaviour
Answer: a
Clarification: None.
8. gets() function checks overflow run.
a) True
b) False
c) Depends on the standard
d) Undefined behaviour
Answer: b
Clarification: None.
9. puts() does the following when it writes to stdout.
a) Deletes everything
b) Adds ‘t’ to the line written
c) Deletes the terminating ‘n’
d) Adds ‘n’ to the line written
Answer: d
Clarification: None.
