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 2 Question 69 Discussion

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

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

#include

#include

#include

using namespace std;

struct Even {

bool operator ()(int a) {

return (a % 2)==0?true:false;

}

};

int main () {

int t[] = {1,2,3,2,3,5,1,2,7,3,2,1,10, 4,4,5};

set s(t,t+15);

int number = count_if(s.begin(), s.end(), Even());

cout<< number<

return 0;

}

Program outputs:

Show Suggested Answer Hide Answer
Suggested Answer: C

Contribute your Thoughts:

Blair
29 days ago
I'm just gonna go with E) compilation error. Why? Because that's the only answer that doesn't involve any actual coding, and we all know the real answer is whatever the compiler decides it is.
upvoted 0 times
...
Thad
1 months ago
Aha! I think I've got it. The set will only contain unique elements, so the duplicates will be ignored. That means the answer is A) 4. Nailed it!
upvoted 0 times
Carry
15 days ago
So the count_if function will return the number of even elements, which is 4.
upvoted 0 times
...
Fabiola
16 days ago
Yes, you're right. The duplicates will be ignored in the set.
upvoted 0 times
...
Aileen
21 days ago
I think the answer is A) 4 because the set will only contain unique elements.
upvoted 0 times
...
...
Kaitlyn
2 months ago
Wait, wait, wait. Didn't we learn that count_if() counts the number of elements for which the predicate is true? So the answer should be B) 3. I hope I'm not missing something obvious here.
upvoted 0 times
Hyun
7 days ago
Yes, count_if() counts the number of elements for which the predicate is true.
upvoted 0 times
...
Evangelina
12 days ago
That's right, count_if() counts the number of elements for which the predicate is true. So the answer is indeed B) 3.
upvoted 0 times
...
Ty
18 days ago
I think you're right, the answer should be B) 3.
upvoted 0 times
...
Evangelina
18 days ago
Yes, you are correct. The answer is B) 3.
upvoted 0 times
...
...
Annice
2 months ago
Ah, I see what's going on here. The set is created with 15 elements, but we're only checking the first 7. So the answer must be D) 8. Easy peasy!
upvoted 0 times
...
Rashida
2 months ago
Hmm, this looks like a tricky one. Let's see, we're using a custom 'Even' functor to count the even numbers in a set. I'm guessing the answer is C) 7, since the set contains 7 even numbers.
upvoted 0 times
...
Asha
2 months ago
I'm not sure, but I think the answer might be B) 3 because the Even struct might count the number 10 as even.
upvoted 0 times
...
Long
2 months ago
I agree with Buffy, the Even struct checks for even numbers and the set contains 4 even numbers.
upvoted 0 times
...
Buffy
2 months ago
I think the answer is A) 4 because the Even struct counts the even numbers in the set.
upvoted 0 times
...

Save Cancel