Here is a listing of C++ interview questions on “Standard Library Design” along with answers, explanations and/or solutions:
1. Pick out the wrong header file about strings.
a)
b)
c)
d)
Answer: c
Clarification: The standard header files for string is string and regex. So the wrong one presented here is ios.
2. Which is best for coding the standard library for c++?
a) no trailing underscores on names
b) complex objects are returned by value
c) have a member-swap()
d) all of the mentioned
Answer: d
Clarification: Best coding for the standard library for c++ is:
-> No trailing underscores on names
-> Complex objects are returned by value
-> It should have a member-swap().
3. What is meant by vector in the container library contains?
a) It is a sequence container that encapsulates dynamic size arrays
b) It is a sequence container that encapsulates static size arrays
c) It manages the memory
d) It manages the length and size
Answer: a
Clarification: Vector in the container library contains sequence container that manipulates and encapsulates dynamic size arrays.
4. What will be the output of the following C++ code?
-
#include -
#include -
using namespace std;
-
int main()
-
{ -
vector<int> v;
-
v.assign( 10, 42 );
-
for (int i = 0; i < v.size(); i++)
-
{ -
cout << v[i] << " ";
-
} -
}
a) 42
b) 42 42
c) 424
d) 42 for 10 times
Answer: d
Clarification: In this program, We used the vector to print the 42 for 10 times.
Output:
$ g++ std.cpp $ a.out 42 42 42 42 42 42 42 42 42 42
5. What will be the output of the following C++ code?
-
#include -
#include -
#include -
using namespace std;
-
int main()
-
{ -
queue<char> q;
-
q.push('a');
-
q.push('b');
-
q.push('c');
-
cout << q.front();
-
q.pop();
-
cout << q.front();
-
q.pop();
-
cout << q.front();
-
q.pop();
-
}
a) ab
b) abc
c) a
d) error
Answer: b
Clarification: We are using queue in this program and queue follows FIFO strategy to handle data hence the following output pattern is observed.
Output:
$ g++ std1.cpp $ a.out abc
