[CPP] Chapter 3 Assessment

 Question 1

/ 1 pts

What will happen when you attempt to compile and run the following code?

#include <iostream>
#include <algorithm>
#include <vector>
#include <deque>
#include <set>
using namespace std;

void myprint(int i) {
    cout << i << ", ";// Line I
}

int main() {
    int mynumbers[] = { 3, 9, 0, 2, 1, 4, 5 };
    vector<int> v1(mynumbers, mynumbers + 7);
    set<int> s1(mynumbers, mynumbers + 7);
    deque<int> d1(mynumbers, mynumbers + 7);
    d1.pop_front();// Line II
    for_each(v1.begin(), v1.end(), myprint); // Line III
    for_each(s1.begin(), s1.end(), myprint);
    for_each(d1.begin(), d1.end(), myprint);
    return 0;
}
  
  
  
  
  
  
  
 
Question 2
/ 1 pts

What will happen when you attempt to compile and run the following code? Choose all that apply.

#include <iostream>
#include <algorithm>
#include <vector>
#include <deque>
#include <set>
using namespace std;

struct myprinter {
  void operator() (int i) {cout << i << ", ";}
};

int main() {
    int mynumbers[] = { 3, 9, 0, 2, 1, 4, 5 };
    vector<int> v1(mynumbers, mynumbers + 7);
    deque<int> d1(mynumbers, mynumbers + 7);
    set<int> s1(mynumbers, mynumbers + 7);
    v1.pop_back(5);// Line I
    for_each(s1.begin(), s1.end(), myprinter());  // Line II
    for_each(d1.begin(), d1.end(), *(new myprinter()));  // Line III
    for_each(v1.begin(), v1.end(), myprinter);  //Line IV
    return 0;
}
  
  
  
  
  
  
  
 
Question 3
/ 1 pts

What will happen 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 myprinter { void operator() (const A & a) {cout << a.getA() << ", ";} };
struct doubler
{
    void operator() (A a) { a.setA(a.getA()*2) ;}//LINE I
};

int main() {
    int mynumbers[] = { 3, 9, 0, 2, 1, 4, 5 };
    vector<A> v1(mynumbers, mynumbers + 7);
    set<A> s1(mynumbers, mynumbers + 7);
    for_each(v1.begin(), v1.end(), doubler()); for_each(v1.begin(), v1.end(), myprinter());//LINE II
    for_each(s1.begin(), s1.end(), doubler()); for_each(s1.begin(), s1.end(), myprinter());//LINE III
    return 0;
}
  
  
  
  
  
  
  
 
Question 4
/ 1 pts

What will happen when you attempt to compile and run the following code? Choose all that apply.

#include <iostream>
#include <algorithm>
#include <vector>
#include <deque>
#include <set>
using namespace std;

int main() {
    int mynumbers[] = { 3, 9, 0, 2, 1, 4, 5 };
    vector<int> v1(mynumbers, mynumbers + 7);
    deque<int> d1(mynumbers, mynumbers + 7);
    set<int> s1(mynumbers, mynumbers + 7);
    vector<int>::iterator found = find(v1.begin(), v1.end(), 3);//LINE I
    if(found!=v1.end())//LINE II
        cout << "found" <<", ";
    cout<<find(d1.begin(), d1.end(), 9)<<", ";//LINE III
    cout<<find(s1.begin(), s1.end(), 6);//LINE IV
    return 0;
}
  
  
  
  
  
  
  
  
 
Question 5
/ 1 pts

Which sentences are true about the code below?

#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 ==(const A & b) const {return (this->a != b.a); }//LINE I
};

struct doubler { void operator()(A & a) { a.setA(a.getA() * 2); } };//LINE II

int main() {
    int mynumbers[] = { 3, 9, 0, 2, 1, 4, 5 };
    vector<A> v1(mynumbers, mynumbers + 7);
    for_each(v1.begin(), v1.end(), doubler());
    vector<A>::iterator it = find(v1.begin(), v1.end(), A(7));
    cout << it->getA() << endl;//LINE III
    return 0;
}

 

  
  
  
  
  
  
  
 
Question 6
/ 1 pts

Which sentences are true about the code below? Choose all that apply.

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
class Founder {
public:
    int val;
    Founder(int  v):val(v){}
    bool operator() (int v) {return (v == val);}//LINE I
};

int main() {
    int mynumbers[] = { 3, 9, 0, 2, 1, 4, 5 };
    vector<int> v1(mynumbers, mynumbers + 7);
    if (find(v1.begin(), v1.end(), 7) == find(v1.begin(), v1.end(), Founder(7).val)) {//LINE II
        cout<<"Found!\n";
    } else {
        cout<<"Not found!\n";
    }
    return 0;
}
  
  
  
  
  
 
Question 7
/ 1 pts

