250+ TOP MCQs on C Input Output and Answers

This section on C++ quiz on “C Input Output”. One shall practice these quizzes 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++ quiz comes with detailed explanation of the answers which helps in better understanding of C++ concepts.

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?

  1.     #include 
  2.     int main () 
  3.     {
  4.         if (remove( "myfile.txt" ) != 0 )
  5.             perror( "Error" );
  6.         else
  7.             puts( "Success" );
  8.         return 0;
  9.     }

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?

  1.     #include 
  2.     int main ()
  3.     {
  4.         FILE * p;
  5.         int c;
  6.         int n = 0;
  7.         p = fopen ("myfile.txt", "r");
  8.         if (p == NULL) 
  9.             perror ("Error opening file");
  10.         else
  11.         {
  12.             do {
  13.                 c = getc (p);
  14.                 if (c == '$') 
  15.                     n++;
  16.             } while (c != EOF);
  17.             fclose (p);
  18.             printf ("%dn", n);
  19.         }
  20.         return 0;
  21.     }

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?

  1.     #include 
  2.     int main ()
  3.     {
  4.         FILE * pFile;
  5.         char c;
  6.         pFile = fopen("sample.txt", "wt");
  7.         for (c = 'A'; c <= 'E'; c++) 
  8.         {    
  9.             putc (c, pFile);
  10.         }
  11.         fclose (pFile);
  12.         return 0;
  13.     }

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

7. What is the name of the myfile2 file after executing the following C++ code?

  1.     #include 
  2.     int main ()
  3.     {
  4.         int result;
  5.         char oldname[] = "myfile2.txt";
  6.         char newname[] = "newname.txt";
  7.         result = rename(oldname, newname );
  8.         if (result == 0)
  9.             puts ( "success" );
  10.         else
  11.             perror( "Error" );
  12.         return 0;
  13.     }

a) name
b) new
c) newname
d) Error
Answer: c
Clarification: 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?

  1.     #include 
  2.     int main ()
  3.     {
  4.         FILE * p;
  5.         int n = 0;
  6.         p = fopen ("newname.txt", "rb");
  7.         if (p == NULL) 
  8.             perror ("Error opening file");
  9.         else
  10.         {
  11.             while (fgetc(p) != EOF)
  12.             {
  13.                 ++n;
  14.             }
  15.             if (feof(p)) 
  16.             {
  17.                 printf ("%dn", n);
  18.             }
  19.             else
  20.                 puts ("End-of-File was not reached.");
  21.             fclose (p);
  22.         }
  23.         return 0;
  24.     }

a) 10
b) 15
c) Depends on the text file
d) 34
Answer: c
Clarification: 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
Answer: b
Clarification: 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
Answer: a
Clarification: 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<

  250+ TOP MCQs on Uncaught Exceptions and Answers

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top