You are on page 1of 22

Input / Output > General Questions

1. In a file contains the line "I am a boy\r\n" then on reading this line into the array str using fgets(). What will str contain? A."I am a boy\r\n\0" B. "I am a boy\r\0" C. "I am a boy\n\0" D."I am a boy" Answer & Explanation Answer: Option C Explanation: Declaration: char *fgets(char *s, int n, FILE *stream); fgets reads characters from stream into the string s. It stops when it reads either n - 1 characters or a newline character, whichever comes first. Therefore, the string str contain "I am a boy\n\0" View Answer Online Compiler Report Discuss in Forum 2. What is the purpose of "rb" in fopen() function used below in the code?
FILE *fp; fp = fopen("source.txt", "rb");

A.open "source.txt" in binary mode for reading B. open "source.txt" in binary mode for reading and writing C. Create a new file "source.txt" for reading and writing D.None of above Answer & Explanation Answer: Option A Explanation: The file source.txt will be opened in the binary mode. View Answer Online Compiler Report Discuss in Forum 3. What does fp point to in the program ?
#include<stdio.h> int main() { FILE *fp;

fp=fopen("trial", "r"); return 0; }

A.The first character in the file B. A structure which contains a char pointer which points to the first character of a file. C. The name of the file. D.The last character in the file. Answer & Explanation Answer: Option B Explanation: The fp is a structure which contains a char pointer which points to the first character of a file. View Answer Online Compiler Report Discuss in Forum 4. Which of the following operations can be performed on the file "NOTES.TXT" using the below code?
FILE *fp; fp = fopen("NOTES.TXT", "r+");

A.Reading C. Appending Answer & Explanation Answer: Option D Explanation:

B. Writing D.Read and Write

r+ Open an existing file for update (reading and writing). View Answer Online Compiler Report Discuss in Forum 5. To print out a and b given below, which of the following printf() statement will you use?
#include<stdio.h> float a=3.14; double b=3.14;

A.printf("%f %lf", a, b); B. printf("%Lf %f", a, b); C. printf("%Lf %Lf", a, b); D.printf("%f %Lf", a, b); Answer & Explanation Answer: Option A

Explanation: To print a float value, %f is used as format specifier. To print a double value, %lf is used as format specifier. Therefore, the answer is printf("%f %lf", a, b); View Answer Online Compiler Report Discuss in Forum

12Next >

6. Which files will get closed through the fclose() in the following program?
#include<stdio.h> int main() { FILE *fs, *ft, *fp; fp = fopen("A.C", "r"); fs = fopen("B.C", "r"); ft = fopen("C.C", "r"); fclose(fp, fs, ft); return 0; }

A."A.C" "B.C" "C.C" C. "A.C" Answer & Explanation Answer: Option D Explanation: Extra parameter in call to fclose().

B. "B.C" "C.C" D.Error in fclose()

View Answer Online Compiler Report Discuss in Forum 7. On executing the below program what will be the contents of 'target.txt' file if the source file contains a line "To err is human"?
#include<stdio.h> int main() { int i, fss; char ch, source[20] = "source.txt", target[20]="target.txt", t; FILE *fs, *ft; fs = fopen(source, "r");

ft = fopen(target, "w"); while(1) { ch=getc(fs); if(ch==EOF) break; else { fseek(fs, 4L, SEEK_CUR); fputc(ch, ft); } } return 0; }

A.r n C. err Answer & Explanation Answer: Option B Explanation:

B. Trh D.None of above

The file source.txt is opened in read mode and target.txt is opened in write mode. The file source.txt contains "To err is human". Inside the while loop, ch=getc(fs); The first character('T') of the source.txt is stored in variable ch and it's checked for EOF. if(ch==EOF) If EOF(End of file) is true, the loop breaks and program execution stops. If not EOF encountered, fseek(fs, 4L, SEEK_CUR); the file pointer advances 4 character from the current position. Hence the file pointer is in 5th character of file source.txt. fputc(ch, ft); It writes the character 'T' stored in variable ch to target.txt. The while loop runs three times and it write the character 1st and 5th and 11th characters ("Trh") in the target.txt file. View Answer Online Compiler Report Discuss in Forum 8. To scan a and b given below, which of the following scanf() statement will you use?
#include<stdio.h> float a; double b;