What will happen when you attempt to compile and run the following code? Choose all that apply.

#include <iostream>
#include <algorithm>
#include <vector>
#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;}
};
class Founder {
    A val;
public:
    Founder(A & v):val(v){}
    bool operator() (A & v) {return (v.getA() == val.getA());}
};
int main() {
    int mynumbers[] = { 3, 9, 0, 2, 1, 4, 5 };
    vector<A> v1(mynumbers, mynumbers + 7);//LINE I
    set<A> s1(mynumbers, mynumbers + 7);
    A a(5);    
    Founder f(a);
    find_if(s1.begin(), s1.end(), f.val);//LINE II
    if (find_if(v1.begin(), v1.end(), f) !=v1.end()) {//LINE III
        cout<<"Found!\n";
    } else {
        cout<<"Not found!\n";
    }
    return 0;
}
  
  
  
  
  
  
  
 
Question 8
/ 1 pts

What will happen when you attempt to compile and run the following code?

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

int main () {
  int mynumbers [] = { 3, 9, 0, 2, 1, 4, 5 };
  vector<int> v (mynumbers, mynumbers + 7);
  vector<int>::iterator it;
  int m1[] = {9, 0, 2 };
  it = find_end (v.begin(), v.end(), m1, m1+3);//LINE I
  if (it != v.end())
    cout << "Found at position: " << it-v.begin() << endl;
  return 0;
}
  
  
  
  
  
  
 
Question 9
/ 1 pts

What will happen when you attempt to compile and run the following code? Choose all that apply.

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
class A {
    int a;
    int getA() const { return a; }    void setA(int a) { this->a = a; }
public:
    A(int a) :    a(a) {}
    bool operator==(A & b) { return a == b.a; }
};
struct Comparer{
    bool operator()(const A & a, const A & b) {return a.getA()==b.getA();};//LINE I
};
int main () {
  int mynumbers[] = { 3, 9, 0, 2, 1, 4, 5 };
  vector<A> v (mynumbers, mynumbers + 7);
  vector<A>::iterator it;
  A m1[] = {A(2), A(3), A(4)};
  it = find_end (v.begin(), v.end(), m1, m1+3, Comparer());//LINE II
  cout << "Found at position: " << it+v.begin() << endl;//LINE III
  return 0;
}
  
  
  
  
  
  
  
 
Question 10
/ 1 pts

What will happen when you attempt to compile and run the following code?

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

int main () {
  int mynumbers[] = { 3, 9, 0, 2, 1, 4, 5 };
  vector<int> v (mynumbers, mynumbers + 7);
  vector<int>::iterator it;
  int m1[] = {9, 0, 2};
  it = find_first_of (v.begin(), v.end(), m1, m1+3);//LINE I
  cout << "First found at position: " << it-v.begin() << endl;//LINE II
  return 0;
}
  
  
  
  
  
  
  
 
Question 11
/ 1 pts

What will happen when you attempt to compile and run the following code?

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
bool compare(int a, int b) { return a == b; }
int main () {
    int mynumbers[] = { 3, 9, 0, 2, 1, 4, 5 };
    vector<int> v (mynumbers, mynumbers + 7);
    vector<int>::iterator it = v.begin();
    int m1[] = {9, 0, 2};

    while ( (it = find_first_of (it, v.end(), m1, m1+3)) != v.end()) {//LINE I
        cout<<it-v.begin()<<", ";//LINE II
        it++;
    }
    cout<< endl;
    return 0;
}
  
  
  
  
  
  
 
Question 12
/ 1 pts

What will happen when you attempt to compile and run the following code?

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

int main () {
  int mynumbers[] =  { 3, 9, 0, 2, 2, 2, 5 };
  vector<int> v (mynumbers, mynumbers + 7);
  vector<int>::iterator it = v.begin();

  while ( (it = adjacent_find (it, v.end())) != v.end()) {//LINE I
      cout<<it-v.begin()<<", ";it--;//LINE II
  }
  cout<< endl;
  return 0;
}
  
  
  
  
  
  
  
  
 
Question 13
/ 1 pts

What will happen 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==(const A & b) const { return a == b.a; }
};
bool compare(const A & a, const A & b) { return a == b; }
int main () {
    int mynumbers[] =  { 3, 9, 0, 2, 2, 2, 5 };
    vector<A> v (mynumbers, mynumbers + 7);
    vector<A>::iterator it = v.begin();

    while ( (it = adjacent_find (it, v.end(), compare)) != v.end()) {//LINE I
        cout<<it-v.begin()<<", ";it++;//LINE II
    }
    cout<< endl;
    return 0;
}
  
  
  
  
  
  
  
  
 
