This set of C Multiple Choice Questions & Answers (MCQs) focuses on “Line Input & Output – 1”.
Pre-requisite for this C MCQ set: Advanced C Programming Video Tutorial.
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
View Answer
Explanation: 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
View Answer
Explanation: None.
3. What will be the output of the following C code?
#include <stdio.h>
#include <string.h>
int main()
{
char line[3];
fgets(line, 3, stdin);
printf("%d\n", strlen(line));
return 0;
}
a) 3
b) 1
c) Any length since line did not end with null character
d) Depends on the standard
View Answer
Explanation: None.
4. What will be the output of the following C code?
#include <stdio.h>
#include <string.h>
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
View Answer
Explanation: None.
5. What will be the output of the following C code if 2 characters is typed by the user?
#include <stdio.h>
#include <string.h>
int main()
{
char line[3];
fgets(line, 3, stdin);
printf("%d\n", line[2]);
return 0;
}
a) Compilation error
b) Undefined behaviour
c) 0
d) 10(ascii value of newline character)
View Answer
Explanation: None.
6. fputs() adds newline character.
a) True
b) False
c) Depends on the standard
d) Undefined behaviour
View Answer
Explanation: None.
7. puts() function adds newline character.
a) True
b) False
c) Depends on the standard
d) Undefined behaviour
View Answer
Explanation: None.
8. gets() function checks overflow run.
a) True
b) False
c) Depends on the standard
d) Undefined behaviour
View Answer
Explanation: 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
View Answer
Explanation: None.
Sanfoundry Global Education & Learning Series – C Programming Language.
To practice all areas of C language, here is complete set of 1000+ Multiple Choice Questions and Answers.
- Practice BCA MCQs
- Apply for C Internship
- Check C Books
- Apply for Computer Science Internship
- Check Computer Science Books