Deal of The Day! Hurry Up, Grab the Special Discount - Save 25% - Ends In 00:00:00 Coupon code: SAVE25
Welcome to Pass4Success

- Free Preparation Discussions

C++ Institute Exam CPP Topic 4 Question 92 Discussion

Actual exam question for C++ Institute's CPP exam
Question #: 92
Topic #: 4
[All CPP Questions]

What happens 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

};

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 v1(t, t + 10);

set 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;

}

Show Suggested Answer Hide Answer
Suggested Answer: D

Contribute your Thoughts:

Emily
4 hours ago
This question seems a bit tricky. I'm going to have to really think it through. Wait, is that a typo in the code? I bet the compiler is going to catch that. Option D sounds like the way to go, but I'll double-check the code just to be sure.
upvoted 0 times
...
Erasmo
2 days ago
Hold on, did you say this is a compilation error? I better take a closer look at the code. I don't want to waste time on a question with a syntax issue. Let me go with option D just to be safe.
upvoted 0 times
...
Mickie
4 days ago
Hmm, interesting. I'm not sure about the behavior of the `set` in this case. I might have to double-check the documentation on how `set` handles custom comparison operators. I'll go with option C just in case.
upvoted 0 times
...
Levi
5 days ago
The code looks pretty straightforward, so I'm going to go with option B. The `for_each` loop should update the values in the `vector` and then display them, and the same thing should happen with the `set`.
upvoted 0 times
...
Malissa
6 days ago
But the code is adding 10 to each element, so the output should be increasing by 10 each time.
upvoted 0 times
...
Wayne
7 days ago
I disagree, I believe the correct answer is C.
upvoted 0 times
...
Malissa
14 days ago
I think the correct answer is B.
upvoted 0 times
...

Save Cancel