[CPP] Chapter 4 Assessment


Question 1
/ 1 pts

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

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
void printer(int i) {
    cout << i << ", ";
}
int main() {
    int mynumbers[] = { 3, 9, 0, 2, 1, 4, 5 };
    vector<int> v1(mynumbers, mynumbers + 7);
    copy(mynumbers, mynumbers + 7, v1.end());//LINE I
    for_each(v1.begin(), v1.end(), printer);//LINE II
    return 0;
}
  
  
  
  
  
  
  
 
Question 2
/ 1 pts

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

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
void printer(int i) {
    cout << i << ", ";
}
int main() {
    int mynumbers[] = { 3, 9, 0, 2, 1, 4, 5 };
    vector<int> v1(mynumbers, mynumbers + 7);
    copy_backward(mynumbers, mynumbers + 7, v1.rend());//LINE I
    for_each(v1.begin(), v1.end(), printer);//LINE II
    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 <set>
using namespace std;
void printer(int i) {
    cout << i << ", ";
}
int main() {
    int mynumbers[] = { 3, 9, 0, 2, 1, 4, 5 };
    set<int> s1(mynumbers, mynumbers + 7);
    vector<int> v1(s1.rbegin(), s1.rend());
    swap(s1, v1);//LINE I
    for_each(v1.begin(), v1.end(), printer);//LINE II
    return 0;
}
  
  
  
  
  
  
  
  
 
Question 4
/ 1 pts

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

#include <iostream>
#include <algorithm>
#include <vector>
#include <deque>
using namespace std;
void printer(int i) {
    cout << i << ", ";
}
int main() {
    int mynumbers[] = { 3, 9, 0, 2, 1, 4, 5 };
    deque<int> d1(mynumbers, mynumbers + 7);
    vector<int> v1(d1.rbegin(), d1.rend());
    swap_ranges(v1.begin(), v1.end(), d1.begin());//LINE I
    sort(d1.begin(), d1.end());//LINE II
    for_each(d1.begin(), d1.end(), printer);
    for_each(v1.begin(), v1.end(), printer);
    return 0;
}
  
  
  
  
  
  
  
  
 
Question 5
/ 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;
void printer(int i) {
    cout << i << ", ";
}
int main() {
    int mynumbers[] = { 3, 9, 0, 2, 1, 4, 5 };
    set<int> s1(mynumbers, mynumbers + 7);
    vector<int> v1(s1.rbegin(), s1.rend());
    swap_ranges(v1.begin(), s1.end(), v1.begin());//LINE I
    swap_ranges(s1.begin(), v1.end(), s1.begin());//LINE II
    for_each(s1.begin(), s1.end(), printer);
    for_each(v1.begin(), v1.end(), printer);
    return 0;
}
  
  
  
  
  
  
  
 
Question 6
/ 1 pts

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

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
void printer(int i) {
    cout << i << ", ";
}
void multiply (int a) {
    a*2;//LINE I
}
int main() {
    int mynumbers[] = { 3, 9, 0, 2, 1, 4, 5 };
    vector<int> v1(mynumbers, mynumbers + 7);
    for_each(v1.begin(), v1.end(), multiply);
    iter_swap(v1.begin(), mynumbers+6);//LINE II
    for_each(v1.begin(), v1.end(), printer);
    return 0;
}
  
  
  
  
  
  
  
 
Question 7
/ 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;
void printer(int i) {
    cout << i << ", ";
}
int  multiply (int a) {
    return a*2;//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);
    transform(s1.begin(), s1.end(), v1.begin(), multiply);//LINE II
    for_each(s1.begin(), s1.end(), printer);
    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>
#include <set>
using namespace std;
void printer(int i) {
    cout << i << ", ";
}
int  multiply (int a) {
    return a*2;//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);
    transform(s1.begin(), s1.end(), v1.begin(), multiply);//LINE II
    for_each(v1.begin(), v1.end(), printer);
    return 0;
}
  
  
  
  
  
  
  
 
Question 9
/ 1 pts

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

#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <deque>
using namespace std;
void printer(int i) {
    cout << i << ", ";
}
int add (int a, int b)
{    
    return a+b;
}
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;
    transform(s1.begin(), s1.end(), v1.begin(), d1.begin(), add);//LINE I
    for_each(d1.begin(), d1.end(), printer); //LINE II
    return 0;
}
  
  
  
  
  
  
  
 
Question 10
/ 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;
void printer(int i) {
    cout << i << ", ";
}
int main() {
    int mynumbers[] = { 3, 9, 0, 2, 1, 4, 5 };
    vector<int> v1(mynumbers, mynumbers + 7);
    set<int> s1(mynumbers, mynumbers + 7);
    replace(v1.begin(), v1.end(), 9, 3);//LINE I
    for_each(v1.begin(), v1.end(), printer);//LINE II
    return 0;
}
  
  
  
  
  
  
  
  
 
Question 11
/ 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;
void printer(int i) {
    cout << i << ", ";
}
bool classifier(int v) {
    return v-3>0;
}
int main() {
    int mynumbers[] = { 3, 9, 0, 2, 1, 4, 5 };
    vector<int> v1(mynumbers, mynumbers + 7);
    set<int> s1(mynumbers, mynumbers + 7);
    replace_if(v1.begin(), v1.end(), classifier, 7);//LINE I
    for_each(v1.begin(), v1.end(), printer);//LINE II
    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>
#include <set>
using namespace std;
void printer(int i) {
    cout << i << ", ";
}
bool classifier(int v) {
    return v-3>0;
}
int main() {
    int mynumbers[] = { 3, 9, 0, 2, 1, 4, 5 };
    vector<int> v1(mynumbers, mynumbers + 7);
    set<int> s1(mynumbers, mynumbers + 7);
    replace(v1.begin(), v1.end(), classifier, 7);//LINE I
    for_each(v1.begin(), v1.end(), printer);//LINE II
    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>
#include <set>
using namespace std;
void printer(int i) {
    cout << i << ", ";
}
int main() {
    vector<int> v1(7,1);
    fill(v1.begin()+3, v1.end()-1, 8);//LINE I
    fill_n(v1.begin()+4, 2, 7);//LINE II
    for_each(v1.begin(), v1.end(), printer);
    return 0;
}
  
  
  
  
  
  
  
 
IncorrectQuestion 14
/ 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;
void printer(int i) {
    cout << i << ", ";
}
int main() {
    vector<int> v1(7,1);
    fill(v1.begin()+3, v1.end()-1, 8);//LINE I
    fill_n(v1.begin()+4, 5, 7);//LINE II
    for_each(v1.begin(), v1.end(), printer);
    return 0;
}
  
  
  
  
  
  
  
 
Question 15
/ 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;
void printer(int i) {    
    cout << i << ", ";
}
struct sequence {
    int val,inc;
public:
    sequence(int s, int i):val(s),inc(i){}
    operator int() const{//LINE I
        int r = val;
        return r;
    }
};
int main() {
    vector<int> v1(7);
    fill(v1.begin(), v1.end(), sequence(1,2));//LINE II
    for_each(v1.begin(), v1.end(), printer);
    return 0;
}
  
  
  
  
  
  
  
  
 
Question 16
/ 1 pts

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

#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
void print(int v) {
    cout<<v<<", ";
}
struct Sequence {
    int start;
    Sequence(int start):start(start){}
    int operator()() {
        return start++;//LINE I
    }
};
int main() {
    vector<int> v1(7);
    generate_n(v1.begin(), 7, Sequence(1));//LINE II
    for_each(v1.begin(), v1.end(), print);
    return 0;
}
  
  
  
  
  
  
  
 
Question 17
/ 1 pts

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

#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
void print(int v) {
    cout<<v<<", ";
}
struct Sequence {
    int start;
    Sequence(int start):start(start){}
    int operator()() {
        return start++;//LINE I
    }
};
int main() {
    vector<int> v1(7);
    generate_n(v1.begin(), 7, Sequence());//LINE II
    for_each(v1.begin(), v1.end(), print);
    return 0;
}
  
  
  
  
  
  
  
 
PartialQuestion 18
0.5 / 1 pts

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

#include <vector>
#include <set>
#include <iostream>
#include <algorithm>
using namespace std;
void print(int v)
{    
    cout<<v<<", ";
}
struct Sequence {
    int start;
    Sequence(int start):start(start){}
    int operator()() { return start++; }
};
bool predicate(int v) { return v%2==0; }
int main() {
    vector<int> v1(7);
    generate_n(v1.begin(), 7, Sequence(1));//LINE I
    remove_if(v1.begin(), v1.end(), predicate);//LINE II
    for_each(v1.begin(), v1.end(), print);
    return 0;
}
  
  
  
  
  
  
  
  
 
Question 19
/ 1 pts

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

#include <vector>
#include <set>
#include <iostream>
#include <algorithm>
using namespace std;
void print(int v)
{    
    cout<<v<<", ";
}
struct Sequence {
    int start;
    Sequence(int start):start(start){}
    int operator()() { return start++; }
};
bool predicate(int v) { return v%2==0; }
int main() {
    vector<int> v1(7);
    generate_n(v1.begin(), 7, Sequence());//LINE I
    remove_if(v1.begin(), v1.end(), predicate);//LINE II
    for_each(v1.begin(), v1.end(), print);
    return 0;
}
  
  
  
  
  
  
  
  
 
Question 20
/ 1 pts

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

#include <vector>
#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 3*(start++ % 2);//LINE I
    }
};
int main() {
    vector<int> v1(7);
    generate(v1.begin(), v1.end(), Sequence(10));//LINE II
    unique(v1.begin(),v1.end());
    for_each(v1.begin(), v1.end(), Out<int>(cout) );
    return 0;
}
  
  
  
  
  
  
  
  
 
Question 21
/ 1 pts

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

#include <vector>
#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 3*(start++ % 2);
    }
};
int main() {
    vector<int> v1(4);
    vector<int> v2(4);
    generate(v1.begin(), v1.end(), Sequence(10));//LINE I
    sort(v1.rbegin(), v1.rend());
    unique_copy(v1.begin(),v1.end(), v2.begin());//LINE II
    for_each(v2.begin(), v2.end(), Out<int>(cout) );
    for_each(v1.begin(), v1.end(), Out<int>(cout) );
    return 0;
}
  
  
  
  
  
  
  
  
 
