Here is a listing of C++ quiz 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
Answer: b
Clarification: 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
Answer: c
Clarification: 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
Answer: c
Clarification: 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 -
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
Answer: d
Clarification: 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 -
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 ("%dn", n);
-
} -
return 0;
-
}
a) Count of ‘$’ symbol
b) Error opening file
c) Count of ‘$’ symbol or Error opening file
d) Error
Answer: c
Clarification: 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 -
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
Answer: c
Clarification: In this program, We are printing from A to E by using the putc function.
Output:
$ g++ out2.cpp $ a.out ABCDE
