C Programming Questions and Answers – Line Input & Output – 1

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

Answer: d
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

Answer: b
Explanation: None.
advertisement
advertisement

3. What will be the output of the following C code?

  1.     #include <stdio.h>
  2.     #include <string.h>
  3.     int main()
  4.     {
  5.         char line[3];
  6.         fgets(line, 3, stdin);
  7.         printf("%d\n", strlen(line));
  8.         return 0;
  9.     }

a) 3
b) 1
c) Any length since line did not end with null character
d) Depends on the standard
View Answer

Answer: b
Explanation: None.

4. What will be the output of the following C code?

advertisement
  1.     #include <stdio.h>
  2.     #include <string.h>
  3.     int main()
  4.     {
  5.         char line[3];
  6.         FILE *fp;
  7.         fp = fopen("newfile.txt", "r");
  8.         while (fgets(line, 3, fp))
  9.         fputs(line, stdout);
  10.         return 0;
  11.     }

a) Compilation error
b) Infinite loop
c) Segmentation fault
d) No.of lines present in file newfile
View Answer

Answer: c
Explanation: None.
advertisement

5. What will be the output of the following C code if 2 characters is typed by the user?

  1.     #include <stdio.h>
  2.     #include <string.h>
  3.     int main()
  4.     {
  5.         char line[3];
  6.         fgets(line, 3, stdin);
  7.         printf("%d\n", line[2]);
  8.         return 0;
  9.     }

a) Compilation error
b) Undefined behaviour
c) 0
d) 10(ascii value of newline character)
View Answer

Answer: c
Explanation: None.

6. fputs() adds newline character.
a) True
b) False
c) Depends on the standard
d) Undefined behaviour
View Answer

Answer: b
Explanation: None.

7. puts() function adds newline character.
a) True
b) False
c) Depends on the standard
d) Undefined behaviour
View Answer

Answer: a
Explanation: None.

8. gets() function checks overflow run.
a) True
b) False
c) Depends on the standard
d) Undefined behaviour
View Answer

Answer: b
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

Answer: d
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.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.