A.scanf("%f %f", &a, &b); C. scanf("%f %Lf", &a, &b);

B. scanf("%Lf %Lf", &a, &b); D.scanf("%f %lf", &a, &b);

Answer & Explanation Answer: Option D Explanation: To scan a float value, %f is used as format specifier. To scan a double value, %lf is used as format specifier. Therefore, the answer is scanf("%f %lf", &a, &b); View Answer Online Compiler Report Discuss in Forum 9. Out of fgets() and gets() which function is safe to use? A.gets() B.fgets() Answer & Explanation Answer: Option B Explanation: Because, In fgets() we can specify the size of the buffer into which the string supplied will be stored. View Answer Online Compiler Report Discuss in Forum 10. Consider the following program and what will be content of t?
#include<stdio.h> int main() { FILE *fp; int t; fp = fopen("DUMMY.C", "w"); t = fileno(fp); printf("%d\n", t); return 0; }

A.size of "DUMMY.C" file B. The handle associated with "DUMMY.C" file C. Garbage value D.Error in fileno() Answer & Explanation Answer: Option B

Explanation: fp = fopen("DUMMY.C", "w"); A file DUMMY.C is opened in write mode and returns the file pointer to fp t = fileno(fp); returns the handle for the fp stream and it stored in the variable t printf("%d\n", t); It prints the handle number. View Answer Online Compiler Report Discuss in Forum
Input / Output - Find Output of Program

1. What will be the content of 'file.c' after executing the following program?
#include<stdio.h> int main() { FILE *fp1, *fp2; fp1=fopen("file.c", "w"); fp2=fopen("file.c", "w"); fputc('A', fp1); fputc('B', fp2); fclose(fp1); fclose(fp2); return 0; }

A.B B B Answer & Explanation C. Answer: Option A Explanation:

B.

A B

D.Error in opening file 'file1.c'

Here fputc('A', fp1); stores 'A' in the file1.c then fputc('B', fp2); overwrites the contents of the file1.c with value 'B'. Because the fp1 and fp2 opens the file1.c in write mode. Hence the file1.c contents is 'B'. View Answer Online Compiler Report Discuss in Forum

2. What will be the output of the program ?


#include<stdio.h> int main() { int k=1; printf("%d == 1 is" "%s\n", k, k==1?"TRUE":"FALSE"); return 0; }

A.k == 1 is TRUE C. 1 == 1 is FALSE Answer & Explanation Answer: Option B Explanation:

B. 1 == 1 is TRUE D.K == 1 is FALSE

Step 1: int k=1; The variable k is declared as an integer type and initialized to '1'. Step 2: printf("%d == 1 is" "%s\n", k, k==1?"TRUE":"FALSE"); becomes => k==1?"TRUE":"FALSE" => 1==1?"TRUE":"FALSE" => "TRUE" Therefore the output of the program is 1 == 1 is TRUE View Answer Online Compiler Report Discuss in Forum 3. What will be the output of the program ?
#include<stdio.h> char *str = "char *str = %c%s%c; main(){ printf(str, 34, str, 34);}"; int main() { printf(str, 34, str, 34); return 0; }

char *str = "char *str = %c%s%c; main(){ printf(str, 34, str, 34);}"; main(){ printf(str, 34, str, 34);} B. char *str = %c%s%c; main(){ printf(str, 34, str, 34);} C. No output D.Error in program Answer & Explanation A.

Answer: Option A Explanation: No answer description available for this question. Let us discuss. View Answer Online Compiler Report Discuss in Forum 4. If the file 'source.txt' contains a line "Be my friend" which of the following will be the output of below program?
#include<stdio.h> int main() { FILE *fs, *ft; char c[10]; fs = fopen("source.txt", "r"); c[0] = getc(fs); fseek(fs, 0, SEEK_END); fseek(fs, -3L, SEEK_CUR); fgets(c, 5, fs); puts(c); return 0; }

A.friend C. end Answer & Explanation Answer: Option C Explanation: The file source.txt contains "Be my friend".

B. frien D.Error in fseek();

