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);}
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.
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 ?
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
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 ?
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.
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 <ENTER> 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 <ENTER> 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;".
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 <ENTER> 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".
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 <ENTER> 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.
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 <ENTER> 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.
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 <ENTER> 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.
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 <ENTER> 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.
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 <ENTER> 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.
12.
The text file "MLKing.txt" consists of the following text:Determine the output when the following is executed:#include <stdio.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);}
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