C MCQ Questions - File Handling

This section focuses on the "File Handling" in C programming. These C MCQ Questions should be practiced to improve the C programming skills required for various interviews (campus interview, walk-in interview, company interview), placement, entrance exam and other competitive examinations.

1. Which of the following true about FILE *fp

A. FILE is a keyword in C for representing files and fp is a variable of FILE type.
B. FILE is a stream
C. FILE is a buffered stream
D. FILE is a structure and fp is a pointer to the structure of FILE type

View Answer


2. Which of the following mode argument is used to truncate?

A. a
B. w
C. f
D. t

View Answer


3. The first and second arguments of fopen() are

A. A character string containing the name of the file & the second argument is the mode
B. A character string containing the name of the user & the second argument is the mode
C. A character string containing file pointer & the second argument is the mode
D. None of the mentioned

View Answer


4. FILE is of type ______

A. int type
B. char * type
C. struct type
D. None of the mentioned

View Answer


5. fseek() should be preferred over rewind() mainly because

A. rewind() doesn't work for empty files
B. rewind() may fail for large files
C. In rewind, there is no way to check if the operations completed successfully
D. All of the above

View Answer


6. FILE reserved word is?

A. A structure tag declared in stdio.h
B. One of the basic datatypes in c
C. Pointer to the structure defined in stdio.h
D. It is a type name defined in stdio.h

View Answer


7. For binary files, a ___ must be appended to the mode string.

A. "b"
B. "B"
C. "binary"
D. "01"

View Answer


8. Which of the following statements about stdout and stderr are true?

A. Same
B. Both connected to screen always.
C. Both connected to screen by default.
D. stdout is line buffered but stderr is unbuffered.

View Answer


9. Which type of files can't be opened using fopen()?

A. .txt
B. .bin
C. .c
D. None of the above

View Answer


10. When a C program is started, O.S environment is responsible for opening file and providing pointer for that file?

A. Standard input
B. Standard output
C. Standard error
D. All of the above

View Answer


11. If there is any error while opening a file, fopen will return?

A. Nothing
B. EOF
C. NULL
D. Depends on compiler

View Answer


12. It is not possible to combine two or more file opening mode in open () method.

A. TRUE
B. FALSE
C. May Be
D. Can't Say

View Answer


13. What is the return value of putchar()?

A. The character written
B. EOF if an error occurs
C. Nothing
D. Both character written & EOF if an error occurs

View Answer


14. Which is true?

A. The symbolic constant EOF is defined in
B. The value is -1
C. The symbolic constant EOF is defined in & value is -1
D. Only value is -1

View Answer


15. What is the purpose of "rb" in fopen() function used below in the code? FILE *fp; fp = fopen("demo.txt", "rb");

A. Open "demo.txt" in binary mode for reading
B. Create a new file "demo.txt" for reading and writing
C. Open "demo.txt" in binary mode for reading and writing
D. None of the above

View Answer


16. Which files will get closed through the fclose() in the following program?

void main()
{
       FILE *fp, *ft;
       fp = fopen("a.txt", "r");
       ft = fopen("b.txt", "r");
       fclose(fp,ft);
}

A. a, b
B. a
C. b
D. Error in fclose

View Answer


17. When fopen() is not able to open a file, it returns

A. EOF
B. NULL
C. Run-time Error
D. None of the above

View Answer


18. getc() returns EOF when

A. When getc() fail to read the character
B. When end of file is reached
C. Both A and B
D. None of the above

View Answer


19. What is the output of this program?

#include <stdio.h>
int main(){
    FILE *fp;
    char *str;
    fp=fopen("demo.txt","r");// demo.txt :you are a good programmer
    while(fgets(str,6,fp)!=NULL)
    puts(str);
    fclose(fp);
    return 0;
}

A. you are a good programmer
B. e a good programmer
C. you ar
D. you are

View Answer


20. What is the output of this program?

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

A. It will print the content of file demo.txt
B. It will print the content of file till it encouter new line character
C. Compilation Error
D. None of the above

View Answer


21. What is the output of this program?

#include <stdio.h>
int main(){
    char c;
    FILE *fp;
    fp=fopen("demo.txt","a+"); // demo.txt : hello you are reading a file
    fprintf(fp," demo");    
	fclose(fp);
    fp=fopen("myfile.txt","r");
    
    while((c=fgetc(fp))!=EOF)
         printf("%c",c);
    fclose(fp);
    return 0;
}

A. hello you are reading a file
B. hello you are reading a file demo
C. demo
D. None of the above

View Answer


22. A data of the file is stored in?

A. Ram
B. Hard disk
C. Rom
D. None

View Answer


23. Select a function which is used to write a string to a file?

A. pits()
B. putc()
C. fputs()
D. fgets()

View Answer


24. Select text file in which data is stored in

A. ASC11 code
B. Binary code
C. Octal code
D. text code

View Answer


25. We should not read after a write to a file without an intervening call to fflush(), fseek() or rewind()

A. TRUE
B. FALSE
C. May Be
D. Can't Say

View Answer


26. Offset used in fseek() function call can be a negative number.

A. TRUE
B. FALSE
C. May Be
D. Can't Say

View Answer


27. Will the following program work?

#include <stdio.h>
int main()
{
    int i=3;
    printf("i=%*d", i, i);
    return 0;
}

A. Yes
B. No
C. May Be
D. Can't Say

View Answer


28. stderr, stdin, stdout are FILE pointers

A. Yes
B. No
C. May Be
D. Can't Say

View Answer


29. which Is data type of file pointer is?

A. int
B. double
C. void
D. File

View Answer


30.  In fopen(), the open mode "wx" is sometimes preferred "w" because.
1) Use of wx is more efficient.
2) If w is used, old contents of file are erased and a new empty file is created. When wx is used, fopen() returns NULL if file already exists.

A. Only 1
B. Only 2
C. Both 1 and 2
D. Neither 1 nor 2

View Answer


31. A file written in text mode can be read back in binary mode.

A. Yes
B. No
C. May Be
D. Can't Say

View Answer


32. A file written in text mode can be read back in binary mode.

A. Yes
B. No
C. May Be
D. Can't Say

View Answer


33. A text stream is an ordered sequence of characters composed into lines, each line consisting of zero or more characters plus a terminating new-line character.

A. TRUE
B. FALSE
C. May Be
D. Can't Say

View Answer


34. While calling the fprintf() function in the format string conversion specifier %s can be used to write a character string in capital letters.

A. TRUE
B. FALSE
C. May Be
D. Can't Say

View Answer


35. Select text file in which number will take.

A. 2 bytes
B. 4 bytes
C. 3 bytes
D. 8bytes

View Answer


36. Which is true about getc.getc returns?

A. The next character from the stream referred to by file pointer
B. EOF for end of file or error
C. Both a & b
D. Nothing

View Answer


37. What is the meant by 'a' in the following operation?

fp = fopen("letsfindcourse.txt", "a");

A. Attach
B. Append
C. Apprehend
D. Add

View Answer


38. What does the following segment of code do? fprintf(fp, "Copying!");

A. It writes "Copying!" into the file pointed by fp
B. It reads "Copying!" from the file and prints on display
C. It writes as well as reads "Copying!" to and from the file and prints it
D. None of the mentioned

View Answer


39. _____removes the named file, so that a subsequent attempt to open it will fail.

A. remove(const *filename)
B. remove(filename)
C. remove()
D. fclose(filename)

View Answer


40. EOF is an integer type defined in stdio. hand has a value ____________

A. 1
B. 0
C. NULL
D. -1

View Answer






Also check :


Discussion



* You must be logged in to add comment.