C++ Programming Questions and Answers – Buffering

This section on C++ Multiple Choice Questions focuses on “Buffering”. One shall practice these questions 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++ questions comes with detailed explanation of the answers which helps in better understanding of C++ concepts.

Here is a listing of C++ Multiple Choice Questions & Answers focuses 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
View Answer

Answer: b
Explanation: 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
View Answer

Answer: b
Explanation: 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
View Answer

Answer: c
Explanation: 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.
advertisement
advertisement

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

  1.     #include <stdio.h>
  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
View Answer

Answer: d
Explanation: In this program, the fopen will create the file and send the text to both of the files.
Output:

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
$ 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?

advertisement
  1.     #include <stdio.h>
  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 (" %ld\n", size);
  15.         }
  16.         return 0;
  17.     }

a) 10
b) 20
c) 5
d) Depends upon the file
View Answer

Answer: d
Explanation: This program will return the size of the file in bytes.
Output:

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

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

  1.     #include <stdio.h>
  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
View Answer

Answer: c
Explanation: In this program, We are sending the text to the file by opening the file using the function freopen.
Output:

$ g++ buf2.cpp
$ a.out

This sentence is redirected to a file

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

  1.     #include <stdio.h>
  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] = '\0';
  14.         puts (buffer);
  15.         return 0;
  16.     }

a) ABCD
b) ABC
c) ABCDE
d) ADCA
View Answer

Answer: b
Explanation: 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 <stdio.h>
  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
View Answer

Answer: a
Explanation: 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
View Answer

Answer: d
Explanation: 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
View Answer

Answer: b
Explanation: There are two parameters that are used in setbuf. They are stream and buffer.

Sanfoundry Global Education & Learning Series – C++ Programming Language.

To practice all areas of C++ language, here is complete set of 1000+ Multiple Choice Questions and Answers.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.