Skip to content
Question of 97

Q.Find and write the output of the following C++ program code: Note : Assume all required header files are already included in the program. void Convert(float &X, int Y=2) { X=X/Y; Y=X+Y; cout<<X<<"*"<<Y<<endl; } void main() { float M=15, N=5; Convert(M,N); Convert(N); Convert(M); }

CBSECBSE Class XII Board 2019Subjective· 3mImportance★★★★★est
0% · 0/97 Questions
🔒 Locked · start free trial →

You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.

Start your 14-day free trial to unlock the full solution →

This program demonstrates call-by-reference and default function arguments in C++. The Convert function modifies its first argument directly, leading to the output:

3*8

2.5*4

1.5*3

The core concepts at play here are call-by-reference for the first parameter X and default arguments for the second parameter Y. Understanding how these work is key to tracing the program's execution.

When a parameter is passed by reference (like float &X), any changes made to X inside the Convert function directly modify the original variable in the main function that was passed to it. This is different from call-by-value, where a copy is made, and the original variable remains unchanged.

The int Y=2 in the function signature means that if the Convert function is called with only one argument, the compiler will automatically use 2 as the value for Y. If two arguments are provided, the second argument will override this default value.

Let's trace the program's execution step-by-step:

  1. Initial State in main:

    • float M = 15;
    • float N = 5;

    At this point, M holds 15.0 and N holds 5.0.

  2. First Call: Convert(M, N);

    • The Convert function is called.
    • X (a reference) is bound to M. So, X now refers to M's memory location, and its initial value is 15.0.
    • Y (a value parameter) receives the value of N. So, Y is 5.
    • Inside Convert:
      • X = X/Y;
        • This becomes M = 15.0 / 5;
        • M (and X) is updated to 3.0.
      • Y = X+Y;
        • This becomes Y = 3.0 + 5;
        • Y (the local parameter) is updated to 8.
      • cout<<X<<"*"<<Y<<endl;
        • This prints the current value of X (which is 3.0) followed by * and then the current value of Y (which is 8).
        • Output: 3*8

    After this call, M is 3.0, and N remains 5.0. The local Y inside Convert is destroyed.

  3. Second Call: Convert(N);

    • The Convert function is called.
    • X (a reference) is bound to N. So, X now refers to N's memory location, and its initial value is 5.0.
    • Since only one argument is provided, Y takes its default value of 2.
    • Inside Convert:
      • X = X/Y;
        • This becomes N = 5.0 / 2;
        • N (and X) is updated to 2.5.
      • Y = X+Y;
        • This becomes Y = 2.5 + 2;
        • Y (the local parameter) is updated to 4.
      • cout<<X<<"*"<<Y<<endl;
        • This prints the current value of X (which is 2.5) followed by * and then the current value of Y (which is 4).
        • Output: 2.5*4

    After this call, M is 3.0, and N is 2.5. The local Y inside Convert is destroyed. …

Unlock everything free for 14 days

  • Full step-by-step solutions
  • Concept-first explanations
  • Methods, shortcuts & mistakes
  • PYQ mapping + timed mock tests

Full access for 14 days. No credit card required.