C Language - Input/Output Questions and Answers
The C Language Input/Output questions with answers focus on concepts like printf(), scanf(), file handling, and formatted I/O operations in C programming. These programming questions and answers help you understand how data is read from and displayed to the console or files. Practicing such MCQs is crucial for C programming interviews, technical tests, and campus placements conducted by Infosys, TCS, and Wipro. Each question includes detailed solutions and code examples to clarify syntax, input validation, and buffer management—making it ideal for students preparing for technical exams.
C Language - Input/Output
Showing 10 of
54 questions
1. In a file contains the line "I am a boy\r\n" then on reading this line into the array str using fgets(). What will str contain?
- "I am a boy\r\n\0"
- "I am a boy\r\0"
- "I am a boy\n\0"
- "I am a boy"
2. What is the purpose of "rb" in fopen() function used below in the code? FILE *fp; fp = fopen("source.txt", "rb");
- open "source.txt" in binary mode for reading
- open "source.txt" in binary mode for reading and writing
- Create a new file "source.txt" for reading and writing
- None of above
3. What does fp point to in the program ? #include<stdio.h> int main() { FILE *fp; fp=fopen("trial", "r"); return 0; }
- The first character in the file
- A structure which contains a char pointer which points to the first character of a file.
- The name of the file.
- The last character in the file.
4. Which of the following operations can be performed on the file "NOTES.TXT" using the below code? FILE *fp; fp = fopen("NOTES.TXT", "r+");
- Reading
- Writing
- Appending
- Read and Write
5. To print out a and b given below, which of the following printf() statement will you use? #include<stdio.h> float a=3.14; double b=3.14;
- printf("%f %lf", a, b);
- printf("%Lf %f", a, b);
- printf("%Lf %Lf", a, b);
- printf("%f %Lf", a, b);
6. Which files will get closed through the fclose() in the following program? #include<stdio.h> int main() { FILE *fs, *ft, *fp; fp = fopen("A.C", "r"); fs = fopen("B.C", "r"); ft = fopen("C.C", "r"); fclose(fp, fs, ft); return 0; }
- "A.C" "B.C" "C.C"
- "B.C" "C.C"
- "A.C"
- Error in fclose()
7. On executing the below program what will be the contents of 'target.txt' file if the source file contains a line "To err is human"? #include<stdio.h> int main() { int i, fss; char ch, source[20] = "source.txt", target[20]="target.txt", t; FILE *fs, *ft; fs = fopen(source, "r"); ft = fopen(target, "w"); while(1) { ch=getc(fs); if(ch==EOF) break; else { fseek(fs, 4L, SEEK_CUR); fputc(ch, ft); } } return 0; }
- r n
- Trh
- err
- None of above
8. To scan a and b given below, which of the following scanf() statement will you use? #include<stdio.h> float a; double b;
- scanf("%f %f", &a, &b);
- scanf("%Lf %Lf", &a, &b);
- scanf("%f %Lf", &a, &b);
- scanf("%f %lf", &a, &b);
10. Consider the following program and what will be content of t? #include<stdio.h> int main() { FILE *fp; int t; fp = fopen("DUMMY.C", "w"); t = fileno(fp); printf("%d\n", t); return 0; }
- size of "DUMMY.C" file
- The handle associated with "DUMMY.C" file
- Garbage value
- Error in fileno()