CPP practice questions, page 7 of 9, with answer review and explanations.
Question 31
What happens when you attempt to compile and run the following code? #include #include #include #include using namespace std; template 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:
0 0
0 1
1 0
1 1
Question 32
What happens when you attempt to compile and run the following code? #include using namespace std; int main () { std::vector v1; v1.push_back(10); return 0; }
compilation fails due to error in line 2
compilation fails due to error in line 5
exception is thrown during run time
code compiles and executes successfully
Question 33
What will happen when you attempt to compile and run the following code? #include #include #include using namespace std; int main(){ int t[] ={ 3, 4, 2, 1, 6, 5, 7, 9, 8, 0 }; vector v(t, t+10); set s1(v.begin(),v.end()); s1.insert(v.begin(),v.end()); bool found = s1.find(7); if (found){ cout<<"Element found!\n"; }else { cout<<"Element not found!\n"; } return 0; }
program will display "Element found!"
program will display "Element not found!\n"
code will not compile
changing type of variable found to int will make this code compile
Question 34
What happens when you attempt to compile and run the following code? #include #include #include #include using namespace std; void myfunction(int i) { cout v1(t, t+10); set s1(t, t+10); replace(v1.begin(), v1.end(),classifier, 10); for_each(v1.begin(), v1.end(), myfunction); return 0; } Program outputs:
1 5 10 5 10 10 10 3 3 1
1 5 2 5 2 4 4 3 3 1
compilation error
10 10 2 10 2 4 4 10 10 10
Question 35
What happens when you attempt to compile and run the following code? #include #include #include using namespace std; int main () { list l1; deque d1; for(int i=0; i<5; i++) { l1.push_back(i);l1.push_front(i); d1.push_back(i);d1.push_front(i); } for(int i=0; i<d1.size(); i++) { cout<<d1[i]<<" "<<l1[i]<<" "; } cout<<endl; return 0; }