C#.Net find output programs (Operators) | set 3

Find the output of C#.Net programs | Operators | Set 3: Enhance the knowledge of C#.Net Operators concepts by solving and finding the output of some C#.Net programs.
Submitted by Nidhi, on February 05, 2021

Question 1:

using System;

namespace Demo
{
    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            bool ret = false;
            int a=20;
            double pi = 3.14;

            ret = (a==20)&&(Math.PI==pi);

            Console.WriteLine(ret);
        }
    }
}

Output:

False
Press any key to continue . . .

Explanation:

The above program will print False on the console screen. In the above program, we created three variables ret, a, and pi, that are initialized with "false", "20", and "3.14" respectively.

ret = (a==20)&&(Math.PI==pi);

In the above expression, first condition (a==20) will be true, but the second condition (Math.PI==pi) will False, because Math.PI property will return "3.14159265358979" and the value of variable pi is 3.14 that's why False is assigned to the variable ret.

Then the final value of ret that is False will be printed on the console screen.

Question 2:

using System;

namespace Demo
{
    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            bool ret = false;
            int a=20;
            int b=0;
            string str="www.includehelp.com";

            ret = (a==20)||((b>=str.Length)?true:false);

            Console.WriteLine(ret);
        }
    }
}

Output:

True
Press any key to continue . . .

Explanation:

The above program will print "True" on the console screen. In the above program, we created four variables ret, a, b, and str that are initialized with false, 20, 0, and "www.includehelp.com" respectively.

Let's evaluate the expression:

ret = (a==20)||((b>=str.Length)?true:false);
ret = True||((b>=str.Length)?true:false);

In case of "||" operator, if first condition is True the second will not check. So finally True is assigned to variable ret and will be printed on the console screen.

Question 3:

using System;

namespace Demo
{
    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            bool ret = true;

            Console.WriteLine(ret&=true);
        }
    }
}

Output:

True
Press any key to continue . . .

Explanation:

The above program will print "True" on the console screen. Here, we created a variable ret which is initialized with true.

Console.WriteLine(ret&=true);

Let's evaluate the expression used in WriteLine() method,

ret &=true;
ret =ret &true;
ret =true &true;
ret = true;

Then finally "True" will be printed on the console screen.

Question 4:

using System;

namespace Demo
{
    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            Console.WriteLine(typeof(bool));
            Console.WriteLine(typeof(short));
        }
    }
}

Output:

System.Boolean
System.Int16
Press any key to continue . . .

Explanation:

In the above program, we used the typeof() operator, which is used to return System data types for the built-in C# data type.

Question 5:

using System;

namespace Demo
{
    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            int X = (1,2,3,4,5);

            Console.WriteLine(X);
        }
    }
}

Output:

main.cs(10,21): error CS8135: Tuple literal `(int, int, int, int, int)' 
cannot be converted to type `int'

Explanation:

The above program will generate errors because we cannot use the comma ',' operator for value assignment like this. We use this kind of assignment in C and C++ languages.





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.