C# Programming - Functions and Subroutines - Discussion

Discussion :: Functions and Subroutines - General Questions (Q.No.7)

7. 

What will be the output of the C#.NET code snippet given below?

namespace IndiabixConsoleApplication
{
    class SampleProgram
    {
        static void Main(string[ ] args)
        {
            int i = 10;
            double d = 34.340;
            fun(i);
            fun(d);
        }
        static void fun(double d)
        {
            Console.WriteLine(d + " ");
        }
    }
}

[A]. 10.000000 34.340000
[B]. 10 34
[C]. 10 34.340
[D]. 10 34.34

Answer: Option D

Explanation:

No answer description available for this question.

Akki said: (May 1, 2013)  
10 will be called automatically and then while calling d it eliminates 0 because it contains double quotes at the consolewriteln.

Praneet said: (Jun 30, 2014)  
Answer Why its not C : The floating-point data types do not have any internal representation of trailing zero characters. For example, they do not distinguish between 4.2000 and 4.2. Consequently, trailing zero characters do not appear when you display or print floating-point values.

Lavanya said: (Jan 28, 2015)  
I did not get this answer. Please can any one give how print 10 before 34.34.

Kartik said: (Jan 29, 2015)  
I too want know how int type was called by fun().

Shaikh Salman said: (Aug 27, 2016)  
I did not know the answer, how it gives the output 10 before 34.34 when int i not call by function?
Please help me.

Sachin Shrivastava said: (Dec 18, 2016)  
Didn't get it. Please elaborate.

Prasanth S said: (Mar 7, 2017)  
When fun(i) is called then print the value of 'i' like 10 first and then the second function is executed to print 34.34.

Anindita said: (Aug 28, 2017)  
The first thing is, fun(i) is getting called, though the parameter defined here as double as in fun(double d).

Because,fun(i) is implicitly converting int i from int to double.

Secondly,double does not understand the difference between 34.340 and 34.34 so it removes trailing zeros.

But if you want to still display the trailing zeros you have to specify the format inside Console.WriteLine.

You can refer the following example:

namespace IndiabixConsoleApplication
{
class SampleProgram
{
static void Main(string[] args)
{
int i = 10;
double d = 34.340;
fun(i);
fun(d);
Console.ReadKey();
}
static void fun(double d)
{
Console.WriteLine(d.ToString("##.000"));
Console.WriteLine(d + " ");
}

}
}

Omik said: (Jan 4, 2018)  
Let's Make it simple.

Actual Output : 10 34.340

If in case,
You have initialized value of i and d as follows;

int i = 10;
double d = 34.00;
fun(i);
fun(d);

The Output will be:
10
34

Even though 34 is double because if numbers are zero's after decimal then
It will consider the only number a before decimal place.
Here, 34.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.