C++ Programming Questions and Answers – Header Files Usage

This section on C++ interview questions and answers focuses on “Header Files Usage”. One shall practice these interview 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++ interview questions come with detailed explanation of the answers which helps in better understanding of C++ concepts.

Here is a listing of C++ interview questions on “Header Files Usage” along with answers, explanations and/or solutions:

1. What is the user-defined header file extension in c++?
a) cpp
b) h
c) hf
d) hg
View Answer

Answer: b
Explanation: .h extensions are used for user defined header files. To include a user defined header file one should use #include”name.h” i.e. enclosed within double quotes.

2. Which of the following keyword is used to declare the header file?
a) include
b) exclude
c) string
d) namespace
View Answer

Answer: a
Explanation: The include keyword is used to include all the required things to execute the given code in the program.

3. Identify the incorrect statement.
a) iostream is a standard header and iostream.h is a non-standard header
b) iostream is a non-standard header and iostream.h is a non-standard header
c) iostream is a standard header and iostream.h is a standard header
d) iostream is a non-standard header
View Answer

Answer: a
Explanation: The iostream.h is used in the older versions of c++ and iostream is evolved from it in the std namespace.
advertisement
advertisement

4. What does a default header file contain?
a) prototype
b) implementation
c) declarations
d) pointing
View Answer

Answer: c
Explanation: In the header file, we define something that to be manipulated in the program.

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

  1.     #include <iostream>
  2.     using namespace std;
  3.     int main()
  4.     {
  5.         char name[30];
  6.         cout << "Enter name: ";
  7.         gets(name);
  8.         cout << "Name: ";
  9.         puts(name);
  10.         return 0;
  11.     }

a) jobsjobs
b) jobs
c) compile time error
d) program will not run
View Answer

Answer: c
Explanation: This program will run on older version of C++ with the inclusion of #include header file, but for on new compiler C++14 and above the gets is removed from the header file so it will not run on them even after inclusion of cstdio header file.
advertisement

6. setprecision requires which of the following header file?
a) stdlib.h
b) iomanip.h
c) console.h
d) conio.h
View Answer

Answer: b
Explanation: The iomanip header file is used to correct the precision of the values.

7. Which of the following header file does not exist?
a) <iostream>
b) <string>
c) <sstring>
d) <sstream>
View Answer

Answer: c
Explanation: There is no such header file <sstring> in C++.
advertisement

8. Which of the header file must be included to use stringstream?
a) <iostream>
b) <string>
c) <sstring>
d) <sstream>
View Answer

Answer: b
Explanation: stringstream is available under the header file <string> in C++.

9. Which of the following header files is required for creating and reading data files?
a) ofstream.h
b) fstream.h
c) ifstream.h
d) console.h
View Answer

Answer: b
Explanation: In this fstream.h header file is used for accessing the files only.

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

  1.     #include <iostream>
  2.     #include <stdarg.h>
  3.     using namespace std;
  4.     float avg( int Count, ... )
  5.     {
  6.         va_list Numbers;
  7.         va_start(Numbers, Count);
  8.         int Sum = 0;
  9.         for (int i = 0; i < Count; ++i)
  10.             Sum += va_arg(Numbers, int);
  11.         va_end(Numbers);
  12.         return (Sum/Count);
  13.     }
  14.     int main()
  15.     {
  16.         float Average = avg(10, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
  17.         cout << Average;
  18.         return 0;
  19.     }

a) 4
b) 5
c) 6
d) compile time error
View Answer

Answer: a
Explanation: In this program, we are finding the average of first 10 numbers using stdarg header file
Output:

$ g++ std.cpp
$ a.out
4

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.

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.