C Programming - File I/O

Approved & Edited by ProProfs Editorial Team
The editorial team at ProProfs Quizzes consists of a select group of subject experts, trivia writers, and quiz masters who have authored over 10,000 quizzes taken by more than 100 million users. This team includes our in-house seasoned quiz moderators and subject matter experts. Our editorial experts, spread across the world, are rigorously trained using our comprehensive guidelines to ensure that you receive the highest quality quizzes.
Learn about Our Editorial Process
| By Mr_chan
M
Mr_chan
Community Contributor
Quizzes Created: 1 | Total Attempts: 690
Questions: 12 | Attempts: 690

SettingsSettingsSettings
C Programming - File I/O - Quiz

*Please be reminded: when you answer the fill-in-the-blanks questions, do not use excessive/redundant -blanks- ,e. G.
enter:- if(a>b) ,
not if(a > b)
Good luck !


Questions and Answers
  • 1. 

     A file (name.txt) containing a list of your classmates reads as below (ALL names follow immediately with a 'new line' character):What would be the output to the screen on running the code below:#include <stdio.h>main(){  char str1[20] = "Hello, boys";  char str2[100] = "God created the world in 7 days.";  FILE *fp;  fp=fopen("name.txt","r");    if (fp==NULL){    printf("Cant open !\n");    exit(1);  }   fgets(str1,12,fp);  fgets(&str2[12],10,fp);  printf("%s",str2);  fclose(fp);} 

    • A.

      God created Ella

    • B.

      God createdElla

    • C.

      God created Hebe

    • D.

      God createdHebe

    • E.

      God created Hebeworld in 7 days.

    Correct Answer
    C. God created Hebe
    Explanation
    The fgets function reads at most one less than the number of characters specified by size from the given stream and stores them in the string str. Reading stops when a newline character is found, at end-of-file or error. The newline, if any, is retained. If any characters are read and there is no error, a '\0' character is appended to end the string.

    Rate this question:

  • 2. 

    The code below read from your keyboard input until an ENTER is pressed, convert any lower case letter to upper case, and then write it to a file:L1:  ch=getchar();    L2:  while (ch!='\n') {L3:      if (ch>='a' and ch<='z')L4:          ch=ch-'A'+'a';L5:      fputc(ch,fp); L6:      ch=getchar();L7:  }L8:  fclose(fp);Which line(s) is/are incorrect ?

    • A.

      L3,L4

    • B.

      L3,L4,L5

    • C.

      L3,L4,L7

    • D.

      L3,L4,L5,L7

    • E.

      L2,L3,L4,L5,L7

    Correct Answer
    A. L3,L4
    Explanation
    L3: Use '&&' instead of 'and' for C conditional statement.
    L4 should be ch=ch-'a'+'A';
    L7: ';' after a loop is optional

    Rate this question:

  • 3. 

    The correct code of previous question:L1:  ch=getchar();    L2:  while (ch!='\n') { L3:      if (ch>='a' && ch<='z') L4:          ch=ch-'a'+'A'; L5:      fputc(ch,fp); L6:      ch=getchar(); L7:  } L8:  fclose(fp);Right after execution of L8, a special character would be written automatically to the file (i.e. fp).  (a) What is the character ?   (b) What is the purpose of L8 ?

    • A.

      (a)'\n' (b)release the resources occupied by the program during file access, e.g. memory

    • B.

      (a)'\0' (b)release the resources occupied by the program during file access, e.g. memory

    • C.

      (a)EOF (b)release the resources occupied by the program during file access, e.g. disk storage

    • D.

      (a)'\0' (b)release the resources occupied by the program during file access, e.g. disk storage

    • E.

      (a)EOF (b)release the resources occupied by the program during file access, e.g. memory

    Correct Answer
    E. (a)EOF (b)release the resources occupied by the program during file access, e.g. memory
    Explanation
    (a) End-of-File
    (b) when a file is opened: file data from disk storage is -copied- to the RAM(memory). Memory is important resource for efficient running of the computer.

    Rate this question:

  • 4. 

    The following C code reads from your keyboard (use getchar()) and writes to a file (use fputc() function) named "myfile.txt":_A_                                          //preprocessor declarationmain(){  char ch;  _B_                                       //declare the file pointer as: fp   _C_                                       //open the file for writing  if (fp==_D_){                             //get into the "printf" statement if the file cannot be opened    printf("Cannot open the file!\n");    exit(1);  }  ch = getchar();                        //get a character from keyboard  while ( _E_ ){                          //continue processing the input if is &ltENTER> not pressed    _F_                                     // write 1 character to the file    _G_                                    // get the next character from keyboard  } _H_                                 // close the file}What would be _A_ ?

    Correct Answer
    #include
  • 5. 

    The following C code reads from your keyboard (use getchar()) and writes to a file (use fputc() function) named "myfile.txt":_A_                                          //preprocessor declarationmain(){  char ch;  _B_                                       //declare the file pointer as: fp   _C_                                       //open the file for writing  if (fp==_D_){                             //get into the "printf" statement if the file cannot be opened    printf("Cannot open the file!\n");    exit(1);  }  ch = getchar();                        //get a character from keyboard  while ( _E_ ){                          //continue processing the input if &ltENTER> is not pressed    _F_                                     // write 1 character to the file    _G_                                    // get the next character from keyboard  } _H_                                 // close the file}What would be _B_ ?

    Correct Answer
    FILE *fp;
    Explanation
    The variable declaration in C code is done by specifying the data type followed by the variable name. In this case, the variable name is "fp" and the data type is "FILE *", which indicates that "fp" is a pointer to a FILE type. Therefore, the correct answer for _B_ is "FILE *fp;".

    Rate this question:

  • 6. 

    The following C code reads from your keyboard (use getchar()) and writes to a file (use fputc() function) named "myfile.txt":_A_                                          //preprocessor declarationmain(){  char ch;  _B_                                       //declare the file pointer as: fp   _C_                                       //open the file for writing  if (fp==_D_){                             //get into the "printf" statement if the file cannot be opened    printf("Cannot open the file!\n");    exit(1);  }  ch = getchar();                        //get a character from keyboard  while ( _E_ ){                          //continue processing the input if &ltENTER> is not pressed    _F_                                     // write 1 character to the file    _G_                                    // get the next character from keyboard  } _H_                                 // close the file}What would be _C_ ?

    Correct Answer
    fp=fopen("myfile.txt","w");
    Explanation
    _C_ would be "fp=fopen("myfile.txt","w");". This line of code opens the file named "myfile.txt" in write mode and assigns the file pointer to the variable "fp".

    Rate this question:

  • 7. 

    The following C code reads from your keyboard (use getchar()) and writes to a file (use fputc() function) named "myfile.txt":_A_                                          //preprocessor declarationmain(){  char ch;  _B_                                       //declare the file pointer as: fp   _C_                                       //open the file for writing  if (fp==_D_){                             //get into the "printf" statement if the file cannot be opened    printf("Cannot open the file!\n");    exit(1);  }  ch = getchar();                        //get a character from keyboard  while ( _E_ ){                          //continue processing the input if &ltENTER> is not pressed    _F_                                     // write 1 character to the file    _G_                                    // get the next character from keyboard  } _H_                                 // close the file}What would be _D_ ?

    Correct Answer
    NULL
    Explanation
    In the given code, _D_ is the file pointer "fp". It is used to open the file for writing. If the file cannot be opened, the condition "fp==NULL" will be true, indicating that the file pointer is pointing to nothing, and the program will enter the "printf" statement to display the message "Cannot open the file!". Therefore, the correct answer for _D_ is NULL.

    Rate this question:

  • 8. 

    The following C code reads from your keyboard (use getchar()) and writes to a file (use fputc() function) named "myfile.txt":_A_                                          //preprocessor declarationmain(){  char ch;  _B_                                       //declare the file pointer as: fp   _C_                                       //open the file for writing  if (fp=_D_){                             //get into the "printf" statement if &ltENTER> the file cannot be opened    printf("Cannot open the file!\n");    exit(1);  }  ch = getchar();                        //get a character from keyboard  while ( _E_ ){                          //continue processing the input if is not pressed    _F_                                     // write 1 character to the file    _G_                                    // get the next character from keyboard  } _H_                                 // close the file}What would be _E_ ?

    Correct Answer
    ch!='\n'
    Explanation
    The condition for _E_ would be "ch!='\n'", which means that while the character read from the keyboard is not equal to a newline character, the loop will continue processing the input.

    Rate this question:

  • 9. 

    The following C code reads from your keyboard (use getchar()) and writes to a file (use fputc() function) named "myfile.txt":_A_                                          //preprocessor declarationmain(){  char ch;  _B_                                       //declare the file pointer as: fp   _C_                                       //open the file for writing  if (fp=_D_){                             //get into the "printf" statement if &ltENTER> the file cannot be opened    printf("Cannot open the file!\n");    exit(1);  }  ch = getchar();                        //get a character from keyboard  while ( _E_ ){                          //continue processing the input if is not pressed    _F_                                     // write 1 character to the file    _G_                                    // get the next character from keyboard  } _H_                                 // close the file}What would be _F_ ?

    Correct Answer
    fputc(ch,fp);
    Explanation
    The correct answer for _F_ would be "fputc(ch,fp)". This is because the fputc() function is used to write a character to the file specified by the file pointer "fp". In this code, it is used to write the character "ch" to the file.

    Rate this question:

  • 10. 

    The following C code reads from your keyboard (use getchar()) and writes to a file (use fputc() function) named "myfile.txt":_A_                                          //preprocessor declarationmain(){  char ch;  _B_                                       //declare the file pointer as: fp   _C_                                       //open the file for writing  if (fp=_D_){                             //get into the "printf" statement if the file cannot be opened    printf("Cannot open the file!\n");    exit(1);  }  ch = getchar();                        //get a character from keyboard  while ( _E_ ){                          //continue processing the input if &ltENTER> is not pressed    _F_                                     // write 1 character to the file    _G_                                    // get the next character from keyboard  } _H_                                 // close the file}What would be _G_ ?

    Correct Answer
    ch=getchar();
    Explanation
    The correct answer for _G_ would be "fputc(ch, fp)". This is because the code is using the fputc() function to write one character (stored in the variable ch) to the file pointed by the file pointer fp.

    Rate this question:

  • 11. 

    The following C code reads from your keyboard (use getchar()) and writes to a file (use fputc() function) named "myfile.txt":_A_                                          //preprocessor declarationmain(){  char ch;  _B_                                       //declare the file pointer as: fp   _C_                                       //open the file for writing  if (fp=_D_){                             //get into the "printf" statement if the file cannot be opened    printf("Cannot open the file!\n");    exit(1);  }  ch = getchar();                        //get a character from keyboard  while ( _E_ ){                          //continue processing the input if &ltENTER> is not pressed    _F_                                     // write 1 character to the file    _G_                                    // get the next character from keyboard  } _H_                                 // close the file}What would be _H_ ?

    Correct Answer
    fclose(fp);
    Explanation
    The correct answer for _H_ would be "fclose(fp)". This is because the code is closing the file that was opened for writing using the fopen function. The fclose function is used to close the file and release any resources associated with it.

    Rate this question:

  • 12. 

    The text file "MLKing.txt" consists of the following text:Determine the output when the following is executed:#include &ltstdio.h>main(){  int i;  char c;  FILE *fptr;    fptr=fopen("MLKing.txt","r");   for(i=0; i<15; i++)  {    c=fgetc(fptr);    if (i>7)      putchar(c);  }  fclose(fptr);}

    • A.

      Ace is n

    • B.

      Ce is no

    • C.

      Ace is

    • D.

      Ce is n

    • E.

      Ce is

    Correct Answer
    D. Ce is n
    Explanation
    1) at the start of text, i=0
    2) the output should be from i=8 to i=14

    Rate this question:

Quiz Review Timeline +

Our quizzes are rigorously reviewed, monitored and continuously updated by our expert board to maintain accuracy, relevance, and timeliness.

  • Current Version
  • Feb 20, 2023
    Quiz Edited by
    ProProfs Editorial Team
  • Mar 16, 2009
    Quiz Created by
    Mr_chan
Back to Top Back to top
Advertisement
×

Wait!
Here's an interesting quiz for you.

We have other quizzes matching your interest.