File Handling Interview Questions

    Question 1File Pointer

    Which type is used for file pointers in C?

    Question 2fopen() Function

    Which is the correct syntax to open a file in C for reading?

    Question 3fopen() Error Handling

    What will this program print if data.txt does not exist?

    #include <stdio.h> int main() { FILE *fp; fp = fopen("data.txt", "r"); if (fp == NULL) printf("File not found"); else printf("File opened"); return 0; }

    Question 4fprintf() Function

    What will this program do?

    #include <stdio.h> int main() { FILE *fp = fopen("out.txt", "w"); fprintf(fp, "Hello C"); fclose(fp); return 0; }

    Question 5File Modes

    What does mode "a" do in fopen()?

    Question 6fgetc() Function

    What will be the output if input.txt contains AB?

    #include <stdio.h> int main() { FILE *fp = fopen("input.txt", "r"); char ch = fgetc(fp); printf("%c", ch); fclose(fp); return 0; }

    Question 7fputc() Function

    What will this program do?

    #include <stdio.h> int main() { FILE *fp = fopen("test.txt", "w"); fputc('Z', fp); fclose(fp); return 0; }

    Question 8EOF Constant

    What does EOF stand for in C file handling?

    Question 9fread() Function

    What does this code do?

    #include <stdio.h> struct Data { int x; float y; }; int main() { struct Data d; FILE *fp = fopen("bin.dat", "rb"); fread(&d, sizeof(d), 1, fp); fclose(fp); return 0; }

    Question 10fclose() Function

    Why should fclose() always be used after file operations?