C#.NET — Multiple return values via function: identify compile-time errors in call and return. Program: namespace CuriousTabConsoleApplication { class SampleProgram { static void Main(string[] args) { int a = 5; int s = 0, c = 0; s, c = fun(a); Console.WriteLine(s + " " + c); } static int fun(int x) { int ss, cc; ss = x * x; cc = x * x * x; return ss, cc; } } } Which statements are correct?

Difficulty: Easy

Correct Answer: 1, 5

Explanation:


Introduction / Context:
This problem highlights that standard C# functions return a single value. It asks you to identify the compile-time errors introduced by attempting to assign two variables from a single function call and by attempting to return two values using a comma expression.


Given Data / Assumptions:

  • Main tries s, c = fun(a);
  • fun is declared as static int fun(int x) but attempts return ss, cc;
  • We are working in standard C# without tuples or out/ref in the signature.


Concept / Approach:
In C#, a method returns exactly one value unless you use out/ref parameters or newer tuple-return syntax (not shown here). The comma between variables in an assignment target is not valid in this context. Likewise, return ss, cc; is not valid to return two values; the method returns int and must return a single int expression.


Step-by-Step Solution:

Statement 1: "An error will be reported in s, c = fun(a);" — Correct; multiple-target assignment in this form is illegal. Statements 2–4: claim concrete outputs; impossible because the code does not compile. Statement 5: "An error will be reported in return ss, cc;" — Correct; return expects one expression of type int, not two.


Verification / Alternative check:
Legal alternatives: use out parameters (e.g., fun(a, out s, out c)) or return a ValueTuple <int,int> and deconstruct: (s, c) = fun(a); with fun returning (int ss, int cc).


Why Other Options Are Wrong:

  • B/C include claims of successful output.
  • E ignores the presence of the two compile-time errors.


Common Pitfalls:
Assuming comma-separated returns or multi-target assignment works in C# without tuples or out/ref.


Final Answer:
1, 5

More Questions from Functions and Subroutines

Discussion & Comments

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