C Programming - File I/O

12 Questions | Total Attempts: 560

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.

  • 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

  • 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

  • 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_ ?
  • 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_ ?
  • 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_ ?
  • 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_ ?
  • 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_ ?
  • 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_ ?
  • 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_ ?
  • 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_ ?
  • 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

Back to Top Back to top