Tuesday, October 23, 2012

Question 6: Predict the output.
    int& getIncrementedData(int data)
    {
        int incrementedData = data + 1;
        return incrementedData;
    }

    int main()
    {
        int& incrementedValue = getIncrementedData(10);
        std::cout<<"\nIncremented Value : "<<incrementedValue;
        incrementedValue = 12;
        std::cout<<"\nIncremented Value : "<<incrementedValue;
    }


    Options:
    a)     Compilation Error. Returning a local variable as reference.

    b)    Run time crash.

    c)     Incremented Value : 11
    Incremented Value : 12

    d)    None of the above.


    Answer: c

1 comment:

  1. I dont get ans c. I get 32 32 which im assuming is garbage.. cant return a reference to a local variable..

    ReplyDelete