What is the output of the program given below?
#include
using namespace std;
int main (int argc, const char * argv[])
{
int i=10;
{
int i=0;
cout<
}
{
i=5;
cout << i;
}
cout<
return 0;
}
Suggested Answer: C
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
HE
Herman
3 months ago
I agree with Lou, but I feel unsure about the scope rules.
TH
Theola
3 months ago
No, it should print 0, then 5, then 10.
LO
Lou
3 months ago
I think it prints 5, then 5, then 5.
TI
Tiera
3 months ago
Confusing question!
IS
Isidra
4 months ago
The outer 'i' doesn't get printed until the end!
CA
Carlota
5 months ago
I thought it would print 10 at some point.
FR
Freeman
5 months ago
Wait, why does it print 0 first?
MA
Maddie
5 months ago
Totally agree, that's how scope works!
FL
Flo
6 months ago
The output will be 05.
JE
Jeff
6 months ago
Haha, this question is like a rite of passage for any C++ programmer. The output is a classic!
SU
Suzan
6 months ago
Wait, what? How can the output be 0 5 10? I'm so confused, someone please help!
AR
Aretha
6 months ago
Hmm, I thought the output would be 0 10 10. This C++ scoping thing is tricky.
NI
Nichelle
6 months ago
Ah, the good old scope resolution! The output is spot on.
AU
Audra
6 months ago
The inner block overrides the outer block's variable, so the output is 0 5 10.
HY
Hyun
7 months ago
The output is 0 5 10.
MI
Micah
7 months ago
I believe it prints 0, then 5, and finally 10. I just hope I got the scoping right!
MA
Marg
7 months ago
I’m a bit confused about the order of outputs. I practiced a question like this, but I can't recall the exact details.
TO
Torie
8 months ago
I remember something similar where inner scopes shadowed outer variables. So maybe it prints 0 first?
IS
Isabelle
8 months ago
I think the output will be 0, 5, and then 10, but I'm not completely sure about the scope rules.
JA
Jamie
8 months ago
Whoa, this is tricky. I better read through the code a few times and draw out the variable scopes to make sure I don't miss anything.
WA
Wava
8 months ago
I think I've got it! The first cout will print 0, the second will print 5, and the third will print 10. I'll double-check my work, but I'm feeling confident about this one.
OC
Ocie
8 months ago
I'm a bit confused by the nested blocks here. I'll need to make sure I understand how variable scope works in C++ to solve this one.
BA
Barrett
9 months ago
Okay, let me think this through step-by-step. There are three blocks of code, each with a different value for the variable 'i'. I'll need to pay attention to which 'i' is being used in each cout statement.
MO
Mozell
9 months ago
Hmm, this looks like a scope issue. I'll need to carefully trace the variable declarations and assignments to figure out the output.
DE
Deandrea
2 months ago
The last `cout` should print 5, right?
FA
Fanny
3 months ago
Yeah, because of the inner scope.
RO
Rodrigo
3 months ago
I think the first `cout` will print 0.