C++ Programming - References - Discussion

Discussion :: References - Programs (Q.No.14)

14. 

What will be the output of the following program?

#include <iostream.h> 
enum xyz 
{
    a, b, c
}; 
int main()
{
    int x = a, y = b, z = c; 
    int &p = x, &q = y, &r = z; 
    p = z; 
    p = ++q;
    q = ++p;
    z = ++q + p++; 
    cout<< p << " " << q << " " << z;
    return 0; 
}

[A]. 2 3 6
[B]. 4 4 7
[C]. 4 5 8
[D]. 3 4 6

Answer: Option B

Explanation:

No answer description available for this question.

Rajendra Mahanor said: (Sep 20, 2014)  
enum started value a=0, b=1, c=3 then,

Assign value x=a=0, y=b=1, z=c=2 then,

Reference value,

&p = x = 0.
&q = y = 1.
&r = z = 2.

Now p=z means p=2 again p=++q pre-increment &q=y=1, p=++q=2.
Then,

q = ++p = 3.
After q=4.

z = ++q+p++.
z = 3+4.
z = 7.
p = 4.

So output is p=4, q=4 and z=7.

Amarnath said: (Sep 20, 2014)  
We can't change(++, --) enum values.

Bidyut said: (Mar 6, 2017)  
How a, b, c are given values?

Pritha said: (Sep 17, 2017)  
How can we assign the values of a, b, c in such a random manner?

Abhinav said: (Jan 14, 2018)  
Explain it.

Manimegalai said: (Apr 20, 2018)  
Please, anybody explain it.

Vishal Singh said: (Jun 17, 2018)  
It's the property of enum that they are initialized from 0 and stay in sequential increment order (i.e. A=0, b=1, c=2), also by enum property we can't change the value of enum after initialization.

Tavish said: (Aug 10, 2019)  
How can we random assigned the values to a b and c.

Minu said: (Nov 29, 2019)  
P=z=2.
P=++q=++1=2.
Q=++p=++2=3.
Z=++q+p++
Z=4+3=7.
Q=4.
P=3++=4.

Sandesh said: (May 20, 2020)  
How q==4? Please anyone explain it.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.