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
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
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
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.
4. What will be the output of the following C++ code in text files?
#include <stdio.h>
int main ()
{
char buffer[BUFSIZ];
FILE *p1, *p2;
p1 = fopen ("myfile.txt", "w");
p2 = fopen ("myfile2.txt", "a");
setbuf ( p1 , buffer );
fputs ("Buffered stream", p1);
fflush (p1);
setbuf ( p2 , NULL );
fputs ("Unbuffered stream", p2);
fclose (p1);
fclose (p2);
return 0;
}
a) Buffered stream
b) Unbuffered stream
c) Error
d) Buffered & Unbuffered stream
View Answer
Explanation: 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?
#include <stdio.h>
int main ()
{
FILE * p;
long size;
p = fopen ("test.txt", "rb");
if (p == NULL)
perror ("Error opening file");
else
{
fseek (p, 0, SEEK_END);
size = ftell (p);
fclose (p);
printf (" %ld\n", size);
}
return 0;
}
a) 10
b) 20
c) 5
d) Depends upon the file
View Answer
Explanation: 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?
#include <stdio.h>
int main ()
{
freopen ("myfile.txt", "w", stdout);
printf ("This sentence is redirected to a file");
fclose (stdout);
return 0;
}
a) This sentence
b) This sentence is redirected
c) This sentence is redirected to a file
d) This sentence to a file
View Answer
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?
#include <stdio.h>
int main ()
{
int n;
FILE * p;
char buffer [5];
p = fopen ("myfile.txt", "w+");
for ( n = 'A' ; n <= 'D' ; n++)
fputc ( n, p);
rewind (p);
fread (buffer, 1, 5, p);
fclose (p);
buffer[3] = '\0';
puts (buffer);
return 0;
}
a) ABCD
b) ABC
c) ABCDE
d) ADCA
View Answer
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?
#include <stdio.h>
int main ()
{
FILE * p;
char buffer[] = { 'x' , 'y' , 'z' };
p = fopen ( "myfile.txt" , "wb" );
fwrite (buffer , 1 , sizeof(buffer) , p );
fclose (p);
return 0;
}
a) xyz
b) zyx
c) yxz
d) yyx
View Answer
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
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
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]
- Practice Computer Science MCQs
- Practice Programming MCQs
- Apply for C++ Internship
- Check Programming Books
- Check C++ Books