fseek(fs, 0, SEEK_END); moves the file pointer to the end of the file. fseek(fs, -3L, SEEK_CUR); moves the file pointer backward by 3 characters. fgets(c, 5, fs); read the file from the current position of the file pointer. Hence, it contains the last 3 characters of "Be my friend". Therefore, it prints "end". View Answer Online Compiler Report Discuss in Forum

5. What will be the output of the program ?


#include<stdio.h> int main() { float a=3.15529; printf("%2.1f\n", a); return 0; }

A.3.00 C. 3.2 Answer & Explanation Answer: Option C Explanation:

B. 3.15 D.3

float a=3.15529; The variable a is declared as an float data type and initialized to value 3.15529; printf("%2.1f\n", a); The precision specifier tells .1f tells the printf function to place only one number after the .(dot). Hence the output is 3.2 View Answer Online Compiler Report Discuss in Forum 6. What will be the output of the program ?
#include<stdio.h> int main() { printf("%c\n", ~('C'*-1)); return 0; }

A.A C. C Answer & Explanation Answer: Option B Explanation:

B. B D.D

No answer description available for this question. Let us discuss. View Answer Online Compiler Report Discuss in Forum

7. What will be the output of the program ?


#include<stdio.h> int main() { FILE *fp; unsigned char ch; /* file 'abc.c' contains "This is IndiaBIX " */ fp=fopen("abc.c", "r"); if(fp == NULL) { printf("Unable to open file"); exit(1); } while((ch=getc(fp)) != EOF) printf("%c", ch); fclose(fp); printf("\n", ch); return 0; }

A.This is IndiaBIX C. Infinite loop Answer & Explanation Answer: Option C Explanation: The macro EOF means -1.

B. This is D.Error

while((ch=getc(fp)) != EOF) Here getc function read the character and convert it to an integer value and store it in the variable ch, but it is declared as an unsigned char. So the while loop runs infinitely. View Answer Online Compiler Report Discuss in Forum 8. What will be the output of the program ?
#include<stdio.h> int main() { char *p; p="%d\n"; p++; p++; printf(p-2, 23); return 0; }

A.21

B. 23

C. Error Answer & Explanation Answer: Option B Explanation:

D.No output

No answer description available for this question. Let us discuss. View Answer Online Compiler Report Discuss in Forum 9. What will be the output of the program ?
#include<stdio.h> int main() { FILE *ptr; char i; ptr = fopen("myfile.c", "r"); while((i=fgetc(ptr))!=NULL) printf("%c", i); return 0; }

A.Print the contents of file "myfile.c" B. Print the contents of file "myfile.c" upto NULL character C. Infinite loop D.Error in program Answer & Explanation Answer: Option C Explanation: The program will generate infinite loop. When an EOF is encountered fgetc() returns EOF. Instead of checking the condition for EOF we have checked it for NULL. so the program will generate infinite loop. View Answer Online Compiler Report Discuss in Forum 10. What will be the output of the program ?
#include<stdio.h> int main() { printf("%%%%\n"); return 0; }

A.%%%%%

B. %%

C. No output Answer & Explanation Answer: Option B Explanation:

D.Error

No answer description available for this question. Let us discuss. View Answer Online Compiler Report Discuss in Forum

< Prev 123Next > 11. What will be the output of the program ?
#include<stdio.h> int main() { int a=250; printf("%1d\n", a); return 0; }

A.1250 C. 50 Answer & Explanation Answer: Option D Explanation:

B. 2 D.250

int a=250; The variable a is declared as an integer type and initialized to value 250. printf("%1d\n", a); It prints the value of variable a. Hence the output of the program is 250. View Answer Online Compiler Report Discuss in Forum 12. What will be the output of the program ?
#include<stdio.h> int main() { FILE *fp; char ch, str[7]; fp=fopen("try.c", "r"); /* file 'try.c' contains "This is Nagpur" */

fseek(fp, 9L, SEEK_CUR); fgets(str, 5, fp); puts(str); return 0; }

A.agpur C. Nagp Answer & Explanation Answer: Option D Explanation:

B. gpur D.agpu

No answer description available for this question. Let us discuss. View Answer Online Compiler Report Discuss in Forum 13. What will be the output of the program if value 25 given to scanf()?
#include<stdio.h> int main() { int i; printf("%d\n", scanf("%d", &i)); return 0; }

