C++ Programming Questions and Answers – Checked Iterators

This section on C++ questions and puzzles focuses on “Checked Iterators”. One shall practice these questions and puzzles to improve their C++ programming skills needed for various interviews (campus interviews, walkin interviews, company interviews), placements, entrance exams and other competitive exams. These programming puzzles 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 come with detailed explanation of the answers which helps in better understanding of C++ concepts.

Here is a listing of C++ questions and puzzles on “Checked Iterators” along with answers, explanations and/or solutions:

1. What is the use of checked iterators?
a) Overwrite the bounds of your container
b) Not allow you to overwrite the bounds of your container
c) It will check the list value
d) Overwrite the bounds of your iterators
View Answer

Answer: b
Explanation: Checked iterators ensure that you do not overwrite the bounds of your container.

2. What will happen if the iterator is unchecked?
a) Arising of compiler warnings
b) Unchecked behavior on program
c) Nothing will execute
d) Arising of compiler warnings & Unchecked behavior on program
View Answer

Answer: d
Explanation: We will get unchecked behavior on calls to an unchecked function and Calls to the standard function will result in compiler warnings.

3. How many adaptors support the checked iterators?
a) 1
b) 2
c) 3
d) 4
View Answer

Answer: b
Explanation: There are two adaptors that support checked iterators. They are checked_array_iterator class, Checked_iterator class.
advertisement
advertisement

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

  1.     #include <vector>
  2.     #include <iostream>
  3.     #include <typeinfo>
  4.     #include <stdexcept>
  5.     using namespace std;
  6.     int main()
  7.     {
  8.         vector<int> vec;
  9.         vec.push_back(10);
  10.         int i = vec[100];
  11.         try {
  12.             i = vec[0];
  13.             cout << i << endl;
  14.         }
  15.         catch (exception &e)
  16.         {
  17.             cout << "Caught: " << e.what( ) << endl;
  18.             cout << "Type: " << typeid( e ).name( ) << endl;
  19.         }
  20.         catch (...) 
  21.         {
  22.             cout << "Unknown exception: " << endl;
  23.         }
  24.         return 0;
  25.     }

a) 10
b) 100
c) Exception
d) Error
View Answer

Answer: a
Explanation: In this program, We are pushing the 10th element and printing it.
Output:

$ g++ chei.cpp
$ a.out
10

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

advertisement
  1.     #include <iostream>
  2.     #include <map>
  3.     using namespace std;
  4.     int main ()
  5.     {
  6.         try {
  7.             map<char, int> mymap;
  8.             map<char, int> :: iterator it;
  9.             mymap['a'] = 50;
  10.             mymap['b'] = 100;
  11.             mymap['c'] = 150;
  12.             mymap['d'] = 200;
  13.             it = mymap.find('b');
  14.             mymap.erase (it);
  15.             mymap.erase (mymap.find('d'));
  16.             cout << mymap.find('a') -> second << '\n';
  17.         }
  18.         catch (...) 
  19.         {
  20.             cout << "Unknown exception: " << endl;
  21.         }
  22.         return 0;
  23.     }

a) 50
b) 100
c) 150
d) Exception
View Answer

Answer: a
Explanation: In this program ,We are finding the element a and printing it.
Output:

advertisement
$ g++ chei1.cpp
$ a.out
50

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

  1.     #include <iostream>
  2.     #include <iterator>
  3.     using namespace std;
  4.     int main ()
  5.     {
  6.         try {
  7.             double value1, value2;
  8.             istream_iterator<double> eos;       
  9.             istream_iterator<double> iit (cin);   
  10.             if (iit != eos) 
  11.                 value1 = *iit;
  12.             iit++;
  13.             if (iit != eos) 
  14.                 value2 = *iit;
  15.             cout << (value1 * value2) << endl;
  16.         }
  17.         catch (...) {
  18.             cout << "Unknown exception: " << endl;
  19.         }
  20.         return 0;
  21.    }

a) 10
b) 45
c) It will print the multiplied value of the input
d) Exception
View Answer

Answer: c
Explanation: In this program, We got the input by using istream iterator and we are manipulating in.
Output:

$ g++ chei2.cpp
$ a.out
3
4
12

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

  1.     #include <iostream>
  2.     #include <iterator>
  3.     #include <vector>
  4.     using namespace std;
  5.     int main () 
  6.     {
  7.         vector<int> myvector;
  8.         for (int i = 1; i < 4; ++i) 
  9.             myvector.push_back(i*10);
  10.         ostream_iterator<int> out_it (cout,", ");
  11.         copy ( myvector.begin(), myvector.end(), out_it );
  12.         return 0;
  13.     }

a) 10, 20, 30
b) 10, 20
c) 10, 20, 30,
d) 30, 15, 10
View Answer

Answer: c
Explanation: In this program, We are producing the values by vector and printing it by using ostream iterator.
Output:

$ g++ chei3.cpp
$ a.out
10, 20, 30,

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

  1.     #include <iostream>
  2.     #include <iterator>
  3.     #include <vector>
  4.     using namespace std;
  5.     int main () 
  6.     {
  7.         vector<int> myvector;
  8.         for (int i = 0; i < 10; i++) 
  9.             myvector.push_back(i);
  10.         typedef vector<int> :: iterator iter_int;
  11.         reverse_iterator<iter_int> rev_iterator;
  12.         rev_iterator = myvector.rend() - 4;
  13.         cout << *rev_iterator << endl;
  14.         return 0;
  15.     }

a) 2
b) 3
c) 4
d) 5
View Answer

Answer: b
Explanation: In this program, We are using the referencing operator and it can print the value currently pointing it.
Output:

$ g++ chei4.cpp
$ a.out
3

9. What does the checked iterator allow you to find?
a) Warnings
b) Compile time error
c) Run time error
d) Warnings & Run time error
View Answer

Answer: c
Explanation: Checked iterator allow you to find Run time error.

10. What kind of errors do checked iterators detect?
a) Uninitialized iterators
b) Initialized iterators
c) Range access
d) Both Uninitialized iterators and range access
View Answer

Answer: d
Explanation: Checked iterators can easily detect the errors in Uninitialized iterators as well as Range of access.

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.