
[Sep-2021] Get 100% Real CPP Exam Questions, Accurate & Verified ExamcollectionPass Dumps in the Real Exam!
Pass Your C++ Certified Exams Fast. All Top CPP Exam Questions Are Covered.
NEW QUESTION 42
What happens when you attempt to compile and run the following code? Choose all that apply.
#include <vector>
#include <iostream>
using namespace std;
int main ()
{
vector<int>v1(10, 3);
v1.push_back(3);
cout<<v1.capacity()<<" "<< v1.size()<<endl;
return 0;
}
- A. size of vector v1 is 11
- B. program displays 10 3
- C. all elements of vector v1 are of the same value
- D. program displays 4 4
Answer: A,C
NEW QUESTION 43
What happens when you attempt to compile and run the following code?
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
class A {
int a;
public:
A(int a) : a(a) {}
int getA() const { return a; } void setA(int a) { this?>a = a; }
bool operator==(A & b) { return a == b.a; }
};
struct Compare{
bool operator()(const A & a, const A & b) {return a.getA()==b.getA();};
};
int main () {
int t[] = {1,2,3,4,5,1,2,3,4,5};
vector<A> v (t,t+10);
vector<A>::iterator it;
A m1[] = {A(1), A(2), A(3)};
it = find_end (v.begin(), v.end(), m1, m1+3, Compare());
cout << "Found at position: " << it?v.begin() << endl;
return 0;
}
- A. program outputs: Found at position: 7
- B. program outputs: Found at position: 5
- C. compilation error
- D. program outputs: Found at position: 10
***/ - E. program outputs: Found at position: 0
Answer: B
NEW QUESTION 44
What happens when you attempt to compile and run the following code?
#include <vector>
#include <set>
#include <iostream>
#include <algorithm>
using namespace std;
template<class T>struct Out {
ostream & out;
Out(ostream & o): out(o){}
void operator() (const T & val ) { out<<val<<" "; } };
struct Sequence {
int start;
Sequence(int start):start(start){}
int operator()() { return start++; } };
int main() {
vector<int> v1(10);
generate_n(v1.begin(), 10, Sequence(1));
random_shuffle(v1.rbegin(), v1.rend());
sort(v1.begin(), v1.end(), great<int>());
for_each(v1.begin(), v1.end(), Out<int>(cout));cout<<endl;
return 0;
}
Program outputs:
- A. compilation error
- B. 8 10 5 1 4 6 2 7 9 3
- C. 1 2 3 4 5 6 7 8 9 10
- D. 10 9 8 7 6 5 4 3 2 1
Answer: A
NEW QUESTION 45
What happens when you attempt to compile and run the following code?
#include <vector>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std;
template<class T>struct Out {
ostream & out;
Out(ostream & o): out(o){}
void operator() (const T & val ) { out<<val<<" "; } };
int Add(int a, int b) {
return a+b;
}
int main() {
int t[]={1,2,3,4,5,6,7,8,9,10};
vector<int> v1(t, t+10);
vector<int> v2(10);
transform(v1.begin(), v1.end(), v2.begin(), bind2nd(ptr_fun (Add),1));
for_each(v2.rbegin(), v2.rend(), Out<int>(cout));cout<<endl;
return 0;
}
Program outputs:
- A. 11 10 9 8 7 6 5 4 3 2
- B. 1 2 3 4 5 6 7 8 9 10
- C. 10 9 8 7 6 5 4 3 2 1
- D. compilation error
- E. 2 3 4 5 6 7 8 9 10 11
Answer: A
NEW QUESTION 46
What happens when you attempt to compile and run the following code? Choose all possible answers.
#include <iostream>
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;
} };
ostream & operator<<(ostream & c, const C & v) {
c<<v._c; return c; }
template <class T>
class A {
T _v;
public:
A() {}
A(T v): _v(v){}
T getV() { return _v; }
void add(T & a) { _v+=a; }
};
int main()
{
A<int> b(2);
A<C> a (5);
a.add(C());
cout << a.getV() <<endl;
return 0;
}
- A. program will not compile
- B. program will cause runtime exception
- C. program will display:5
- D. program will compile
Answer: C,D
NEW QUESTION 47
What happens when you attempt to compile and run the following code?
# include <iostream>
# include <map>
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<int, string> m;
for (int i = 0; i < 10; i++) {
m.insert(pair<int, string>(t[i], s[i]));
}
if (m.count(3) == 2) {
m.erase(3);
}
for (map<int, string>::iterator i = m.begin(); i != m.end(); i++) {
cout << i?>first << " ";
}
return 0;
}
- A. program outputs: 1 1 2 2 3 4 4 5 5
- B. program outputs: 1 1 2 3 3 4 4 5 5
- C. program outputs: 1 2 3 4 5
- D. program outputs: one two three four five
- E. program outputs: 1 2 4 5
Answer: C
NEW QUESTION 48
What happens when you attempt to compile and run the following code?
#include <vector>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std;
template<class T>struct Out {
ostream & out;
Out(ostream & o): out(o){}
void operator() (const T & val ) { out<<val<<" "; } };
struct Add : public binary_function<int, int, int> {
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};
vector<int> v1(t, t+10);
vector<int> v2(10);
transform(v1.begin(), v1.end(), v2.begin(), bind1st(Add(), 1));
for_each(v2.rbegin(), v2.rend(), Out<int>(cout));cout<<endl;
return 0;
}
Program outputs:
- A. 11 10 9 8 7 6 5 4 3 2
- B. 1 2 3 4 5 6 7 8 9 10
- C. 10 9 8 7 6 5 4 3 2 1
- D. compilation error
- E. 2 3 4 5 6 7 8 9 10 11
Answer: A
NEW QUESTION 49
What happens when you attempt to compile and run the following code?
#include <iostream>
#include <algorithm>
#include <vector>
#include <deque>
#include <set>
using namespace std;
class A {
int a;
public:
A(int a):a(a) {}
int getA() const { return a;} void setA(int a){ this?>a = a;}
bool operator < ( const A & b) const { return a<b.a;}
};
struct display { void operator() (const A & a) {cout << " " << a.getA();} }; struct add10
{
void operator() (A & a) { a.setA(a.getA()+10) ;}
};
int main() {
int t[] = { 10, 5, 9, 6, 2, 4, 7, 8, 3, 1 };
vector<A> v1(t, t + 10);
set<A> s1(t, t + 10);
for_each(v1.begin(), v1.end(), add10()); for_each(v1.begin(), v1.end(), display()); for_each(s1.begin(), s1.end(), add10()); for_each(s1.begin(), s1.end(), display()); return 0;
}
- A. compilation error
- B. program outputs: 10 5 9 6 2 4 7 8 3 1 1 2 3 4 5 6 7 8 9 10
- C. program outputs: 20 15 19 16 12 14 17 18 13 11 1 2 3 4 5 6 7 8 9 10
- D. program outputs: 20 15 19 16 12 14 17 18 13 11 11 12 13 14 15 16 17 18 19 20
Answer: A
NEW QUESTION 50
What happens when you attempt to compile and run the following code?
#include <iostream>
using namespace std;
template<int>
void g(int a)
{
cout<<a?1<<endl;
}
template<class A>
void g(A a)
{
cout<<a+1<<endl;
}
int main()
{
int a = 1;
g(a);
return 0;
}
- A. program displays: 2
- B. runtime exception
- C. program displays: 1
- D. compilation error
Answer: A
NEW QUESTION 51
What will happen when you attempt to compile and run the code below, assuming that you enter the following sequence: true false<enter>?
#include <iostream>
#include <string>
using namespace std;
int main ()
{
bool a,b;
cin>>boolalpha>>a>>b;
cout<<a<<b<<endl;
return 0;
}
Program will output:
- A. 1false
- B. none of these
- C. 0
- D. truefalse
- E. true0;
Answer: C
NEW QUESTION 52
What happens when you attempt to compile and run the following code?
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main () {
int t[] = {1,2,3,3,5,1,2,4,4,5};
vector<int> v (t,t+10);
vector<int>::iterator it = v.begin();
while ( (it = adjacent_find (it, v.end())) != v.end()) {
cout<<it?v.begin()<<" ";it++;
}
cout<< endl;
return 0;
}
- A. program will run forever
- B. program outputs: 2 7
- C. program outputs: 3 8
- D. compilation error
- E. program outputs: 2 3
Answer: B
NEW QUESTION 53
What happens when you attempt to compile and run the following code?
#include <set>
#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() {
int t[]={8, 10, 5, 1, 4, 6, 2, 7, 9, 3};
set<B> s1(t, t+10);
sort(s1.begin(), s1.end());
for_each(s1.begin(), s1.end(), Out<B>(cout));cout<<endl;
return 0;
}
Program outputs:
- A. compilation error
- B. 8 10 5 1 4 6 2 7 9 3
- C. 1 2 3 4 5 6 7 8 9 10
- D. 10 9 8 7 6 5 4 3 2 1
Answer: A
NEW QUESTION 54
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:
- A. compilation error
- B. 1 2 3 4 5 6 7 8 9 10
- C. 3 2 4 1 5 6 7 8 9 10
- D. 1 2 3 4 5 6 10 8 7 9
- E. 3 2 4 1 5 6 10 8 7 9
Answer: A
NEW QUESTION 55
What happens when you attempt to compile and run the following code?
#include <deque>
#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() { int t[]={8, 10, 5, 1, 4, 6, 2, 7, 9, 3}; deque<B> d1(t, t+10); sort(d1.begin(), d1.end()); deque<B>::iterator it = upper_bound(d1.begin(), d1.end(), B(4)); for_each(it, d1.end(), Out<B>(cout)); cout<<endl; return 0;
}
Program outputs:
- A. 4 5 6 7 8 9 10
- B. 1 2 3 4
- C. 1 2 3 4 5
- D. 6 7 8 9 10
- E. 5 6 7 8 9 10
Answer: E
NEW QUESTION 56
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() {
int t[]={8, 10, 5, 1, 4, 6, 2, 7, 9, 3};
vector<B> v1(t, t+10);
sort(v1.begin(), v1.end(), greater<B>());
for_each(v1.begin(), v1.end(), Out<B>(cout));cout<<endl;
return 0;
}
Program outputs:
- A. compilation error
- B. 8 10 5 1 4 6 2 7 9 3
- C. 1 2 3 4 5 6 7 8 9 10
- D. 10 9 8 7 6 5 4 3 2 1
Answer: A
NEW QUESTION 57
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:
- A. the exact output is impossible to determine
- B. 1.13 1.32 1.66 1.96 2.28 2.3 2.62 2.98 3.64 3.94
- C. 1.66 1.32 1.96 1.13 2.28 2.3 2.98 2.62 3.94 3.64
- D. compilation error
- E. 3.94 3.64 2.98 2.62 2.3 2.28 1.96 1.66 1.32 1.13
Answer: C
NEW QUESTION 58
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=0):val(v){}
int getV() const {return val;}
operator int () const { return val;} };
template<class T>struct Out {
ostream & out;
Out(ostream & o): out(o){}
void operator() (const T & val ) { out<<val<<" "; } };
struct Add {
B operator()(B & a, B & b) { return a+b; }};
int main() {
int t[]={1,2,3,4,5,6,7,8,9,10};
vector<B> v1(t, t+10);
vector<B> v2(10);
transform(v1.begin(), v1.end(), v2.begin(), bind1st(Add(),1));
for_each(v2.rbegin(), v2.rend(), Out<B>(cout));cout<<endl;
return 0;
}
Program outputs:
- A. compilation error
- B. 11 10 9 8 7 6 5 4 3 2
- C. 1 2 3 4 5 6 7 8 9 10
- D. 10 9 8 7 6 5 4 3 2 1
- E. 2 3 4 5 6 7 8 9 10 11
Answer: A
NEW QUESTION 59
What happens when you attempt to compile and run the following code?
#include <iostream>
#include <deque>
#include <list>
#include <stack>
#include <vector>
using namespace std;
int main()
{
deque<int> mydeck;list<int> mylist; vector<int> myvector;
stack<int> first;
stack<int> second(mydeck);
stack<int> third(second);
stack<int, list<int> > fourth(mylist);
fourth.push(10);fourth.push(11);fourth.push(12);
stack<int, vector<int> > fifth(myvector);
fifth.push(10);fifth.push(11);fifth.push(12);
while(!fifth.empty())
{
cout<<fifth.top()<<" ";
fifth.pop();
}
while (!fourth.empty())
{
cout << fourth.front() << " ";
fourth.pop();
}
return 0;
}
- A. compilation error
- B. runtime exception
- C. program outputs: 10 11 12 10 11 12
- D. program outputs: 12 11 10 12 11 10
Answer: A
NEW QUESTION 60
What happens when you attempt to compile and run the following code?
# include <iostream>
# include <map>
# include <vector>
# include <sstream>
# include <string>
using namespace std;
int main() {
int t[] = { 3, 4, 2, 1, 6, 5, 7, 9, 8, 0 };
vector<int> v(t, t + 10);
map<int, string> m;
for (vector<int>::iterator i = v.begin(); i != v.end(); i++) {
stringstream s;s << *i << *i;
m.insert(pair<int, string>(*i, s.str()));
}
pair<map<int, string>::iterator, map<int, string>::iterator> range;
range = m.equal_range(6);
for (map<int, string>::iterator i = range.first; i != range.second; i++) { cout << i?>first << " ";
}
return 0;
}
- A. program outputs: 6 7
- B. program outputs: 6 5
- C. program outputs: 5 7
- D. program outputs: 6
- E. program outputs: 1 5
Answer: D
NEW QUESTION 61
What happens when you attempt to compile and run the following code?
#include <iostream>
#include <map>
#include <string>
using namespace std;
int main(){
int second[] ={ 3, 4, 2, 1, 6, 5, 7, 9, 8, 10 };
string first[] = {"three", "four", "two", "one", "six","five", "seven", "nine","eight"," ten"}; map<int,string> m; for(int i=0; i<10; i++) { m.insert(pair<int,string>(second[i],first[i]));
}
if (m[11] == "eleven") {
cout<<"eleven ";
}
for(map<int, string>::iterator i=m.begin();i!= m.end(); i++) {
cout<<i?>second<<" ";
}
cout<<m.size();
return 0;
}
- A. program outputs: one two three four five six seven eight nine ten 11
- B. program outputs: eleven one two three four five six seven eight nine ten 10
- C. runtime exception
- D. program outputs: one two three four five six seven eight nine ten 10
- E. program outputs: one two three four five six seven eight nine ten 10
Answer: A
NEW QUESTION 62
......
Penetration testers simulate CPP exam: https://www.examcollectionpass.com/c-plus-plus-institute/CPP-practice-exam-dumps.html