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 CPA-21-02 Topic 1 Question 39 Discussion

Actual exam question for C++ Institute's CPA-21-02 exam
Question #: 39
Topic #: 1
[All CPA-21-02 Questions]

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

#include

using namespace std;

int main(){

int i = 1;

if (i++==1) {

cout << i;

} else {

cout << i-1;

}

return 0;

}

Show Suggested Answer Hide Answer
Suggested Answer: D

Contribute your Thoughts:

Basilia
2 months ago
Haha, this is a classic trick question! The right answer is B, because the post-increment operator `i++` returns the original value of `i`, which is 1, and then increments it. So the output is 2, not 1.
upvoted 0 times
Nohemi
14 days ago
User1: Got it, thanks for clarifying!
upvoted 0 times
...
Denny
15 days ago
User3: Oh, I see! Thanks for the explanation.
upvoted 0 times
...
Julio
27 days ago
User2: Actually, it prints: 2
upvoted 0 times
...
Jillian
1 months ago
User1: I think it prints: 1
upvoted 0 times
...
...
Regenia
2 months ago
I think the correct answer is B. The `if (i++==1)` condition evaluates to true, and the `cout << i;` statement prints the incremented value of `i`, which is 2.
upvoted 0 times
...
Deane
2 months ago
The output is obviously 2, since the post-increment operator `i++` returns the original value of `i` before incrementing it.
upvoted 0 times
Audry
2 months ago
Correct, the output is 2.
upvoted 0 times
...
Audry
2 months ago
It prints: 2
upvoted 0 times
...
...
Tegan
3 months ago
I think it prints 2 because the post-increment operator is used.
upvoted 0 times
...
Freeman
3 months ago
I'm not sure, but I think it's A.
upvoted 0 times
...
Keshia
3 months ago
I believe it prints 2.
upvoted 0 times
...
Brock
3 months ago
I think the answer is B.
upvoted 0 times
...

Save Cancel