18
Saikat Banerjee 1 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" 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. 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.

Input / Output - · PDF fileInput / Output General Questions 1 ... FILE *stream); fgets reads characters from stream into the string s ... The while loop runs three times and it write

  • Upload
    lekiet

  • View
    269

  • Download
    1

Embed Size (px)

Citation preview

Page 1: Input / Output - · PDF fileInput / Output General Questions 1 ... FILE *stream); fgets reads characters from stream into the string s ... The while loop runs three times and it write

Saikat Banerjee

1

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"

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.

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.

Page 2: Input / Output - · PDF fileInput / Output General Questions 1 ... FILE *stream); fgets reads characters from stream into the string s ... The while loop runs three times and it write

Saikat Banerjee

2

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.

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 B. Writing

C. Appending D. Read and Write

Answer & Explanation

Answer: Option D

Explanation:

r+ Open an existing file for update (reading and writing).

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);

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

#include<stdio.h>

int main()

{

FILE *fs, *ft, *fp;

Page 3: Input / Output - · PDF fileInput / Output General Questions 1 ... FILE *stream); fgets reads characters from stream into the string s ... The while loop runs three times and it write

Saikat Banerjee

3

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" B. "B.C" "C.C"

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

Answer & Explanation

Answer: Option D

Explanation:

Extra parameter in call to fclose().

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 B. Trh

C. err D. None of above

Answer & Explanation

Answer: Option B

Explanation:

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.

Page 4: Input / Output - · PDF fileInput / Output General Questions 1 ... FILE *stream); fgets reads characters from stream into the string s ... The while loop runs three times and it write

Saikat Banerjee

4

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.

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); B. scanf("%Lf %Lf", &a, &b);

C. scanf("%f %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);

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.

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

Page 5: Input / Output - · PDF fileInput / Output General Questions 1 ... FILE *stream); fgets reads characters from stream into the string s ... The while loop runs three times and it write

Saikat Banerjee

5

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.

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.

A

B

C.

B

B D. Error in opening file 'file1.c'

Answer & Explanation

Answer: Option A

Explanation:

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'.

2. What will be the output of the program ?

#include<stdio.h>

int main()

{

int k=1;

Page 6: Input / Output - · PDF fileInput / Output General Questions 1 ... FILE *stream); fgets reads characters from stream into the string s ... The while loop runs three times and it write

Saikat Banerjee

6

printf("%d == 1 is" "%s\n", k, k==1?"TRUE":"FALSE");

return 0;

}

A. k == 1 is TRUE B. 1 == 1 is TRUE

C. 1 == 1 is FALSE D. K == 1 is FALSE

Answer & Explanation

Answer: Option B

Explanation:

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

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;

}

A. 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

Answer: Option A

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);

Page 7: Input / Output - · PDF fileInput / Output General Questions 1 ... FILE *stream); fgets reads characters from stream into the string s ... The while loop runs three times and it write

Saikat Banerjee

7

puts(c);

return 0;

}

A. friend B. frien

C. end D. Error in fseek();

Answer & Explanation

Answer: Option C

Explanation:

The file source.txt contains "Be my friend".

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".

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 B. 3.15

C. 3.2 D. 3

Answer & Explanation

Answer: Option C

Explanation:

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

6. What will be the output of the program ?

#include<stdio.h>

int main()

{

Page 8: Input / Output - · PDF fileInput / Output General Questions 1 ... FILE *stream); fgets reads characters from stream into the string s ... The while loop runs three times and it write

Saikat Banerjee

8

printf("%c\n", ~('C'*-1));

return 0;

}

A. A B. B

C. C D. D

Answer & Explanation

Answer: Option B

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 Saikat12 " */

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 Saikat12 B. This is

C. Infinite loop D. Error

Answer & Explanation

Answer: Option C

Explanation:

The macro EOF means -1.

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.

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;

}

Page 9: Input / Output - · PDF fileInput / Output General Questions 1 ... FILE *stream); fgets reads characters from stream into the string s ... The while loop runs three times and it write

Saikat Banerjee

9

A. 21 B. 23

C. Error D. No output

Answer & Explanation

Answer: Option B

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.

10. What will be the output of the program ?

#include<stdio.h>

int main()

{

printf("%%%%\n");

return 0;

}

A. %%%%% B. %%

C. No output D. Error

Answer & Explanation

Answer: Option B

11. What will be the output of the program ?

#include<stdio.h>

int main()

{

Page 10: Input / Output - · PDF fileInput / Output General Questions 1 ... FILE *stream); fgets reads characters from stream into the string s ... The while loop runs three times and it write

Saikat Banerjee

10

int a=250;

printf("%1d\n", a);

return 0;

}

A. 1250 B. 2

C. 50 D. 250

Answer & Explanation

Answer: Option D

Explanation:

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.

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 B. gpur

C. Nagp D. agpu

Answer & Explanation

Answer: Option D

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 B. 2

C. 1 D. 5

Answer & Explanation

Answer: Option C

Explanation:

Page 11: Input / Output - · PDF fileInput / Output General Questions 1 ... FILE *stream); fgets reads characters from stream into the string s ... The while loop runs three times and it write

Saikat Banerjee

11

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'.

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.

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

Page 12: Input / Output - · PDF fileInput / Output General Questions 1 ... FILE *stream); fgets reads characters from stream into the string s ... The while loop runs three times and it write

Saikat Banerjee

12

C. No error

D. None of above

Answer & Explanation

Answer: Option B

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.

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:

Page 13: Input / Output - · PDF fileInput / Output General Questions 1 ... FILE *stream); fgets reads characters from stream into the string s ... The while loop runs three times and it write

Saikat Banerjee

13

The path of file name must be given as "c:\\tc\file.c"

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.

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

Page 14: Input / Output - · PDF fileInput / Output General Questions 1 ... FILE *stream); fgets reads characters from stream into the string s ... The while loop runs three times and it write

Saikat Banerjee

14

Explanation:

This program counts the number of lines in the file myfile.c by counting the character '\n' in that file.

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.

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);

Page 15: Input / Output - · PDF fileInput / Output General Questions 1 ... FILE *stream); fgets reads characters from stream into the string s ... The while loop runs three times and it write

Saikat Banerjee

15

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.

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.

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.

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

Page 16: Input / Output - · PDF fileInput / Output General Questions 1 ... FILE *stream); fgets reads characters from stream into the string s ... The while loop runs three times and it write

Saikat Banerjee

16

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.

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.

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.

Yes / No Questions

1. stderr, stdin, stdout are FILE pointers

A. Yes B. No

Page 17: Input / Output - · PDF fileInput / Output General Questions 1 ... FILE *stream); fgets reads characters from stream into the string s ... The while loop runs three times and it write

Saikat Banerjee

17

Answer & Explanation

Answer: Option A

Explanation:

Yes, these will be declared like

The corresponding stdio.h variable is FILE* stdin;

The corresponding stdio.h variable is FILE* stdout;

The corresponding stdio.h variable is FILE* stderr;

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 end-of-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.

3. Will the following program work?

#include<stdio.h>

int main()

{

int n=5;

printf("n=%*d\n", n, n);

return 0;

}

A. Yes B. No

Answer & Explanation

Answer: Option A

Explanation:

It prints n= 5

4. Can we specify a variable filed width in a scanf() format string?

A. Yes B. No

Answer & Explanation

Page 18: Input / Output - · PDF fileInput / Output General Questions 1 ... FILE *stream); fgets reads characters from stream into the string s ... The while loop runs three times and it write

Saikat Banerjee

18

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.

END