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

Posted by Teri on May 02, 2025 at 10:59 AM

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;

}

Suggested Answer: D

Get Premium CPA-21-02 Questions as Interactive Web-Based Practice Test or PDF Real Exam Format Detailed Explanations Instant Access Get Your Practice Test
27 comments · newest first
MI
Mindy 10 months ago
Nope, it’s not 0 or -1 either.
GA
Galen 10 months ago
I thought it would print 1, but I guess not!
LO
Loren 10 months ago
Totally agree, it’s definitely 2!
TO
Tonette 10 months ago
It prints: 2
CH
Chantay 11 months ago
Wait, how does it end up being 2?
AN
Antonio 11 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.
NO
Noemi 11 months ago
I practiced a similar question, and I think it should print 2 because `i` is incremented after the condition is evaluated.
MA
Magda 11 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?
RE
Ressie 11 months ago
I think the code checks if `i++` equals 1, so it might print 2 since `i` gets incremented after the comparison.
DA
Dante 12 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.
MA
Martina 12 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.
DE
Denny 12 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.
KA
Karon 1 year 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.
AM
Amina 1 year ago
Hmm, this looks like a tricky one. I'll need to carefully step through the code and think about the order of operations.
BA
Basilia 1 year 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.
NO
Nohemi 1 year ago
↳ replying to Basilia
User1: Got it, thanks for clarifying!
DE
Denny 1 year ago
↳ replying to Basilia
User3: Oh, I see! Thanks for the explanation.
JU
Julio 1 year ago
↳ replying to Basilia
User2: Actually, it prints: 2
JI
Jillian 1 year ago
↳ replying to Basilia
User1: I think it prints: 1
RE
Regenia 1 year 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.
DE
Deane 1 year ago
The output is obviously 2, since the post-increment operator `i++` returns the original value of `i` before incrementing it.
AU
Audry 1 year ago
↳ replying to Deane
Correct, the output is 2.
AU
Audry 1 year ago
↳ replying to Deane
It prints: 2
TE
Tegan 1 year ago
I think it prints 2 because the post-increment operator is used.
FR
Freeman 1 year ago
I'm not sure, but I think it's A.
KE
Keshia 1 year ago
I believe it prints 2.
BR
Brock 1 year ago
I think the answer is B.
0/2000 characters

Save Cancel