C MCQ Questions and Answers on File Operations IO

Image
C MCQ Questions and Answers

Study C MCQ Questions and Answers on C File Operations IO - Input Output. Easily attend technical job interviews with these Multiple Choice Questions.

Go through C Theory Notes on File Handling before studying these questions.



1) What is the need for a File when you can store anything in memory.?
A) Memory (RAM) is limited in any computer.
B) A file is stored on Hard Disk which can store Gigabytes of data.
C) File stored on Hard Disk is safe even if PC is switched off. But Memory or RAM contents are cleared when PC is off.
D) All the above
Answer [=]
D
2) What is the keyword used to declare a C file pointer.?
A) file
B) FILE
C) FILEFP
D) filefp
Answer [=]
B
Explanation:

FILE *fp;

3) What is a C FILE data type.?
A) FILE is like a Structure only
B) FILE is like a Union only
C) FILE is like a user define int data type
D) None of the above
Answer [=]
A
Explanation:

Yes. FILE type pointer eg. FILE *fp holds an address of a C Structure that can store type of file operation, memory location of current read and next read and other useful information.

4) Where is a file temporarily stored before read or write operation in C language.?
A) Notepad
B) RAM
C) Hard disk
D) Buffer
Answer [=]
D
Explanation:

Yes. A Buffer is like an empty bucket that is filled with information so that direct read write operation on hard disk is avoided for better performance.

5) Choose a correct statement about C file operation program.?
int main()
{
    FILE *fp;
    char ch;
    fp=fopen("readme.txt","r");
    while((ch=fgetc(fp)) != EOF)
    {
        printf("%c",ch);
    }
}
A) FOPEN opens a file named readme.txt in Read Mode ("r).
B) EOF is End Of File. ch==EOF checks for end of file and while loop stops or exits.
C) FGETC(fp) is a function that returns one character and cursor goes to next character.
D) All the above
Answer [=]
D
Explanation:

Inside while loop, we have not done any increment operations like i++. Because fget() function takes care of returning one by one character from file.

6) What is the need for closing a file in C language.?
A) fclose(fp) closes a file to release the memory used in opening a file.
B) Closing a file clears Buffer contents from RAM or memory.
C) Unclosed files occupy memory and PC hangs when on low memory.
D) All the above
Answer [=]
D
7) If a FILE pointer is NULL what does it mean.?
FILE *fp;
fp=fopen("abc.txt","w");
A) Unable to open a file named abc.txt
B) abc.txt is not available on disk
C) Hard disk has hard ware problems.
D) All the above
Answer [=]
D


8) Choose a correct statement about FGETS in C program.?
int main()
{
    FILE *fp;
    char str[80];
    fp=fopen("readme.txt","r");
    while(fgets(str,80,fp) != NULL)
    {
        printf("%s",str);
    }
    fclose(fp);
}
A) str in fgets() is a like a user buffer that can store 80 characters each time
B) FGETS returns null if no characters are left
C) fgets() reads content from File. FPUS writes content back to File.
D) All the above
Answer [=]
D
Explanation:

Here "r" is File open mode. eg. "w" is used for writing a file.

9) Choose a correct statement about C file "R" mode operation using fopen.
fopen("abc.txt","r");
A) If the file abc.txt is found, fopen returns a FILE pointer.
B) If the file abc.txt is not found, fopen returns NULL or 0.
C) File abc.txt is only opened in Read Mode. Now write operation is performed.
D) All the above
Answer [=]
D
10) Choose a correct statement about File Write Mode "w".
File *fp;
fp=fopen("abc.txt","w");
A) If the file abc.txt is not found File abc.txt is created on disk.
B) If the file abc.txt is found, fopen() returns a FILE pointer as usual. Every time, contents of abt.txt are overwritten in "w" mode.
C) Read operation is not allowed in "w" mode.
D) All the above
Answer [=]
D
Explanation:

All the above

11) Choose a correct statement about C File Mode "w+".
FILE *p;
p=fopen("abc.txt","r+");
A) r+ mode allows reading of existing contents of file abc.txt only if file is found.
B) If file is not found, NULL is returned by fopen().
C) You can read existing contents, edit existing content and add new content.
D) All the above
Answer [=]
D
12) Choose a correct statement about C file mode "w+".
FILE *fp;
fp=fopen("abc.txt","w+");
A) Like "w" mode, "w+" mode creates a new file abc.txt if not found. NULL is only returned if some other problems in PC exist in opening a file.
B) w+ mode allows you to read the file also. Only after writing, you can read contents if any written. Before writing existing contents are emptied.
C) w+ mode always makes a file contents empty like w mode.
D) All the above
Answer [=]
D
13) Choose a correct statement about C file mode "a".
FILE *fp;
fp=fopen("abc.txt","a");
A) "a" is for append operation. You can append or add new content to the existing contents.
B) If file is not found, new file is created.
C) You can not write read file contents.
D) All the above
Answer [=]
D
14) Choose a correct statement about C file mode "a+".
FILE *fp;
fp=fopen("abc.txt","a+);
A) a+ mode always appends new data to the end of existing content
B) a+ mode creates a new file if not found or existing.
C) a+ mode allows reading also. mode "a" allows only appending not reading.
D) All the above
Answer [=]
D


15) Choose a correct statement about opening a file in binary mode for reading.
FILE *fp;
fp=fopen("abc.txt","rb");
A) rb mode opens the file in binary mode
B) Binary mode is just reading or writing in bytes instead of integers, characters or strings.
C) Binary mode saves memory occupied by contents.
D) All the above
Answer [=]
D
16) What is the syntax for writing a file in C using binary mode.?
FILE *fp;
A)
fp=fopen("abc.txt","wr");
B)
fp=fopen("abc.txt","wb");
C)
fp=fopen("abc.txt","wbin");
D)
fp=fopen("abc.txt","b");
Answer [=]
B
17) What are the C functions used to read or write a file in Text Mode.?
A) fprintf(), fscanf()
B) fread(), fwrite()
C) fprint(), fscan()
D) read(), write()
Answer [=]
A
Explanation:

You can even use fputs(). Only strings can be read or write instead of integers, float or characters.

18) What are the C functions used to read or write a file in Binary Mode.?
A) fprintf(), fscanf()
B) fread(), rwrite()
C) readf(), writef()
D) printf(), scanf()
Answer [=]
B
Explanation:

fwrite(pointer, size, count , filepointer); count=1 usually

fwrite(pointer, size, count , filepointer); 

19) What is the C function used to move current pointer to the beginning of file.?
FILE *fp;
A) rev(fp)
B) rewind(fp)
C) rew(fp)
D) wind(fp)
Answer [=]
B
Explanation:

ftell(fp) returns current position of pointer inside FILE.

fseek(fp,0,SEEK_END);
long num=ftell(fp);
//num contains the file size in bytes.
20) Choose a correct syntax for FSCANF and FPRINTF in c language.?
A) fprintf("format specifier",variables, fp); fscanf("format specifier",variables, fp);
B) fprintf(fp,count,"format specifier",variables); fscanf(fp,count,"format specifier",variables);
C) fprintf(fp,"format specifier",variables); fscanf(fp,"format specifier",variables);
D) None of the above
Answer [=]
C
Explanation:

You can check for end of file contents using EOF.

fscanf()!= EOF
fread() != EOF
fgetc() != EOF
fgets() != NULL
fseek(fp,bytescount,SEEK_SET)
moves pointer to that count location.
Example a binary file is mp3, jpg etc.