C Programming Questions and Answers – Line Input & Output
C Programming Questions and Answers – Line Input & Output
1.The syntax of fgets is char *fgets(char *line, int maxline, FILE *fp).which is true for fgets.fgets
a) returns line on success
b) On end of file or error it returns NULL.
c) Nothing
d) Both a & b
View Answer
Answer:d
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
3.What is the output of this 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
Answer:b
4.What is the output of this 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
Answer:c
5.What is the output of this C code if 2 character 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
Answer:c
6.fputs adds newline character
a) true
b) false
c) Depends on the standard
d) Undefined behaviour
View Answer
Answer:b
7.puts function adds newline character
a) true
b) false
c) Depends on the standard
d) Undefined behaviour
View Answer
Answer:a
8.gets function checks overflow run
a) true
b) false
c) Depends on the standard
d) Depends on the compiler
View Answer
Answer:b
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
10.What is the size of array “line” used in fgets(line, maxline, *fp) function?
a) maxline – 1
b) maxline
c) maxline + 1
d) Size is dynamic
View Answer
Answer:b
11.The following function
int fputs(char *line, FILE *fp)
returns EOF when:
a) ‘?’ character of array line is encountered
b) ‘n’ character in array line is encountered
c) ‘t’ character in array line is encountered
d) When an error occurs
View Answer
Answer:d
12.Identify X library function for line input and output?
#include <stdio.h>
int X(char *s, FILE *iop)
{
int c;
while (c = *s++)
putc(c, iop);
return ferror(iop) ? EOF : 0;
}
a) getc
b) putc
c) fgets
d) fputs
View Answer
Answer:d
13.Which function has a return type as char pointer?
a) getline
b) fputs
c) fgets
d) All of the mentioned
View Answer
Answer:c
14.Which of the following is the right declaration for fgets inside the library?
a) int *fgets(char *line, int maxline, FILE *fp);
b) char *fgets(char *line, int maxline, FILE *fp);
c) char *fgets(char *line, FILE *fp);
d) int *fgets(char *line, FILE *fp);
View Answer
Answer:b
15.Which is true about fputs.fputs returns?
a) EOF if an error occurs
b) Non-negative if no error
c) Both a & b
d) None of the mentioned
View Answer
Answer:c
16.gets and puts operate on
a) stdin and stdout
b) files
c) stderr
d) Nothing
View Answer
Answer:a
17.gets does the following when it reads from stdin
a) Deletes the ‘t’
b) Puts adds it.
c) Deletes the terminating ‘n’
d) Nothing
View Answer
Answer:c