PartialQuestion 22
0.5 / 1 pts

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

#include <vector>
#include <iostream>
#include <set>
#include <deque>
#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++ % 7;
    }
};
int main() {
    vector<int> v1(3);
    generate(v1.begin(), v1.end(), Sequence(10));//LINE I
    set<int> s1(v1.rbegin(), v1.rend());
    deque<int> d1(s1.rbegin(), s1.rend());
    reverse(v1.begin(),v1.end());
    reverse(d1.begin(), d1.end());//LINE II
    for_each(s1.begin(), s1.end(), Out<int>(cout) );
    for_each(v1.begin(), v1.end(), Out<int>(cout) );
    for_each(d1.begin(), d1.end(), Out<int>(cout) );
    return 0;
}
  
  
  
  
  
  
  
  
  
 
Question 23
/ 1 pts

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

#include <vector>
#include <iostream>
#include <functional>
#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++ % 7;
    }
};
int main() {
    vector<int> v1(4);
    vector<int> v2(4);
    generate(v1.begin(), v1.end(), Sequence(10));
    reverse_copy(v1.begin(),v1.end(), v2.rbegin());//LINE I
    sort(v2.begin(), v2.end(), less_equal<int>());;//LINE II
    for_each(v2.begin(), v2.end(), Out<int>(cout) );
    return 0;
}
  
  
  
  
  
  
  
  
  
 
Question 24
/ 1 pts

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

