Free CPP Practice Test Questions and Answers (2026) | Cert Empire Practice Questions

Free preview: 20 questions.

Already purchased? Log in

C++ Institute CPP

View Mode
Q: 1
What happens when you attempt to compile and run the following code? #include #include #include using namespace std; int main(){ int t[] ={ 3, 4, 2, 1, 6, 5, 7, 9, 8, 0 }; multiset s1(t,t+10); s1.insert(s1.find(7), 3); for(multiset::iterator i=s1.begin();i!= s1.end(); i++) { cout<<*i<<" "; } return 0; }
Options
Q: 2
What happens when you attempt to compile and run the following code? #include #include #include #include using namespace std; templatestruct Out { ostream & out; Out(ostream & o): out(o){} void operator() (const T & val ) { out<<val<<" "; } }; struct Add : public binary_function { int operator() (const int & a, const int & b) const { return a+b; } }; int main() { int t[]={1,2,3,4,5,6,7,8,9,10}; deque d1(t, t+10); deque d2(10); transform(d1.begin(), d1.end(), d2.begin(), bind2nd(Add(), 1)); for_each(d2.rbegin(), d2.rend(), Out(cout));cout<<endl; return 0; } Program outputs:
Options
Q: 3
What happens when you attempt to compile and run the following code? #include using namespace std; template void f(A a) { cout<<1<<endl; } void f(int a) { cout<<2<<endl; } int main() { int a = 1; f(a); return 0; }
Options
Q: 4
What will be output of the program when you attempt to compile and run the following code? #include #include #include #include using namespace std; int main(){ int second[] ={ 3, 4, 2, 1, 6, 5, 7, 9, 8, 0 }; string first[] = {"three", "four", "two", "one", "six","five", "seven", "nine","eight","zero"}; multimap m; for(int i=0; i<10; i++) { m.insert(pair(second[i],first[i])); } m[0]="ten"; m.insert(pair(1,"eleven")); for(multimap::iterator i=m.begin();i!= m.end(); i++) { cout<second<<" "; } return 0; }
Options
Q: 5
What happens when you attempt to compile and run the following code? #include #include #include #include using namespace std; int main() { int t[] = {1,2,3,2,3,5,1,2,7,3,2,1,10, 4,4,5}; vector v1(t, t + 15); set s1(t, t + 15); pair<set::iterator, vector::iterator > resultSet = equal(s1.begin(), s1.end(), v1.begin()); cout<<*resultSet.first<<" "<<*resultSet.second<<endl; return 0; } Program outputs:
Options
Q: 6
What happens when you attempt to compile and run the following code? #include #include using namespace std; template class A { T_v; public: A() {} A(T v): _v(v){} T getV() { return _v; } void add(T & a) { _v+=a; } void add(string & a) { _v.insert(0, a); } }; int main() { Aa("Hello"); string s(" world!"); a.add(s); cout << a.getV() <<endl; return 0; }
Options
Q: 7
What happens when you attempt to compile and run the following code? #include #include #include #include using namespace std; class B { int val; public: B(int v):val(v){} int getV() const {return val;} bool operator > (const B & v) const { return val>v.val;} }; ostream & operator <<(ostream & out, const B & v) { out<<v.getV(); return out;} templatestruct Out { ostream & out; Out(ostream & o): out(o){} void operator() (const T & val ) { out<<val<<" "; } }; int main() { int t[]={20, 30, 10, 20, 30, 10, 20, 30, 10, 20}; deque d1(t, t+10); sort(d1.begin(), d1.end(), greater()); pair<deque ::iterator, deque::iterator > result = equal_range(d1.begin(), d1.end(), B(20), greater()); for_each(result.first, result.second, Out(cout));cout<<endl; return 0; } Program outputs:
Options
Q: 8
What will happen when you attempt to compile and run the code below, assuming that you enter the following sequence: 1.1 2.2 3.3? #include #include using namespace std; int main () { int a,b,c; cin>>a>>b>>c; cout<<a<<b<<c<<endl; return 0; } Program will output:
Options
Q: 9
What happens when you attempt to compile and run the following code? #include #include #include using namespace std; bool compare(int a, int b) { return a == b; } int main () { int t[] = {1,2,3,4,5,1,2,3,4,5}; vector v (t,t+10); vector::iterator it = v.begin(); int m1[] = {1, 2, 3}; while ( (it = find_first_of (it, v.end(), m1, m1+3)) != v.end()) { cout<<it?v.begin()<<" "; } cout<< endl; return 0; }
Options
Q: 10
What will happen when you attempt to compile and run the following code? #include using namespace std; class C { public: int _c; C():_c(0){} C(int c) { _c = c;} C operator+=(C & b) { C tmp; tmp._c = _c+b._c; return tmp; } }; template class A { T_v; public: A() {} A(T v): _v(v){} T getV() { return _v; } void add(T & a) { _v+=a; } }; int main() { A b(2); Aa (5); Cc; a.add(c); cout << a.getV() <<endl; return 0; }
Options
Q: 11
What happens when you attempt to compile and run the following code? #include #include int main () { std::vectorv1; for(int i = 10; i>0; i??) { v1.push_back(i); } std::vector::iterator it = v1.begin(); int sum = 0; while(it != v1.end()) { sum+=it++; } std::cout<<*v1.erase(v1.begin(),v1.end()?3)<<" "<<sum <<std::endl; return 0; }
Options
Q: 12
What happens when you attempt to compile and run the following code? #include #include #include #include using namespace std; class B { int val; public: B(int v):val(v){} int getV() const {return val;} bool operator < (const B & v) const { return val<v.val;} }; ostream & operator <<(ostream & out, const B & v) { out<<v.getV(); return out;} templatestruct Out { ostream & out; Out(ostream & o): out(o){} void operator() (const T & val ) { out<<val<<" "; } }; int main() { B t[]={8, 10, 5, 1, 4, 6, 2, 7, 9, 3}; B t1[]={B(1),B(2),B(3),B(4)}; deque d1(t, t+10); set s1(t, t+10); sort(d1.begin(), d1.end()); cout<<includes(d1.begin(),d1.end(), t1,t1+4)<<" "<<includes(s1.begin(),s1.end(), t1,t1+4) <<endl; return 0; } Program outputs:
Options
Q: 13
What happens when you attempt to compile and run the following code? #include #include using namespace std; int main() { int t[] = { 1, 1, 2, 2, 3, 3, 4, 4, 5, 5 }; string s[] = { "one", "one", "two", "two", "three","three", "four", "four", "five", "five"}; map m; for (int i = 0; i < 10; i++) { m.push_back(pair(t[i], s[i])); } for (map::iterator i = m.begin(); i != m.end(); i++) { cout <first << " "; } return 0; }
Options
Q: 14
What happens when you attempt to compile and run the following code? #include #include using namespace std; int main() { int t[] = { 1, 1, 2, 2, 3, 3, 4, 4, 5, 5 }; string s[] = { "one", "one", "two", "two", "three","three", "four", "four", "five", "five"}; multimap m; for (int i = 0; i < 10; i++) { m.push_back(pair(t[i], s[i])); } for (multimap::iterator i = m.begin(); i != m.end(); i++) { cout <first << " "; } return 0; }
Options
Q: 15
What happens when you attempt to compile and run the following code? #include #include #include #include using namespace std; bool identical(int a, int b) { return b == 2*a?true:false; } int main() { int t[] = {1,2,3,2,3,5,1,2,7,3,2,1,10, 4,4,5}; int u[] = {2,4,6,4,6,10,2,4,14,6,4,2,20,8,8,5}; vector v1(t, t + 15); deque d1(u, u + 15); pair<deque::iterator, vector::iterator > result; result = mismatch(d1.begin(), d1.end(), v1.begin(), identical); //Line I if (result.first == d1.end() && result.second == v1.end()) {//Line II cout<<"Identical\n"; } else { cout<<"Not identical\n"; } return 0; } Program outputs:
Options
Q: 16
What happens when you attempt to compile and run the following code? #include #include #include using namespace std; void print(int v) { cout<<v<<" "; } struct Sequence { int start; Sequence(int start):start(start){} int operator()() { return start++; } }; int main() { vector v1(10); generate_n(v1.begin(), 10, Sequence(1)); for_each(v1.begin(), v1.end(), print); cout<<endl; return 0; } Program outputs:
Options
Q: 17
What happens when you attempt to compile and run the following code? #include #include #include using namespace std; template void print(T start, T end) { while (start != end) { std::cout << *start << " "; start++; } } int main(){ vectorv; multiset s; for(int i=10; i>0; i??) { v.push_back(i); s.push_back(i); } print(v.begin(), v.end()); print(s.begin(), s.end());cout<<endl; return 0; }
Options
Q: 18
What happens when you attempt to compile and run the following code? Choose all that apply. #include #include using namespace std; int main () { vectorv1(10, 3); v1.push_back(3); cout<<v1.capacity()<<" "<< v1.size()<<endl; return 0; }
Options
Q: 19
What happens when you attempt to compile and run the following code? #include #include #include using namespace std; int main () { listl1; dequed1; for(int i=0; i<5; i++) { l1.push_back(i);l1.push_front(i); d1.push_back(i);d1.push_front(i); } for(int i=0; i<d1.size(); i++) { cout<<d1[i]<<" "<<l1[i]<<" "; } cout<<endl; return 0; }
Options
Q: 20
What happens when you attempt to compile and run the following code? #include #include #include using namespace std; int main(){ int t[] ={ 3, 4, 2, 1, 6, 5, 7, 9, 8, 0 }; vectorv(t, t+10); multiset s1(v.begin(),v.end()); s1.insert(v.begin(),v.end()); pair<multiset::iterator,multiset::iterator> range; range = s1.equal_range(6); while (range.first != range.second) { cout<<*range.first<<" "; range.first++; } return 0; }
Options
Question 1 of 20

Premium Access Includes

  • Quiz Simulator
  • Exam Mode
  • Progress Tracking
  • Question Saving
  • Flash Cards
  • Drag & Drops
  • 3 Months Access
  • PDF Downloads
Get Premium Access
Scroll to Top

FLASH OFFER

Days
Hours
Minutes
Seconds

avail 10% DISCOUNT on YOUR PURCHASE