Question 14
/ 1 pts

What will happen when you attempt to compile and run the following code?

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

int main () {
    int mynumbers[] = { 3, 9, 0, 2, 1, 4, 5, 6, 6, 9, 8, 2 };
    vector<int> v (mynumbers, mynumbers +12);

    int found = count(v.begin(), v.end(), 6);//LINE I
    cout<< found << endl;
    return 0;
}
  
  
  
  
  
  
  
 
Question 15
/ 1 pts

What will happen when you attempt to compile and run the following code?

#include <iostream>
#include <algorithm>
#include <deque>
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; }
};
int main () {
    int mynumbers[] = { 3, 9, 0, 2, 1, 4, 5, 6, 6, 9, 8, 2 };
    deque<int> d (mynumbers, mynumbers +12);
    int count = count(d.begin(), d.end(), 6);//LINE I
    cout<< count << endl;
    return 0;
}
  
  
  
  
  
  
  
 
IncorrectQuestion 16
/ 1 pts

What will happen when you attempt to compile and run the following code?

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
struct Compare {
    bool operator ()(int a) {return (a > 0);}//LINE I
};
int main () {
    int mynumbers[] = { 3, 9, 0, 2, 1, 4, 5, 6, 6, 9, 8, 2 };
    vector<int> v (mynumbers, mynumbers +12);

    int count = std::count(v.begin(), v.end(), Compare());//LINE II
    cout<< count <<endl;
    return 0;
}
  
  
  
  
  
  
  
  
 
Question 17
/ 1 pts

What will happen when you attempt to compile and run the following code?

#include <iostream>
#include <algorithm>
#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;}//LINE I
};
struct Compare {
    bool operator ()(A a) {    return (a.getA() < 6); }
};
int main () {
    int mynumbers[] = { 3, 9, 0, 2, 1, 4, 5, 6, 6, 9, 8, 2 };
    set<A> d (mynumbers, mynumbers +12);
    int count = count_if(d.begin(), d.end(), Compare());//LINE II
    cout<< count <<endl;
    return 0;
}
  
  
  
  
  
  
  
 
Question 18
/ 1 pts

What will happen when you attempt to compile and run the following code?

#include <iostream>
#include <algorithm>
#include <set>
using namespace std;
struct Pair {
    bool operator ()(int a) {
        return (a % 2)!=0;    //LINE I
    }
};
int main () {
    int mynumbers[] = { 3, 9, 0, 2, 1, 4, 5, 6, 6, 9, 8, 2 };
    set<int> s(mynumbers, mynumbers +12);

    int count = count_if(s.begin(), s.end(), Pair());//LINE II
    cout<< count <<endl;
    return 0;
}
  
  
  
  
  
  
  
 
Question 19
/ 1 pts

What will happen when you attempt to compile and run the following code?

#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
using namespace std;

int main() {
    int mynumbers[] = { 3, 9, 0, 2, 1, 4, 5, 6, 6, 9, 8, 2 };
    vector<int> v1(mynumbers, mynumbers + 12);
    set<int> s1(mynumbers, mynumbers + 12);
    v1.push_back(10);//LINE I
    pair<set<int>::iterator, vector<int>::iterator> resultSet = mismatch(s1.begin(), s1.end(), v1.begin());//LINE II
    cout<<*resultSet.first<<", "<<*resultSet.second<<endl;//LINE III

    return 0;
}
  
  
  
  
  
  
  
  
 
Question 20
/ 1 pts

What will happen when you attempt to compile and run the following code?

#include <iostream>
#include <algorithm>
#include <deque>
#include <vector>
using namespace std;
bool identical(int a, int b) {
    return b == a;//LINE I
}
int main() {
    int mynumbers[]    = { 3, 9, 0, 2, 1, 4, 5, 6, 6, 9, 8, 2 };
    int othernumbers[] = { 3, 9, 0, 3, 1, 4, 3, 6, 6, 9, 8, 3 };
    vector<int> v1(mynumbers, mynumbers + 12);
    deque<int> d1(othernumbers, othernumbers + 12);

    pair<deque<int>::iterator, vector<int>::iterator > result;//LINE II
    result = mismatch(d1.begin(), d1.end(), v1.begin(), identical);     //LINE III
    if (result.first == d1.end() && result.second == v1.end())
        cout<<"Identical\n";
    else
        cout<<"Not identical\n";
    return 0;
}
  
  
  
  
  
  
 
Question 21
/ 1 pts

What will happen when you attempt to compile and run the following code?

#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
using namespace std;

