C++ Programming Questions and Answers – C Input Output

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

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

Answer: b
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

Answer: c
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

Answer: c
Explanation: There are streams that are automatically created when executing a program. They are stdin, stdout and stderr.
advertisement
advertisement

4. What will be the output of the following C++ code by manipulating the text file?

  1.     #include <stdio.h>
  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
View Answer

Answer: d
Explanation: If myfile.txt exists, then it will delete the file. Else it will print an error message.
Output:

Note: Join free Sanfoundry classes at Telegram or Youtube
$ g++ out.cpp
$ a.out
Success

5. What will be the output of the following C++ code?

advertisement
  1.     #include <stdio.h>
  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 ("%d\n", n);
  19.         }
  20.         return 0;
  21.     }

a) Count of ‘$’ symbol
b) Error opening file
c) Count of ‘$’ symbol or Error opening file
d) Error
View Answer

Answer: c
Explanation: Anyone is possible – Either the file doesn’t exist or If exist, it will print the total number of ‘$’ character.
advertisement

6. What will be the output of the following C++ code in the text file?

  1.     #include <stdio.h>
  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
View Answer

Answer: c
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?

  1.     #include <stdio.h>
  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
View Answer

Answer: c
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?

  1.     #include <stdio.h>
  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 ("%d\n", 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
View Answer

Answer: c
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

Answer: b
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

Answer: a
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]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.