New Year Sale 2026! Hurry Up, Grab the Special Discount - Save 25% - Ends In 00:00:00 Coupon code: SAVE25
Welcome to Pass4Success

- Free Preparation Discussions

C++ Institute CPA-21-02 Exam - 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:

0/2000 characters
Mindy
2 months ago
Nope, it’s not 0 or -1 either.
upvoted 0 times
...
Galen
2 months ago
I thought it would print 1, but I guess not!
upvoted 0 times
...
Loren
2 months ago
Totally agree, it’s definitely 2!
upvoted 0 times
...
Tonette
3 months ago
It prints: 2
upvoted 0 times
...
Chantay
3 months ago
Wait, how does it end up being 2?
upvoted 0 times
...
Antonio
3 months ago
Wait, could it be that it prints 1? I feel like the increment happens after the comparison, so I'm confused about the output.
upvoted 0 times
...
Noemi
3 months ago
I practiced a similar question, and I think it should print 2 because `i` is incremented after the condition is evaluated.
upvoted 0 times
...
Magda
4 months ago
I'm not entirely sure, but I remember something about post-increment. Maybe it prints 1 because `i` is still 1 when checked?
upvoted 0 times
...
Ressie
4 months ago
I think the code checks if `i++` equals 1, so it might print 2 since `i` gets incremented after the comparison.
upvoted 0 times
...
Dante
4 months ago
Alright, time to put my C++ knowledge to the test. I'll work through this step-by-step and make sure I understand the output.
upvoted 0 times
...
Martina
4 months ago
I think I've got a handle on this. The key is understanding how the post-increment operator works in the `if` condition.
upvoted 0 times
...
Denny
4 months ago
I'm a bit confused by this one. The `if` statement and the `cout` statements don't seem to match up at first glance.
upvoted 0 times
...
Karon
5 months ago
Okay, let me see here. The post-increment operator `i++` is used in the `if` condition, so I'll need to pay close attention to that.
upvoted 0 times
...
Amina
5 months ago
Hmm, this looks like a tricky one. I'll need to carefully step through the code and think about the order of operations.
upvoted 0 times
...
Basilia
8 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
7 months ago
User1: Got it, thanks for clarifying!
upvoted 0 times
...
Denny
7 months ago
User3: Oh, I see! Thanks for the explanation.
upvoted 0 times
...
Julio
7 months ago
User2: Actually, it prints: 2
upvoted 0 times
...
Jillian
8 months ago
User1: I think it prints: 1
upvoted 0 times
...
...
Regenia
9 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
9 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
8 months ago
Correct, the output is 2.
upvoted 0 times
...
Audry
8 months ago
It prints: 2
upvoted 0 times
...
...
Tegan
9 months ago
I think it prints 2 because the post-increment operator is used.
upvoted 0 times
...
Freeman
10 months ago
I'm not sure, but I think it's A.
upvoted 0 times
...
Keshia
10 months ago
I believe it prints 2.
upvoted 0 times
...
Brock
10 months ago
I think the answer is B.
upvoted 0 times
...

Save Cancel