Computer Science - Object Oriented Programming Using C++ - Discussion


If p and q are assigned the values 2 and 3 respectively then the statement p = q++

[A]. gives an error message
[B]. assigns a value 4 to p
[C]. assigns a value 3 to p
[D]. assigns a value 5 to p
[E]. None of the above

Answer: Option C


No answer description available for this question.

Biswajit Borah said: (Oct 15, 2012)  
Here value of q is first assigned to p that is 3 is assigned to 3 then The value is incremented to 4 by q++.

Vikas Bansal said: (Dec 3, 2013)  
This is post increment operator, first value assigned and then increment occurs.

Akram said: (Jan 18, 2015)  
Please explain how could you possible that I think right answer is B.

Rama said: (Mar 16, 2015)  
As per to my knowledge assigning is occurs after initialization. But here the p = q++. q is initialized and then assigned to p.

The increment operator is post-fix. So at first the value is of p is 3.

Sharon said: (Nov 23, 2015)  
I am afraid, the answer must be option B.

Because when q is declared as 3, here it is also given an operation "p=q++" which shows that there is an increment to be done. So obviously 3 must be incremented form 3 to 4 so the answer must be p=4.

Rup said: (Oct 7, 2016)  
Here, q is having post increment operator. So, 3 is assigned to p first. Then only q is incremented.

Sadiq said: (Nov 13, 2016)  
Here postfix increment is used. If the statement is "p=++q" then the answer must be 4. But here the right answer is 3.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.