All Exams Test series for 1 year @ ₹349 only
Question

A function xyz is defined as :

Void xyz (int a = 0, int b, int c = 0) 

           cout << a << b << c; 

}

Which of the following calls are illegal ?
(Assume h, g are declared as integers)
(a) xyz( );

(b) xyz (h, h) ;

(c) xyz (h);

(d) xyz (g, g);
Codes :

The correct answer is
(b) and (d) are correct

Understanding C++ Default Arguments in Function Calls

The function definition is $Void xyz (int a = 0, int b, int c = 0)$.

Key C++ rules for default arguments:

  • Parameters with default values must appear at the end of the parameter list. (Note: The provided signature technically violates this rule as $b$ is between $a$ and $c$, but we proceed based on the question's premise).
  • When calling a function, arguments are matched from left to right.
  • A parameter without a default value must always be provided an argument during the function call.
  • If you skip an argument for a parameter with a default value, all subsequent parameters must also be skipped (and thus rely on their default values or must also have defaults).

Analyzing Function Calls for Legality

Let's analyze each call based on the rules, considering the parameter $b$ requires a value:

Call (a): $xyz( );$

  • This call provides zero arguments.
  • The parameter $b$ does not have a default value and is not provided.
  • Result: Illegal

Call (b): $xyz (h, h) ;$

  • This call provides two arguments.
  • The first argument $h$ is assigned to $a$.
  • The second argument $h$ is assigned to $b$.
  • The parameter $c$ is omitted and takes its default value $0$.
  • Since $b$ receives a value, this call is valid.
  • Result: Legal

Call (c): $xyz (h);$

  • This call provides one argument.
  • The argument $h$ is assigned to $a$.
  • The parameter $b$ is omitted. Since $b$ lacks a default value, it must be supplied.
  • Result: Illegal

Call (d): $xyz (g, g);$

  • This call provides two arguments.
  • The first argument $g$ is assigned to $a$.
  • The second argument $g$ is assigned to $b$.
  • The parameter $c$ is omitted and takes its default value $0$.
  • Since $b$ receives a value, this call is valid.
  • Result: Legal

Conclusion on Call Legality

Based on the analysis:

  • Calls $xyz (h, h)$ and $xyz (g, g)$ are legal because the mandatory parameter $b$ receives a value.
  • Calls $xyz( )$ and $xyz (h)$ are illegal because the mandatory parameter $b$ does not receive a value.

Therefore, the calls $(b)$ and $(d)$ are the ones considered valid (correct) in this context.

Was this answer helpful?

Important Questions from Programming in C

  1. Which among the following is a valid string function?

  2. A function which calls itself is called a

  3. Which of the following function sets first n characters of a string to a given character?

  4. Which of the following statement provides an easy way to dispatch execution to different parts of code based on the value of the expression?

  5. Which of the following operators has left to right associativity?

Need Expert Advice?

Start Your Preparation with Prepp Mobile App

Download the app from Google Play & App Store
Download the app from Google Play & App Store
Prepp Mobile App