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

Find the output of C#.Net programs | Data Types | Set 2: Enhance the knowledge of C#.Net Data Types 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)
        {
            ubyte A = 10;
            ubyte B = 20;
            ubyte C = 0;

            C = A++ * ++B + 100;

            Console.WriteLine("A: " + A);
            Console.WriteLine("B: " + B);
            Console.WriteLine("C: " + C);
        }
    }
}

Output:

Error: The type or namespace name 'ubyte' could not be found 
(are you missing a using directive or an assembly reference?)

Explanation:

The above program will generate a syntax error. because ubyte is not a built-in data type in C#.

Question 2:

using System;

namespace Demo
{
    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            sbyte A = 10;
            byte  B = 20;
            int   C = 30;

            Console.WriteLine(A.GetType());
            Console.WriteLine(B.GetType());
            Console.WriteLine(C.GetType());
        }
    }
}

Output:

System.SByte
System.Byte
System.Int32
Press any key to continue . . .

Explanation:

In the above program, we created the variables A, B, and C initialized with 10, 20, and 30, and then print the System types of the variable using the GetType() method.

Question 3:

using System;

namespace Demo 
{
    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            sbyte A = 10;

            if (A.GetType().ToString() == "System.SByte")
                Console.WriteLine("Hello");
            else
                Console.WriteLine("Hiii");
        }
    }
}

Output:

Hello
Press any key to continue . . .

Explanation:

The above program will print "Hello" on the console screen. Here, we created a variable A of sbyte type and initialized with 10. The GetType() method will return System.SByte, and then we convert it into the string and compared with System.SByte. Then "Hello" 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)
        {
            float A = 10;

            if (A.GetType().ToString() == "System.Float")
                Console.WriteLine("Hello");
            else
                Console.WriteLine("Hiii");
        }
    }
}

Output:

Hiii
Press any key to continue . . .

Explanation:

The above program will print "Hiii" on the console screen. Here, we created a variable A of float type and initialized with 10. The GetType() method will return System.Single, and then we convert it into the string and compared with System.Float. Then the if condition will false and "Hiii" will be printed on the console screen.

Question 5:

using System;

namespace MyRefDemo
{
    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            byte VAL = 0;
            byte A = 13;
            byte B = 20;

            VAL = (byte)(A * B);

            Console.WriteLine(VAL);
        }
    }
}

Output:

4
Press any key to continue . . .

Explanation:

The above program will print "4" on the console screen. In the above program, we created three variables VAL, A, and B initialized with 0, 13, and 20.

Let's evaluate the expression,

VAL = (byte)(A * B);
VAL = (byte)(13*20);
VAL = (byte)(260);
VAL = 4;

As we know that the maximum value of a byte variable is 255, and we are assigning 260, then the value will we move in cyclic order. It means the value will move 5 numbers in cyclic order,

255 ->0 ->1 ->2-> 3->4





Comments and Discussions!

Load comments ↻





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