C++ Programming - References - Discussion
Discussion Forum : References - Programs (Q.No. 7)
7.
Which of the following statement is correct about the program given below?
#include<iostream.h>
int main()
{
int x = 0;
int &y = x; y = 5;
while(x <= 5)
{
cout<< y++ << " ";
x++;
}
cout<< x;
return 0;
}
Discussion:
6 comments Page 1 of 1.
Pratik Patil said:
1 year ago
@All.
First, it check the while loop, while(5<=5)--> the condition is true.
Then enter into the loop, then first print y and then increment the value, because y++ (post-increment).
Now x=y=6;
x++; -----> (post-increment)
Now x=y=7;
Now print the x.
So, the answer is 5 and 7.
First, it check the while loop, while(5<=5)--> the condition is true.
Then enter into the loop, then first print y and then increment the value, because y++ (post-increment).
Now x=y=6;
x++; -----> (post-increment)
Now x=y=7;
Now print the x.
So, the answer is 5 and 7.
Pooja said:
5 years ago
Y++ = 6 but it post increment so it print 5.
Now, y=x=6.
It will come out of the loop .
X++=7.
So out is 5, 7.
Now, y=x=6.
It will come out of the loop .
X++=7.
So out is 5, 7.
Ajay said:
7 years ago
int &y = x; y = 5;
Is that syntax right?
Is that syntax right?
Bidyut said:
8 years ago
But when x will be 7 it will come out of loop and print the value.
Naman said:
10 years ago
But when the value of x=7, counter will not satisfy while loop.
Nitish said:
1 decade ago
Since, y and x are reference variable. They point to same location in memory. So, when y=5, then both x & y have value 5.
while(5=5) //x=y=5.
{
cout<<y++ // it makes value of y=x=6, but prints 5, post increment.
x++ //now x becomes 7.
Finally, cout<<x prints 7.
Hence, output is 5 7.
while(5=5) //x=y=5.
{
cout<<y++ // it makes value of y=x=6, but prints 5, post increment.
x++ //now x becomes 7.
Finally, cout<<x prints 7.
Hence, output is 5 7.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers