C++ Institute CPP-22-02

Page:    1 / 46   
Total 230 questions | Updated On: Apr 25, 2024
Question 1

What happens when you attempt to compile and run the following code?
 #include <deque>
 #include <iostream>#include <algorithm>
 #include <functional>
 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;}
 template<class T>struct 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<B> d1(t, t+10);
 sort(d1.begin(), d1.end(), greater<B>());
 pair<deque<B> ::iterator, deque<B>::iterator > result = equal_range(d1.begin(), d1.end(),
B(20), greater<B>());
 for_each(result.first, result.second, Out<B>(cout));cout<<endl;
 return 0;
 }
Program outputs:


Answer: B
Question 2

What happens when you attempt to compile and run the following code?
 #include <vector>
 #include <iostream>
 #include <algorithm>
 using namespace std;
 template<typename T>class B { T val;
 public:
 B(T v):val(v){}
 T getV() const {return val;} bool operator < (const B & v) const { return val<v.val;} };
 template<class T>ostream & operator <<(ostream & out, const B<T> & v) { out<<v.getV();
return out;}
 template<class T>struct Out {
 ostream & out;
 Out(ostream & o): out(o){}void operator() (const T & val ) { out<<val<<" "; } };
 bool Less(const B<float> &a, const B<float> &b) { return int(a.getV())<int(b.getV());}
 int main() {
 float t[]={2.28, 1.66, 1.32, 3.94, 3.64, 2.3, 2.98, 1.96, 2.62, 1.13};
 vector<B<float> > v1; v1.assign(t, t+10);
 stable_sort(v1.begin(), v1.end(), Less);
 for_each(v1.begin(), v1.end(), Out<B<float> >(cout));cout<<endl;
 return 0;
 }
Program outputs:


Answer: A
Question 3

What happens when you attempt to compile and run the following code?
 #include <vector>
 #include <iostream>
 #include <algorithm>
 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;}
 template<class T>struct Out {
 ostream & out;
 Out(ostream & o): out(o){}
 void operator() (const T & val ) { out<<val<<" "; } };
 int main() {
 B t1[]={3,2,4,1,5};
 B t2[]={6,10,8,7,9};
 vector<B> v1(10);
 sort(t1, t1+5);
 sort(t2, t2+5);
 merge(t1,t1+5,t2,t2+5,v1.begin());
 for_each(v1.begin(), v1.end(), Out<B>(cout));cout<<endl;
 return 0;
 }
Program outputs:


Answer: A
Question 4

What happens when you attempt to compile and run the following code?
 #include <iostream>
 using namespace std;
 template<class A>
 void f(A a)
 {
 cout<<1<<endl;
 }
 void f(int a)
 {
 cout<<2<<endl;
 }
 int main(){
 int a = 1;
 f<float>(a);
 return 0;
 }


Answer: A
Question 5

What happens when you attempt to compile and run the following code?
 #include <deque>
 #include <iostream>
 #include <algorithm>
 #include <set>
 using namespace std;
 template<class T>struct Out {
 ostream & out;
 Out(ostream & o): out(o){}
 void operator() (const T & val ) { out<<val<<" "; }
 };
 bool Compare(char a, char b) { return tolower(a) < tolower(b);}
 int main() {
 char s[]={"qwerty"};
 char t1[]={"ert"};
 char t2[]={"ERT"};
 sort(s, s+6);
 cout<<includes(s,s+6, t1,t1+3, Compare)<<" "<<includes(s,s+6, t2,t2+3, Compare)<<endl;
 return 0;
 }
Program outputs:


Answer: D
Page:    1 / 46   
Total 230 questions | Updated On: Apr 25, 2024

Quickly grab our CPP-22-02 product now and kickstart your exam preparation today!

Name: CPP - C++ Certified Professional Programmer
Exam Code: CPP-22-02
Certification: C++ Certified Professional Programmer
Vendor: C++ Institute
Total Questions: 230
Last Updated: Apr 25, 2024