Docsity

C++ Programming Exercises: Vectors, Deques, Lists, Priority Queues, Sets, and Multisets, Exams of Nursing

Toggle sidebar
bg1
CPP Questions and Answers 2023
1
What will happen when you attempt to compile and run the
following code? Choose all that apply.
#include <vector>
#include <iostream>
int main ()
{
std::vector<int> v1;
v1.push_back(17); // LINE I
std::cout<<v1.front()<<", "<<v1.back()<<std::endl; // LINE II
return 0;
}
Group of answer choices
compilation error in LINE I
program outputs: 0, 17,
code compiles and executes successfully
compilation error in LINE II program
outputs: 0, 0,
program outputs: 17, 17,
Question 21 pts
What will happen when you attempt to compile and run the
following code?
#include <deque>
#include <iostream>
using namespace std;
template<typename T> ostream & print(T & start, T & end)
{
for(; start != end; ++start)
cout<< *start<< " ";
return cout;
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e
pf4f
pf50
pf51
pf52
pf53
pf54
pf55
pf56
pf57
pf58
pf59
pf5a
pf5b
pf5c
pf5d
pf5e
pf5f
pf60
pf61
pf62
pf63
pf64

Partial preview of the text

Download C++ Programming Exercises: Vectors, Deques, Lists, Priority Queues, Sets, and Multisets and more Exams Nursing in PDF only on Docsity!

CPP Questions and Answers 2023

What will happen when you attempt to compile and run the

following code? Choose all that apply.

#include #include int main () { std::vector v1; v1.push_back(17); // LINE I std::cout< #include using namespace std; template ostream & print(T & start, T & end) { for(; start != end; ++start) cout<< *start<< " "; return cout;

empty_all()

clear()

there is no such method emptier() Question 41 pts What will happen when you attempt to compile and run the following code? #include #include #include #include using namespace std; template ostream & print(const T & start, const T & end) { T tmp = start; for(; tmp != end; ++tmp) { cout<< *tmp ; } return cout; } class A { public: int a; public: A(int a):a(a) {} }; void fill (const int table[], unsigned size, vector & v) { for(unsigned i = 0; i < size; ++i) { v.push_back(new A(table[i])); //LINE I } } ostream & operator<<(ostream & c, const A & o) { c << o.a << ", "; return c; } int main() { int tab[]={8, 7, 6, 4, 2, 1}; vector v1;

deque d; priority_queue q(d.begin(), d.end());

list_queue l; priority_queue q(l); deque d; priority_queue q(d);

Question 61 pts Which container class can be used as an underlying container for the stack? Choose all that apply. Group of answer choices deque vector list multimap Set Question 71 pts What will happen when you attempt to compile and run the following code? #include #include #include using namespace std; int main(){ int mynumbers[] = {8,9,7,6,4,1}; vector v(mynumbers, mynumbers+6); set s1(v.begin(),v.end()); s1.insert(v.begin(),v.end()); s1.insert(v.begin(),v.end());//LINE I set::iterator found = s1.find(9); for (; found!=s1.end(); ++found) cout << *found << ", "; return 0; } Group of answer choices program outputs: 0,

program outputs: 9,

runtime error at LINE I compilation error in LINE I Question 81 pts What will happen 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(){ int mynumbers[] = {8,6,4,1}; vector v(mynumbers, mynumbers+6); set s(v.begin(),v.end()); for(int i=4; i>0; i ) { int x = *(s.begin()); //LINE I s.pop(); //LINE II v.push_back(i+x); } print(v.begin(), v.end()); print(s.begin(), s.end());cout< program outputs: 8, 6, 4, 1, 8, 6, 4, 1,

#include #include

#include using namespace std; int main(){ int mynumbers[] = { 8, 9, 7, 6, 4, 1, 6 }; multiset s1(mynumbers, mynumbers+6);//LINE I s1.insert(s1.find(9), 5); //LINE II for(multiset::iterator i=s1.begin();i!= s1.end(); i++) cout<<*i<<", "; return 0; } Group of answer choices runtime error at LINE I program outputs: 1, 4, 5, 6, 6, 7, 8, 9, program outputs: 1, 4, 5, 6, 7, 8, 9, program outputs: 1, 4, 5, 6, 7, 8, compilation error in LINE I compilation error in LINE II Question 111 pts 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(){ int mynumbers[] = { 8, 9, 7, 6}; vector v(mynumbers, mynumbers+4); multiset s1(mynumbers, mynumbers+4); for(int i=4; i>0; i-- ) { double x=(s1.begin());//LINE I v.push_back(i+x); //LINE II

print(v.begin(), v.end()); cout< #include #include using namespace std; int main(){ int mynumbers[] = { 8, 9, 7, 6, 4, 1, 2}; vector v(mynumbers, mynumbers+6); multiset s1(v.begin(),v.end()); s1.insert(v.begin(),v.end()); s1.insert(v.begin(),v.end());//LINE I pair::iterator,multiset::iterator> range; range = s1.equal_range(6); while (range.first != range.second) { cout<<*range.first<<", "; //LINE II range.first++; } return 0; }

Group of answer choices

program outputs: zero, two, seven, seven, eight, eight, nine, nine,

CPP compilation error in LINE I program outputs: zero, two, two, seven, seven, eight, eight, nine, nine, program outputs: two, two, seven, seven, eight, eight, nine, nine, Question 141 pts What will happen when you attempt to compile and run the following code? #include #include #include #include #include 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 v1(mynumbers, mynumbers + 6); set s1(mynumbers, mynumbers + 6); 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; } Group of answer choices runtime error at LINE I