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

Find the output of C#.Net programs | Operators | Set 2: 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)
        {
            float  A = 3.14F;
            double B = 3.14;

            int RES = 0;

            RES = (A == B);

            Console.WriteLine(RES);
        }
    }
}

Output:

main.cs(15,20): error CS0029: Cannot implicitly convert type `bool' to `int'

Explanation:

The above program will generate syntax error because the relational operator returns boolean values in C#. Here, we used relation operator (==) to check equality.

RES = (A == B);

In the above expression, RES is an integer variable, and here we assigned a boolean value to RES, then it will generate an error.

Question 2:

using System;

namespace Demo
{
    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            float  A = 3.14F;
            double B = 3.14;
            
            (A == B) ? Console.WriteLine("Hello") : Console.WriteLine("Hiiii");
        }
    }
}

Output:

main.cs(13,22): error CS0201: Only assignment, call, increment, decrement, await, 
and new object expressions can be used as a statement

Explanation:

The above program will generate syntax error, because "conditional operator" required LVALUE for assignment and return type of WriteLine() method is void also.

Question 3:

using System;

namespace Demo
{
    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            float  A = 3.14F;
            double B = 3.14;
            int ret = 0;
            
            ret = (A == B) ? 1 :0;

            Console.WriteLine(ret);
        }
    }
}

Output:

0
Press any key to continue . . .

Explanation:

In the above program, we created three variables A, B, and ret that are initialized with "3.14F", "3.14", and 0 respectively. Here, data type of A is float and data type of B is double then (A==B) condition will false and 0 assigned to variable ret. So finally "0" will print on the console screen.

Question 4:

using System;

namespace Demo
{
    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            float  A = 3.14F;
            double B = 3.14;
            int ret = 0;
            
            ret = (A == (float)B) ? sizeof(int)==sizeof(float)?10:20:30;

            Console.WriteLine(ret);
        }
    }
}

Output:

10
Press any key to continue . . .

Explanation:

The above program will print 10 on the console screen. In the above program, we created three variables A, B, and ret that are initialized with "3.14F", "3.14", and 0 respectively.

Let's evaluate the conditional operator,

ret = (A == (float)B) ? sizeof(int)==sizeof(float)?10:20:30;

Here, we typecast B into float type, then (A == (float)B) condition will true then we check below condition,

sizeof(int)==sizeof(float)

The size of int and float is 4 then this condition will true and 10 is assigned to the variable ret.

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

Question 5:

using System;

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

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

            Console.WriteLine(ret);
        }
    }
}

Output:

main.cs(14,19): error CS0019: Operator `&&' cannot be applied 
to operands of type `int' and `bool'

Explanation:

The above program will generate syntax error because in the below expression, we used assignment operator "=" instead of "equal to" operator "==", that why 20 is assigned to the variable "a", and we know that we cannot apply "&&" operator between int and bool operands.

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


ADVERTISEMENT
ADVERTISEMENT




Comments and Discussions!




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.