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
User1: Got it, thanks for clarifying!
DE
Denny
1 year ago
User3: Oh, I see! Thanks for the explanation.
JU
Julio
1 year ago
User2: Actually, it prints: 2
JI
Jillian
1 year ago
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
Correct, the output is 2.
AU
Audry
1 year ago
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.