Explore topic-wise MCQs in C++ Programming.

This section includes 22 Mcqs, each offering curated multiple-choice questions to sharpen your C++ Programming knowledge and support exam preparation. Choose a topic below to get started.

1.

What is the output of this program?
#include <iostream>
int main ()
{
FILE * ptr;
long size;
ptr = fopen ("Example.txt", "rb");
if (ptr == NULL)
perror ("Error opening file");
else
{
fseek (ptr, 0, SEEK_END);
size = ftell (ptr);
fclose (ptr);
printf (" %ld n", size);
}
return 0;
}

A. Depends upon the file
B. 50
C. 100
D. 20
E. None of these
Answer» B. 50
2.

By seeing which operator thus this program stops getting the input?
#include <iostream>
#include <fstream>
using namespace std;
int main ()
{
char ch;
streambuf * pbuf;
ofstream os ("Example.txt");
pbuf = os.rdbuf();
do {
ch = cin.get();
pbuf -> sputc(ch);
} while (ch != '.');
os.close();
return 0;
}

A. $ symbol
B. dot operator
C. insertion operator
D. All of above
E. None of these
Answer» C. insertion operator
3.

What is the output of this program?
#include <iostream>
using namespace std;
int main ()
{
char First_ch, Second_ch, Third_ch;
cout << "Enter any word or number: ";
First_ch = cin.get();
cin.sync();
Second_ch = cin.get();
cin.sync();
Third_ch = cin.get();
cout << First_ch << endl;
cout << Second_ch << endl;
cout << Third_ch << endl;
return 0;
}

A. Third
B. Second
C. First
D. Returns first 3 letter or number from the entered word
E. None of these
Answer» E. None of these
4.

What is the output of this program?
#include <iostream>
#include <fstream>
using namespace std;
int main ()
{
int len;
char * buff;
ifstream is;
is.open ("Example.txt", ios :: binary );
is.seekg (0, ios :: end);
len = is.tellg();
is.seekg (0, ios :: beg);
buff = new char [len];
is.read (buff, len);
is.close();
cout.write (buff, len);
delete[] buff;
return 0;
}

A. Example.text
B. This is example
C. Example
D. Compilation Error
E. Runtime Error
Answer» F.
5.

What is the output of this program?
#include <iostream>
using namespace std;
int main ()
{
int num1 = 150;
double num2 = 6.251;
cout << num1;
cout << " ";
cout << num2 << " " << num1 * num2;
endl (cout);
return 0;
}

A. 150 6.251 937.65
B. 150 6.251
C. 937.65
D. 150
E. None of these
Answer» B. 150 6.251
6.

What is the output of this program?
#include <iostream>
#include <fstream>
using namespace std;
int main ()
{
ofstream outfile ("Sample.txt");
for (int num = 0; num < 50; num++)
{
outfile << num;
outfile.flush();
}
cout << "Done successfully";
outfile.close();
return 0;
}

A. compilation Error
B. Runtime Error
C. Garbage value
D. Done successfully
E. None of these
Answer» E. None of these
7.

What is the output of this program in text files?
#include <iostream>
int main ()
{
char buff[BUFSIZ];
FILE *ptr1, *ptr2;
ptr1 = fopen ("FirstFile.txt", "w");
ptr2 = fopen ("SecondFile.txt", "a");
setbuf ( ptr1 , buff );
fputs ("Buffered stream", ptr1);
fflush (ptr1);
setbuf ( ptr2 , NULL );
fputs ("Unbuffered stream", ptr2);
fclose (ptr1);
fclose (ptr2);
return 0;
}

A. Compilation Error
B. Buffered stream
C. Unbuffered stream
D. Buffered & Unbuffered stream
E. None of these
Answer» E. None of these
8.

What is the output of this program in the text file?
#include 
int main ()
{
FILE * ptr;
char buff[] = { 'I' , 'L' , 'U' };
ptr = fopen ( "Example.txt" , "wb" );
fwrite (buff , 1 , sizeof(buff) , ptr );
fclose (ptr);
return 0;
}

A. I
B. L
C. U
D. ILU
E. None of these
Answer» E. None of these
9.

What is the output of this program?
#include <iostream>
int main ()
{
int k;
FILE * ptr;
char buff[10];
ptr = fopen ("Sample.txt", "w+");
for ( k = 'A' ; k <= 'D' ; k++)
fputc ( k, ptr);
rewind (ptr);
fread (buff, 1, 5, ptr);
fclose (ptr);
buff[3] = ' 0';
puts (buff);
return 0;
}

A. ABC
B. EFTLM
C. ABCDE
D. ABCD
E. None of these
Answer» B. EFTLM
10.

What is the output of this program in the file?
#include <iostream>
int main ()
{
freopen ("Sample.txt", "w", stdout);
printf ("This is redirected to a file");
fclose (stdout);
return 0;
}

A. This is redirected
B. Compilation Error
C. This is redirected to a file
D. Sample.txt
E. None of these
Answer» D. Sample.txt
11.

What is the output of this program in the text file?

A. I
B. L
C. U
D. ILU
E. None of these
Answer» E. None of these
12.

What is the output of this program in text files?

A. Compilation Error
B. Buffered stream
C. Unbuffered stream
D. Buffered & Unbuffered stream
E. None of these
Answer» E. None of these
13.

What will act as a intermediate between i/o operations and physical file?

A. Stream buffer
B. Memory
C. Ram
D. All of above
E. None of these
Answer» B. Memory
14.

How many tests are available in read and write operations?

A. 4
B. 3
C. 2
D. 1
E. None of these
Answer» D. 1
15.

Which one is always faster in writing on C++?

A. Writing to memory
B. Reading from the network
C. Writing to a file
D. All of above
E. None of these
Answer» B. Reading from the network
16.

Which operator is used to insert the data into file?

A. <
B. >>
C. <<
D. All of above
E. None of these
Answer» D. All of above
17.

Which header file is used for reading and writing to a file?

A. #include <fstream>
B. #include <file>
C. #include <iostream>
D. All of above
E. None of these
Answer» B. #include <file>
18.

Which member function is used to determine whether the stream object is currently associated with a file?

A. buf
B. string
C. is_open
D. All of above
E. None of these
Answer» D. All of above
19.

By seeing which operator thus this program stops getting the input?

A. $ symbol
B. dot operator
C. insertion operator
D. All of above
E. None of these
Answer» C. insertion operator
20.

What is the output of this program in the file?

A. This is redirected
B. Compilation Error
C. This is redirected to a file
D. Sample.txt
E. None of these
Answer» D. Sample.txt
21.

How many parameters are available in the function setbuf?

A. 4
B. 3
C. 2
D. 1
E. None of these
Answer» D. 1
22.

By using which function does the buffer are automatically flushed?

A. fclose
B. compare
C. copy
D. fopen
E. None of these
Answer» B. compare