#include <vector>
#include <iostream>
#include <functional>
#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++ % 7;
    }
};
int main() {
    vector<int> v1(4);
    generate(v1.rbegin(), v1.rend(), Sequence(10));//LINE I
    rotate(v1.begin(),v1.begin() + 1, v1.end() );//LINE II
    for_each(v1.begin(), v1.end(), Out<int>(cout) );
    return 0;
}
  
  
  
  
  
  
  
  
  
 
Question 25
/ 1 pts

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

#include <vector>
#include <iostream>
#include <functional>
#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++ % 7;
    }
};
int main() {
    vector<int> v1(4);
    generate(v1.rbegin(), v1.rend(), Sequence(10));//LINE I
    random_shuffle(v1.begin(),v1.begin());//LINE II
    for_each(v1.begin(), v1.end(), Out<int>(cout) );
    return 0;
}
  
  
  
  
  
  
  
  
  
 
Question 26
/ 1 pts

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

#include <vector>
#include <iostream>
#include <functional>
#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++ % 7;
    }
};
struct Odd {    bool operator()(int v) { return v%2==0; }};
int main() {
    vector<int> v1(4);
    generate(v1.rbegin(), v1.rend(), Sequence(10));//LINE I
    partition(v1.begin(),v1.begin(), Odd());//LINE II
    for_each(v1.begin(), v1.end(), Out<int>(cout) );
    return 0;
}
  
  
  
  
  
  
  
  
  
 
Question 27
/ 1 pts

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

#include <vector>
#include <iostream>
#include <functional>
#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++ % 7;
    }
};
struct Odd {    bool operator()(int v) { return v%2==0; }};
int main() {
    vector<int> v1(4);
    generate(v1.rbegin(), v1.rend(), Sequence(10));//LINE I
    stable_partition(v1.begin(),v1.begin(), Odd());//LINE II
    for_each(v1.begin(), v1.end(), Out<int>(cout) );
    return 0;
}
  
  
  
  
  
  
  
  
Quiz Score: 24 out of 27