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 CPP Exam - Topic 11 Question 119 Discussion

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
D) compilation error
A) program outputs: 10 5 9 6 2 4 7 8 3 1 1 2 3 4 5 6 7 8 9 10
B) program outputs: 20 15 19 16 12 14 17 18 13 11 1 2 3 4 5 6 7 8 9 10
C) program outputs: 20 15 19 16 12 14 17 18 13 11 11 12 13 14 15 16 17 18 19 20

C++ Institute CPP Exam - Topic 11 Question 119 Discussion

Actual exam question for C++ Institute's CPP exam
Question #: 119
Topic #: 11
[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:

0/2000 characters
Veronique
22 days ago
Wait, are you serious? I thought it would just run fine.
upvoted 0 times
...
Ronna
27 days ago
Definitely going with option D, that won't compile!
upvoted 0 times
...
Willodean
1 month ago
Looks like a compilation error due to the `this?>a` typo.
upvoted 0 times
...
Stephanie
1 month ago
The output should be option B if it runs correctly!
upvoted 0 times
...
Wilford
1 month ago
Wait, are you sure it won't compile?
upvoted 0 times
...
Xuan
2 months ago
I thought it would output the original numbers first, then the modified ones.
upvoted 0 times
...
Ilda
2 months ago
Totally agree, that syntax is wrong!
upvoted 0 times
...
Oretha
2 months ago
Looks like a compilation error due to the `this?>a` line.
upvoted 0 times
...
Doretha
2 months ago
I feel like the output should be option B, since we're adding 10 to each element in the vector, but I'm not completely confident about the set part.
upvoted 0 times
...
Genevive
2 months ago
I practiced a similar question where we had to modify a vector and then print it, but I can't recall if the set will reflect those changes too.
upvoted 0 times
...
Freeman
2 months ago
I remember something about how sets work with custom classes, but I'm not sure if they will actually store the modified values after using `add10`.
upvoted 0 times
...
Tamala
3 months ago
I think there might be a compilation error because of the `this?>a` line in the `setA` method. That doesn't look right.
upvoted 0 times
...

Save Cancel