int main() {
    int mynumbers[] = { 3, 9, 0, 2, 1, 4, 5, 6, 6, 9, 8, 2 };
    vector<int> v1(mynumbers, mynumbers + 12);
    set<int> s1(mynumbers, mynumbers + 12);
    v1.push_back(10);
    pair<set<int>::iterator, vector<int>::iterator > resultSet = std::equal(s1.begin(), s1.end(), v1.begin());//LINE I
    cout<<*resultSet.first<<", "<<*resultSet.second<<endl;//LINE II

    return 0;
}
  
  
  
  
  
  
  
 
Question 22
/ 1 pts

What will happen when you attempt to compile and run the following code?

#include <iostream>
#include <algorithm>
#include <set>
using namespace std;
class A {
    int a;
public:
    A(int a) :    a(a) {}
    operator int() const {return a;}//LINE I
};

int main () {
    int mynumbers[] = { 3, 9, 0, 2, 1, 4, 5, 6, 6, 9, 8, 2 };
    set<A> s (mynumbers, mynumbers + 12);
    cout << equal(s.begin(), s.end(), s.begin()) << endl;//LINE II

    return 0;
}
  
  
  
  
  
  
  
 
Question 23
/ 1 pts

What will happen when you attempt to compile and run the following code?

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

int main () {
    int mynumbers[] = { 3, 9, 0, 2, 1, 4, 5 };
    vector<int> v (mynumbers, mynumbers + 7);
    vector<int>::iterator it;
    int m1[] = {9, 0, 2};
    it = search (v.begin(), v.end(), m1, m1+3);//LINE I
    cout << "found at position: " << it-v.begin() << endl;//LINE II
    return 0;
}
  
  
  
  
  
  
 
Question 24
/ 1 pts

What will happen 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 mynumbers[] = { 3, 9, 0, 2, 1, 4, 5 };
    vector<A> v (mynumbers, mynumbers + 7);
    vector<A>::iterator it;
    A m1[] = {A(2), A(3), A(4)};
    it = search (v.begin(), v.end(), m1, m1+3, Compare());//LINE I
    cout << "First found at position: " << it-v.begin() << endl;//LINE II
    return 0;
}
  
  
  
  
  
  
  
 
Question 25
/ 1 pts

What will happen when you attempt to compile and run the following code?

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

int main () {
    int mynumbers[] = { 3, 9, 0, 2, 1, 4, 5, 6, 6, 9, 8, 2 };
    vector<int> v (mynumbers, mynumbers + 12);

    vector<int>::iterator it = search_n(v.begin(), v.end(), 2, 1);//LINE I
    cout<< it-v.begin()<<endl;//LINE II
    return 0;
}
  
  
  
  
  
  
  
 
Question 26
/ 1 pts

What will happen when you attempt to compile and run the following code?

#include <iostream>
#include <algorithm>
#include <deque>
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; }
};
struct Equals {
    bool operator ()(const A & a, const A &b) {    return (a.getA()==b.getA());}//LINE I
};
int main () {
    int mynumbers[] = { 3, 9, 0, 2, 1, 4, 5, 6, 6, 9, 8, 2 };
    deque<int> d (mynumbers, mynumbers +12);
    deque<int>::iterator it = search_n(d.begin(), d.end(), 2, 1, Equals());//LINE II
    cout<< it-d.begin()<<endl;//LINE III
    return 0;
}
  
  
  
  
  
  
  
  
 
Question 27
/ 1 pts

What will happen when you attempt to compile and run the following code?

#include <iostream>
#include <algorithm>
#include <map>
using namespace std;

int main() {
    int mynumbers[] = { 3, 9, 0, 2, 1, 4, 5 };
    map<int, int> m;
    for(int i=0; i < 7; i++) {
        m[i]=mynumbers[i];
    }
    pair<const int,int> p(4,1);
    map<int, int>::iterator it = find(m.begin(), m.end(), p);//LINE I
    if (it != m.end())
        cout<<it->first<<endl;
    else
        cout<<"Not found!\n";
    return 0;
}
  
  
  
  
  
  
 
IncorrectQuestion 28
/ 1 pts

What will happen when you attempt to compile and run the following code? Choose all that apply.

#include <iostream>
#include <algorithm>
#include <map>
using namespace std;

void myprint(pair<int, int> i) {
    cout << i.first << ", " ;
}

int main() {
    int mynumbers[] = { 3, 9, 0, 2, 1, 4, 5 };
    map<int, int> m;
    for(int i=0; i < 7; i++) {
        m[i]=mynumbers[i];//LINE I
    }

    for_each(m.begin(), m.end(), myprint);//LINE II
    return 0;
}
  
  
  
  
  
  
  
  
  
  
Quiz Score: 26 out of 28