A.25 C. 1 Answer & Explanation Answer: Option C Explanation:

B. 2 D.5

The scanf function returns the number of input is given. printf("%d\n", scanf("%d", &i)); The scanf function returns the value 1(one). Therefore, the output of the program is '1'. View Answer Online Compiler Report Discuss in Forum

Input / Output - Point Out Errors

1. Point out the error in the program?


#include<stdio.h> #include<stdlib.h> int main() { unsigned char; FILE *fp; fp=fopen("trial", "r"); if(!fp) { printf("Unable to open file"); exit(1); } fclose(fp); return 0; }

A.Error: in unsigned char statement B. Error: unknown file pointer C. No error D.None of above Answer & Explanation Answer: Option C Explanation: This program tries to open the file trial.txt in read mode. If file not exists or unable to read it prints "Unable to open file" and then terminate the program. If file exists, it simply close the file and then terminates the program. View Answer Online Compiler Report Discuss in Forum 2. Point out the error in the program?
#include<stdio.h> int main() { char ch; int i; scanf("%c", &i); scanf("%d", &ch); printf("%c %d", ch, i); return 0; }

A.Error: suspicious char to in conversion in scanf()

B. Error: we may not get input for second scanf() statement C. No error D.None of above Answer & Explanation Answer: Option B Explanation: No answer description available for this question. Let us discuss. View Answer Online Compiler Report Discuss in Forum 3. Point out the error in the program?
#include<stdio.h> int main() { FILE *fp; fp=fopen("trial", "r"); fseek(fp, "20", SEEK_SET); fclose(fp); return 0; }

A.Error: unrecognised Keyword SEEK_SET B. Error: fseek() long offset value C. No error D.None of above Answer & Explanation Answer: Option B Explanation: Instead of "20" use 20L since fseek() need a long offset value. View Answer Online Compiler Report Discuss in Forum 4. Point out the error in the program?
#include<stdio.h> /* Assume there is a file called 'file.c' in c:\tc directory. */ int main() { FILE *fp; fp=fopen("c:\tc\file.c", "r"); if(!fp) printf("Unable to open file.");

fclose(fp); return 0; }

A.No error, No output. B. Program crashes at run time. C. Output: Unable to open file. D.None of above Answer & Explanation Answer: Option C Explanation: The path of file name must be given as "c:\\tc\file.c" View Answer Online Compiler Report Discuss in Forum 5. Point out the error/warning in the program?
#include<stdio.h> int main() { unsigned char ch; FILE *fp; fp=fopen("trial", "r"); while((ch = getc(fp))!=EOF) printf("%c", ch); fclose(fp); return 0; }

A.Error: in unsigned char declaration B. Error: while statement C. No error D.It prints all characters in file "trial" Answer & Explanation Answer: Option A Explanation: Here, EOF is -1. As 'ch' is declared as unsigned char it cannot deal with any negative value. View Answer Online Compiler Report Discuss in Forum

Input / Output - Point Out Correct Statements

1. Which of the following statement is correct about the program?


#include<stdio.h> int main() { FILE *fp; char ch; int i=1; fp = fopen("myfile.c", "r"); while((ch=getc(fp))!=EOF) { if(ch == '\n') i++; } fclose(fp); return 0; }

A.The code counts number of characters in the file B. The code counts number of words in the file C. The code counts number of blank lines in the file D.The code counts number of lines in the file Answer & Explanation Answer: Option D Explanation: This program counts the number of lines in the file myfile.c by counting the character '\n' in that file. View Answer Online Compiler Report Discuss in Forum

2. Which of the following statement is correct about the program?


#include<stdio.h> int main() { FILE *fp; char str[11], ch; int i=0; fp = fopen("INPUT.TXT", "r"); while((ch=getc(fp))!=EOF) { if(ch == '\n' || ch == ' ') { str[i]='\0'; strrev(str); printf("%s", str); i=0; } else

str[i++]=ch; } fclose(fp); return 0; }

