Skip to content

ISRO Scientist/Engineer CSE 2018Object-Oriented Programming and Java › Not yet assigned to a topic

ISRO Scientist/Engineer CSE 2018 PYQ: Consider the following C++ program int a (int m) {return ++m;} int b(int&m) {return ++m;} int{char &m}…

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. A
    results in compilation error
    Correct answer
  2. B
    print 123
  3. C
    print 111
  4. D
    print 322

Answer

(A) results in compilation error

Approach

Function b takes int& and function c takes char&, both non-const lvalue references. In q += b(a(q)), the call a(q) returns an int prvalue (temporary), and a non-const int& cannot bind to a prvalue, so this is ill-formed. In r += a(c(r)), r is an int lvalue but the parameter is char&; an int lvalue cannot bind to a char& (no reference-compatible types, and any conversion would require a temporary), another ill-formed construct. Since at least these two errors exist, the program fails to compile and produces no output, making option A correct.

Reviewed by ISRO IQ Editorial Team.