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
Answer: b
Clarification: .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
Answer: a
Clarification: 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
Answer: a
Clarification: The iostream.h is used in the older versions of c++ and iostream is evolved from it in the std namespace.
4. What does a default header file contain?
a) prototype
b) implementation
c) declarations
d) pointing
Answer: c
Clarification: 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?
-
#include -
using namespace std;
-
int main()
-
{ -
char name[30];
-
cout << "Enter name: ";
-
gets(name);
-
cout << "Name: ";
-
puts(name);
-
return 0;
-
}
a) jobsjobs
b) jobs
c) compile time error
d) program will not run
Answer: c
Clarification: This program will run on older version of C++ with the inclusion of #include
6. setprecision requires which of the following header file?
a) stdlib.h
b) iomanip.h
c) console.h
d) conio.h
Answer: b
Clarification: The iomanip header file is used to correct the precision of the values.
7. Which of the following header file does not exist?
a)
b)
c)
d)
Answer: c
Clarification: There is no such header file
8. Which of the header file must be included to use stringstream?
a)
b)
c)
d)
Answer: b
Clarification: stringstream is available under the header file
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
Answer: b
Clarification: In this fstream.h header file is used for accessing the files only.
10. What will be the output of the following C++ code?
-
#include -
#include -
using namespace std;
-
float avg( int Count, ... )
-
{ -
va_list Numbers;
-
va_start(Numbers, Count);
-
int Sum = 0;
-
for (int i = 0; i < Count; ++i)
-
Sum += va_arg(Numbers, int);
-
va_end(Numbers);
-
return (Sum/Count);
-
} -
int main()
-
{ -
float Average = avg(10, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
-
cout << Average;
-
return 0;
-
}
a) 4
b) 5
c) 6
d) compile time error
Answer: a
Clarification: In this program, we are finding the average of first 10 numbers using stdarg header file
Output:
