C#.NET — What does the following program print (factorial computed via out parameter)? namespace CuriousTabConsoleApplication { class SampleProgram { static void Main(string[] args) { int i; int res = fun(out i); Console.WriteLine(res); } static int fun(out int i) { int s = 1; i = 7; for (int j = 1; j <= i; j++) { s = s * j; } return s; } } }
C# Programming
Functions and Subroutines
Difficulty: Easy
Choose an option
-
A1
-
B7
-
C8
-
D720
-
E5040
Answer
Correct Answer: 5040
Explanation
Introduction / Context:This program demonstrates using an out parameter to pass a value from a method while also returning a computed result. The loop calculates a factorial.
Given Data / Assumptions:
- fun sets i = 7 using an out parameter.
- It computes s as the product from 1 to i inclusive.
- Console.WriteLine prints the return value s.
Concept / Approach:Factorial of n is 1 * 2 * 3 * ... * n. Thus factorial of 7 is 5040.
Step-by-Step Solution:
Initialize s = 1.Loop j = 1 to 7: s = s * j.After loop: s = 5040.Return s; print 5040.Verification / Alternative check:Known value: 7! = 5040; quick multiplication confirms.
Why Other Options Are Wrong:
- 1: Only the initial value of s, not the factorial.
- 7, 8, 720: Do not equal 7!.
Common Pitfalls:Confusing the printed value with the out parameter i; the console prints the return value.
Final Answer:5040