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.

advertisement

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.
advertisement

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.

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!

advertisement
advertisement

Leave a Comment

Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn | Youtube | Instagram | Facebook | Twitter

Check his free tutorial videos on Advanced C Programming. Join his Sanfoundry-Classes telegram channel for all videos and queries on Programming, Data-Structures, Kernel, Personality development and Mentoring.