In C++ (aliases to the same actual parameter), a member function takes two int& parameters and mutates the first. What output is printed when the same variable is passed twice? #include<iostream.h> class CuriousTab { int x, y; public: void SetValue(int &a, int &b) { a = 100; // mutates caller x = a; y = b; Display(); } void Display() { cout << x << " " << y; } }; int main() { int x = 10; CuriousTab objCuriousTab; objCuriousTab.SetValue(x, x); // both refs alias the same variable return 0; }

Difficulty: Easy

Correct Answer: The program will print the output 100 100.

Explanation:


Introduction / Context:
This question demonstrates aliasing of reference parameters: both a and b reference the same caller variable, so writing through one reference affects the value seen through the other in the same call.


Given Data / Assumptions:

  • a and b both bind to main’s x.
  • a = 100 occurs before copying into members.


Concept / Approach:
Because a and b alias the same object, after a = 100, both a and b read back as 100. The object stores those copies into x and y and then prints them.


Step-by-Step Solution:

Caller x initially 10. Inside SetValue: a=100 → caller x becomes 100. x (member) = a = 100; y (member) = b = 100. Display prints "100 100".


Verification / Alternative check:
If b were a distinct variable, the second value would reflect the original b instead.


Why Other Options Are Wrong:
"100 10" would require b to remain 10, but b aliases the mutated variable.


Common Pitfalls:
Assuming parameters are evaluated independently when they are the same lvalue bound via references.


Final Answer:
The program will print the output 100 100.

More Questions from References

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion