recategorized by
3,819 views
3 votes
3 votes

Consider the following C++ program

int a (int m)
{return ++m;}
int b(int&m)
{return ++m;}
int{char &m}
{return ++m;}
void main()
{             
       int p = 0, q=0, r = 0;
       p += a(b(p)) ;
       q+= b(a(q);)
       r+=a(c(r));
       cout<<p<<q << r;
}

Assuming the required header first are already included, the above program

  1. results in compilation error
  2. print $123$
  3. print $111$
  4. print $322$
recategorized by

2 Answers

3 votes
3 votes

Answer should be A. But ISRO answer key has answer D. The statements q+=b(a(q)) and r+=a(c(r)) will result in compilation error. 

0 votes
0 votes

Temporaries cannot be bound to non-constant references means when 

int &z = 12;  //this is wrong here 12 is temporary.a temporary object of type int is created from the integral literal 12, but the temporary cannot be bound to non-const reference. Hence the error.
int y;
int &r = y;//but this is correct

In 1st expression p += a(b(p)) , directly calling b function means we are assigning value of object type.but in 2nd and 3rd expression, we are passing temporary values i.e  q+= b(a(q));// first b function calls,then m becomes 3,and that 3(temporary value) we r assigning  to b function parameter that is not valid.same in the case of 3. so option A  (compile error)

Temporaries only  be bound to  constant references 

const int &z = 12;

 

Answer:

Related questions

3 votes
3 votes
2 answers
2
Arjun asked Apr 22, 2018
3,773 views
The difference between a named pipe and a regular file in Unix is thatUnlike a regular file, named pipe is a special fileThe data in a pipe is transient, unlike the conte...
2 votes
2 votes
1 answer
3
1 votes
1 votes
2 answers
4
Arjun asked Apr 22, 2018
5,245 views
The Functions Point (FP) metric isCalculated from user requirementCalculated from lines of codeCalculated from software complexity assessmentNone of the above