250+ TOP MCQs on Buffering and Answers

This section on C++ quiz on “Buffering”. One shall practice these quizzes to improve their C++ programming skills needed for various interviews (campus interviews, walkin interviews, company interviews), placements, entrance exams and other competitive exams. These questions can be attempted by anyone focusing on learning C++ programming language. They can be a beginner, fresher, engineering graduate or an experienced IT professional. Our C++ quiz comes with detailed explanation of the answers which helps in better understanding of C++ concepts.

Here is a listing of C++ quiz on “Buffering” along with answers, explanations and/or solutions:

1. Which one is always faster in writing on C++?
a) Writing to a file
b) Writing to memory
c) Reading from the network
d) Deleting a file
Answer: b
Clarification: For the stand of file operations, writing to memory (RAM) is always faster than writing to the file on the disk directly.

2. How many tests are available in read and write operations?
a) 1
b) 2
c) 3
d) 4
Answer: b
Clarification: There are two types of read and write tests. They are throughput in random reads and throughput in contiguous reads.

3. What will act as a intermediate between i/o operations and physical file?
a) Memory
b) Ram
c) Stream buffer
d) Storage
Answer: c
Clarification: A stream buffer is a block of data that acts as intermediary between the i/o operations and the physical file associated to the stream.

4. What will be the output of the following C++ code in text files?

  1.     #include 
  2.     int main ()
  3.     {
  4.         char buffer[BUFSIZ];
  5.         FILE *p1, *p2;
  6.         p1 = fopen ("myfile.txt", "w");
  7.         p2 = fopen ("myfile2.txt", "a");
  8.         setbuf ( p1 , buffer );
  9.         fputs ("Buffered stream", p1);
  10.         fflush (p1);
  11.         setbuf ( p2 , NULL );
  12.         fputs ("Unbuffered stream", p2);
  13.         fclose (p1);
  14.         fclose (p2);
  15.         return 0;
  16.     }

a) Buffered stream
b) Unbuffered stream
c) Error
d) Buffered & Unbuffered stream
Answer: d
Clarification: In this program, the fopen will create the file and send the text to both of the files.
Output:

$ g++ buf.cpp
$ a.out
"Buffered stream" in myfile.txt
"Unbuffered stream" in myfile2.txt

5. What will be the output of the following C++ code?

  1.     #include 
  2.     int main ()
  3.     {
  4.         FILE * p;
  5.         long size;
  6.         p = fopen ("test.txt", "rb");
  7.         if (p == NULL) 
  8.             perror ("Error opening file");
  9.         else
  10.         {
  11.             fseek (p, 0, SEEK_END); 
  12.             size = ftell (p);
  13.             fclose (p);
  14.             printf (" %ldn", size);
  15.         }
  16.         return 0;
  17.     }

a) 10
b) 20
c) 5
d) Depends upon the file
Answer: d
Clarification: This program will return the size of the file in bytes.
Output:

$ g++ buf1.cpp
$ a.out
20

6. What will be the output of the following C++ code?

  1.     #include 
  2.     int main ()
  3.     {
  4.         freopen ("myfile.txt", "w", stdout);
  5.         printf ("This sentence is redirected to a file");
  6.         fclose (stdout);
  7.         return 0;
  8.     }

a) This sentence
b) This sentence is redirected
c) This sentence is redirected to a file
d) This sentence to a file
Answer: c
Clarification: In this program, We are sending the text to the file by opening the file using the function freopen.
Output:

This sentence is redirected to a file

7. What will be the output of the following C++ code?

  1.     #include 
  2.     int main ()
  3.     {
  4.         int n;
  5.         FILE * p;
  6.         char buffer [5];
  7.         p = fopen ("myfile.txt", "w+");
  8.         for ( n = 'A' ; n <= 'D' ; n++)
  9.         fputc ( n, p);
  10.         rewind (p);
  11.         fread (buffer, 1, 5, p);
  12.         fclose (p);
  13.         buffer[3] = ' ';
  14.         puts (buffer);
  15.         return 0;
  16.     }

a) ABCD
b) ABC
c) ABCDE
d) ADCA
Answer: b
Clarification: In this program, We are setting the buffer size upto 3 only, So it is printing ABC.
Output:

$ g++ buf3.cpp
$ a.out
ABC

8. What will be the output of the following C++ code in text file?

  1.     #include 
  2.     int main ()
  3.     {
  4.         FILE * p;
  5.         char buffer[] = { 'x' , 'y' , 'z' };
  6.         p = fopen ( "myfile.txt" , "wb" );
  7.         fwrite (buffer , 1 , sizeof(buffer) , p );
  8.         fclose (p);
  9.         return 0;
  10.     }

a) xyz
b) zyx
c) yxz
d) yyx
Answer: a
Clarification: In this program, We are writing into the file by using the function fwrite.
Output:

$ g++ buf4.cpp
$ a.out
xyz

9. By using which function does the buffer are automatically flushed?
a) fopen
b) copy
c) compare
d) fclose
Answer: d
Clarification: fclose() is used to close the file and flush it out of memory for safe closing of files opened during the execution of program. In short to avoid memory faults during the execution of the program.

10. How many parameters are available in the function setbuf?
a) 1
b) 2
c) 3
d) 4
Answer: b
Clarification: There are two parameters that are used in setbuf. They are stream and buffer.

  250+ TOP MCQs on Lambda Expressions and Answers

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top