Independence Day Deal! Unlock 25% OFF Today – Limited-Time Offer - 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
1 months 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
Galen
7 days ago
I'm leaning towards C) 7 as the output.
upvoted 0 times
...
Carlee
11 days ago
I believe the correct output is B) 3.
upvoted 0 times
...
Sanda
14 days ago
I think it will output A) 4.
upvoted 0 times
...
...
Thad
2 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
1 months ago
So the count_if function will return the number of even elements, which is 4.
upvoted 0 times
...
Fabiola
1 months ago
Yes, you're right. The duplicates will be ignored in the set.
upvoted 0 times
...
Aileen
1 months 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
Paz
1 days ago
I think you're right, the answer should be B) 3.
upvoted 0 times
...
Herminia
8 days ago
Exactly, the code is counting the number of even numbers in the set.
upvoted 0 times
...
Melynda
14 days ago
So, the output should be 3 because the Even struct checks for even numbers.
upvoted 0 times
...
Hyun
22 days ago
Yes, count_if() counts the number of elements for which the predicate is true.
upvoted 0 times
...
Evangelina
27 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
1 months ago
I think you're right, the answer should be B) 3.
upvoted 0 times
...
Evangelina
1 months 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
3 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
az-700  pass4success  az-104  200-301  200-201  cissp  350-401  350-201  350-501  350-601  350-801  350-901  az-720  az-305  pl-300  

Warning: Cannot modify header information - headers already sent by (output started at /pass.php:70) in /pass.php on line 77