Click here to Skip to main content
15,896,201 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
What can be the possible output of Console.WriteLine() in below code and in what scenarios?

static void Check(out int a, out int b)
{
a = 2;
b = 3;
Console.WriteLine (a == b);
}

What I have tried:

as I ran it in VS , it simply gave false as the answer.
But this was asked in a puzzle so I think there would be true possible for this but in a specific scenario that I am looking forward to have the explanation for
Posted
Updated 3-Aug-16 19:20pm
Comments
[no name] 2-Aug-16 3:19am    
Do you have a link to the "puzzle"?
Richard MacCutchan 2-Aug-16 4:47am    
The use of the out keyword has no bearing on the answer, which will always be false, since 2 is not equal to 3.

Returns True when function is called as below

int a;
Check(out a, out a);

Returns False in all other cases.

This is what I got to know after the answer was revealed. !!!
 
Share this answer
 
In my opinion t is always false.

In this method you always set a to 2 and b to 3.
After that you compare if they are equal and thanks to the method settings (a=2, b=3), it is always the same.

Create this in C# and test with various scenarios to be sure tho
 
Share this answer
 
Comments
Learning_to_code 2-Aug-16 3:03am    
I have tried this in VS and it returned false, That is pretty much clear.
But the beauty/trick lies in the understanding of Out keyword used here.

The question is under which scenario does this snippet have "true" as answer and there should be no change to the snipped put above.
Evosoul04 2-Aug-16 3:11am    
The out keyword works like this:

You call the method like this:
int a = 5;
int b = 6;
Check(a,b);
after that method a is 2 and b is 3.

The out keyword simply says that the Value of the Variables are "returned". So the 2 is returned to a and the 3 is returned to b.

The answer to the question is that without a change the Console.Writeline is never true.

For the out keyword try to implement the example above and make a Console.Writeline which shows the values a and b after the Check method.
Learning_to_code 4-Aug-16 1:03am    
Returns True when function is called as below -

int a;
Check(out a, out a);

Returns False in all other cases.
I think interviewer was trying to confuse you by using OUT keyword. As in the program it is written a=2 and b=3 so it will be false obviously.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900