+ 2

How the answer is 8 for the following code:

static int Vol(int x, int y=3, int z=1) { return x*y*z; } static void Main(string[] args) { Console.WriteLine(Vol(2, 4)); }

22nd May 2017, 9:00 AM
Rishabh saxena
Rishabh saxena - avatar
3 Answers
+ 22
x=2 and y=4 is passed to Vol function Now it needs z which is taken as 1 by Vol function by default. so 2*4*1=8 It will take y=3 when you don't specify the value for y..[like no z in Vol(2,4) is passed and hence z=1] //Default arguments
22nd May 2017, 9:02 AM
Frost
Frost - avatar
+ 9
Vol(2, 4) gives the parameters x=2 and y=4 to the function. Now x is 2, y is 4 and not 3, and z is still 1. 2*4*1 = 8
22nd May 2017, 9:03 AM
Tim G
Tim G - avatar
0
Given the information above (values), the answer will always be 8 due to the order of operations. Even by placing part of the equation in parenthesis so that those items are considered first, the value is still 8. The only way you will get a value other than 8 would be if one of the multiplication symbols were changed to another operation such as addition or subtraction.
22nd May 2017, 11:18 PM
Bob
Bob - avatar