Here is a listing of C++ Questions & Answers focuses on “C Input Output” along with answers, explanations and/or solutions:
1. Which header file is used with input and output operations of C in C++?
a) stdio.h
b) cstdio
c) iostream
d) streamio
View Answer
Explanation: Input and Output operations of c can be performed in C++ using the C Standard Input and Output Library.
2. Which will be used with physical devices to interact from C++ program?
a) Programs
b) Library
c) Streams
d) Iterators
View Answer
Explanation: C++ library uses streams to operate with physical devices such as Keyboards, Printers, Terminals or with any other type of files supported by the system.
3. How many streams are automatically created when executing a program?
a) 1
b) 2
c) 3
d) 4
View Answer
Explanation: There are streams that are automatically created when executing a program. They are stdin, stdout and stderr.
4. What will be the output of the following C++ code by manipulating the text file?
#include <stdio.h>
int main ()
{
if (remove( "myfile.txt" ) != 0 )
perror( "Error" );
else
puts( "Success" );
return 0;
}
a) Error
b) Success
c) Runtime Error
d) Can’t say
View Answer
Explanation: If myfile.txt exists, then it will delete the file. Else it will print an error message.
Output:
$ g++ out.cpp $ a.out Success
5. What will be the output of the following C++ code?
#include <stdio.h>
int main ()
{
FILE * p;
int c;
int n = 0;
p = fopen ("myfile.txt", "r");
if (p == NULL)
perror ("Error opening file");
else
{
do {
c = getc (p);
if (c == '$')
n++;
} while (c != EOF);
fclose (p);
printf ("%d\n", n);
}
return 0;
}
a) Count of ‘$’ symbol
b) Error opening file
c) Count of ‘$’ symbol or Error opening file
d) Error
View Answer
Explanation: Anyone is possible – Either the file doesn’t exist or If exist, it will print the total number of ‘$’ character.
6. What will be the output of the following C++ code in the text file?
#include <stdio.h>
int main ()
{
FILE * pFile;
char c;
pFile = fopen("sample.txt", "wt");
for (c = 'A'; c <= 'E'; c++)
{
putc (c, pFile);
}
fclose (pFile);
return 0;
}
a) ABCD
b) ABC
c) ABCDE
d) ADC
View Answer
Explanation: In this program, We are printing from A to E by using the putc function.
Output:
$ g++ out2.cpp $ a.out ABCDE
7. What is the name of the myfile2 file after executing the following C++ code?
#include <stdio.h>
int main ()
{
int result;
char oldname[] = "myfile2.txt";
char newname[] = "newname.txt";
result = rename(oldname, newname );
if (result == 0)
puts ( "success" );
else
perror( "Error" );
return 0;
}
a) name
b) new
c) newname
d) Error
View Answer
Explanation: In this program, We are renaming the myfile2 to newname by using the function rename.
Output:
myfile2.txt is renamed to newname.txt
8. How are many number of characters available in newname.txt?
#include <stdio.h>
int main ()
{
FILE * p;
int n = 0;
p = fopen ("newname.txt", "rb");
if (p == NULL)
perror ("Error opening file");
else
{
while (fgetc(p) != EOF)
{
++n;
}
if (feof(p))
{
printf ("%d\n", n);
}
else
puts ("End-of-File was not reached.");
fclose (p);
}
return 0;
}
a) 10
b) 15
c) Depends on the text file
d) 34
View Answer
Explanation: In this program, We are reading the number of characters in the program by using the function feof.
Output:
$ g++ out4.cpp $ a.out 162
9. How many indicators are available in c++?
a) 4
b) 3
c) 2
d) 1
View Answer
Explanation: There are three indicators are available in C++. They are Error indicator, End-Of-File indicator and Position indicator.
10. What is the benefit of c++ input and output over c input and output?
a) Type safety
b) Exception
c) Both Type safety & Exception
d) Sequence container
View Answer
Explanation: C++ input and output are type safety that means we don’t need to specify the type of variable we are printing.
eg:
in C we need to specify %d showing that an integer will be printed, whereas in C++ we just cout the variable.
printf(“%d”, a);
cout<<a;
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]
- Apply for Computer Science Internship
- Practice Programming MCQs
- Check Computer Science Books
- Apply for C++ Internship
- Check Programming Books