A.The code writes a text to a file B. The code reads a text files and display its content in reverse order C. The code writes a text to a file in reverse order D.None of above Answer & Explanation Answer: Option B Explanation: This program reads the file INPUT.TXT and store it in the string str after reversing the string using strrev function. View Answer Online Compiler Report Discuss in Forum

3. Point out the correct statements about the program?


#include<stdio.h> int main() { FILE *fptr; char str[80]; fptr = fopen("f1.dat", "w"); if(fptr == NULL) printf("Cannot open file"); else { while(strlen(gets(str))>0) { fputs(str, fptr); fputs("\n", fptr); } fclose(fptr); } return 0; }

A.The code copies the content of one file to another B. The code writes strings that are read from the keyboard into a file. C. The code reads a file D.None of above Answer & Explanation Answer: Option B

Explanation: This program get the input string from the user through gets function and store it in the file f1.txt using fputs function. View Answer Online Compiler Report Discuss in Forum

Input / Output - True / False Questions

1. 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 Answer & Explanation Answer: Option B Explanation: The %s format specifier tells the compiler the given input was string of characters. View Answer Online Compiler Report Discuss in Forum 2. 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 Answer & Explanation Answer: Option A Explanation: True, each line may contain zero or more characters terminated by a newline character. View Answer Online Compiler Report Discuss in Forum 3. Offset used in fseek() function call can be a negative number. A.True B.False Answer & Explanation Answer: Option A Explanation:

True, offset in fseek() function can be a negative number. It makes the file pointer to move backwards from the current position. Declaration: retval = fseek( fp, offset, from ); Where: FILE *fp; = points to the file on which I/O is to be repositioned. long offset; = is an integer giving the number of bytes to move forward or backward in the file. This may be positive or negative. int from; = is one of the manifests SEEK_SET, SEEK_CUR, or SEEK_END. int retval; = is non-zero if the seek operation was invalid (e.g. on a file not opened with a "b" option); otherwise, the return value is zero. View Answer Online Compiler Report Discuss in Forum 4. We should not read after a write to a file without an intervening call to fflush(), fseek() or rewind() A.True B.False Answer & Explanation Answer: Option A Explanation: True, we should not be able to read a file after writing in that file without calling the below functions. int fflush ( FILE * stream ); If the given stream was open for writing and the last i/o operation was an output operation, any unwritten data in the output buffer is written to the file. int fseek ( FILE * stream, long int offset, int origin ); Its purpose is to change the file position indicator for the specified stream. void rewind ( FILE * stream ); Sets the position indicator associated with stream to the beginning of the file. View Answer Online Compiler Report Discuss in Forum 5. In a call to printf() function the format specifier %b can be used to print binary equivalent of an integer. A.True B.False Answer & Explanation

Answer: Option B Explanation: There is no format specifier named %b in c. View Answer Online Compiler Report Discuss in Forum
Input / Output - Yes / No Questions

1. stderr, stdin, stdout are FILE pointers A.Yes Answer & Explanation Answer: Option A Explanation: Yes, these will be declared like

B.No

The corresponding stdio.h variable is FILE* stdin; The corresponding stdio.h variable is FILE* stdout; The corresponding stdio.h variable is FILE* stderr; View Answer Online Compiler Report Discuss in Forum 2. A file written in text mode can be read back in binary mode. A.Yes B.No Answer & Explanation Answer: Option B Explanation: The difference is that text files contain lines (or records) of text and each of these has an endof-line marker automatically appended to the end of it whenever you indicate that you have reached the end of a line. Binary files are not broken up into separate lines or records so the end-of line marker is not written when writing to a binary file. So, we cannot read the correct the data in binary mode.

View Answer Online Compiler Report Discuss in Forum 3. Will the following program work?
#include<stdio.h> int main() { int n=5; printf("n=%*d\n", n, n); return 0; }

A.Yes Answer & Explanation Answer: Option A Explanation:


It prints n= 5

B.No

View Answer Online Compiler Report Discuss in Forum 4. Can we specify a variable filed width in a scanf() format string? A.Yes B.No Answer & Explanation Answer: Option B Explanation: In scanf() a * in a format string after a % sign is used for the suppression of assignment. That is, the current input field is scanned but not stored. View Answer Online Compiler Report Discuss in Forum

You might also like