C++ Programming Questions and Answers – Standard Library Design

This section on C++ interview questions and answers focuses on “Standard Library Design”. 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 “Standard Library Design” along with answers, explanations and/or solutions:

1. Pick out the wrong header file about strings.
a) <string>
b) <regex>
c) <ios>
d) <reg>
View Answer

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

Answer: d
Explanation: 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
View Answer

Answer: a
Explanation: Vector in the container library contains sequence container that manipulates and encapsulates dynamic size arrays.
advertisement
advertisement

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

  1.     #include <iostream>
  2.     #include <vector>
  3.     using namespace std;
  4.     int main()
  5.     {
  6.         vector<int> v;
  7.         v.assign( 10, 42 );
  8.         for (int i = 0; i < v.size(); i++) 
  9.         {
  10.             cout << v[i] << " ";
  11.         }
  12.     }

a) 42
b) 42 42
c) 424
d) 42 for 10 times
View Answer

Answer: d
Explanation: 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?

advertisement
  1.     #include <iostream>
  2.     #include <list>
  3.     #include <queue>
  4.     using namespace std;
  5.     int main()
  6.     {
  7.         queue<char> q;
  8.         q.push('a');
  9.         q.push('b');
  10.         q.push('c');
  11.         cout << q.front();
  12.         q.pop();
  13.         cout << q.front();
  14.         q.pop();
  15.         cout << q.front();
  16.         q.pop();
  17.     }

a) ab
b) abc
c) a
d) error
View Answer

Answer: b
Explanation: We are using queue in this program and queue follows FIFO strategy to handle data hence the following output pattern is observed.
Output:

advertisement
$ g++ std1.cpp
$ a.out
abc

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

  1.     #include <list>
  2.     #include <string>
  3.     #include <iostream>
  4.     using namespace std ;
  5.     typedef list<string> LISTSTR; 
  6.     int main()
  7.     {
  8.         LISTSTR :: iterator i;
  9.         LISTSTR test;
  10.         test.insert(test.end(), "one");
  11.         test.insert(test.end(), "two");
  12.         LISTSTR test2(test);
  13.         LISTSTR test3(3, "three");
  14.         LISTSTR test4(++test3.begin(),
  15.         test3.end());
  16.         cout << "test:";
  17.         for (i =  test.begin(); i != test.end(); ++i)
  18.             cout << " " << *i << endl;
  19.         cout << "test:";
  20.         for (i =  test2.begin(); i != test2.end(); ++i)
  21.             cout << " " << *i << endl;
  22.         cout << "test:";
  23.         for (i =  test3.begin(); i != test3.end(); ++i)
  24.             cout << " " << *i << endl;
  25.         cout << "test:";
  26.         for (i =  test4.begin(); i != test4.end(); ++i)
  27.             cout << " " << *i << endl;
  28.     }

a) test
b) test one
c) test two
d)

 test: one
 two
test: one
 two
test: three
 three
 three
test: three
 three
View Answer
Answer: d
Explanation: In this program, We used the list to manipulate the given value.
Output:

$ g++ std3.cpp
$ a.out
test: one
 two
test: one
 two
test: three
 three
 three
test: three
 three
 
 

7. Pick out the wrong header file.
a) <algorithm>
b) <containers>
c) <iterators>
d) <process>
View Answer

Answer: d
Explanation: There is no header file named <process> in C++.

8. What is meant by standard c++ library?
a) It is the collection of class definitions for standard data structures and a collection of algorithms
b) It is a header file
c) Collection of algorithms
d) Step by step process
View Answer

Answer: a
Explanation: It is the collection of class definitions for standard data structures. This part of the library was derived from the Standard Template Library.

9. Pick out parameter for rehash method in unordered_set in c++?
a) count
b) size
c) hash
d) type
View Answer

Answer: a
Explanation: count is used to return the new number of buckets.

10. What is the use of <exception> header
a) Contains the standard exception files
b) Contains the standard library files
c) It is used to arise an exception in the program
d) Reduce the memory size
View Answer

Answer: a
Explanation: <exception> header file contains standard exception files used for exception handling in a C++ program.

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.