C#.Net find output programs (Classes & Objects) | set 1

Find the output of C#.Net programs | Classes & Objects | Set 1: Enhance the knowledge of C#.Net Classes & Objects concepts by solving and finding the output of some C#.Net programs.
Submitted by Nidhi, on February 08, 2021

Question 1:

using System;

namespace Demo
{
    class Sample
    {
        int A;
        int B;

        public void set(int a, int b)
        {
            A = a;
            B = b;
        }

        public void print()
        {
            Console.WriteLine(A + "," + B);
        }
    }
    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            Sample S = Sample();

            S.set(10,20);
            S.print();
        }
    }
}

Output:

main.cs(26,24): error CS0119: Expression denotes a `type', 
where a `variable', `value' or `method group' was expected

Explanation:

The above program will generate syntax error because we did not create an object of the class sample correctly. We need to use a new operator to create an object of the class. The correct way is given below,

Sample S = new Sample();

Question 2:

using System;

namespace Demo
{
    class Sample
    {
        int A;
        int B;

        public void set(int a, int b)
        {
            A = a;
            B = b;
        }

        public void print()
        {
            Console.WriteLine(A + "," + B);
        }
    }S;

    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            S.set(10,20);
            S.print();
        }
    }
}

Output:

main.cs(20,5): error CS1525: Unexpected symbol `S'
main.cs(22,9): error CS1514: Unexpected symbol `class', expecting `.' or `{'
main.cs(31,0): error CS1525: Unexpected symbol `}'

Explanation:

The above program will generate syntax error because we tried to create an object in C++ fashion, which is not allowed in C#.

Question 3:

using System;

namespace Demo
{
    class Sample
    {
        int A;
        int B;

        public void set(int a, int b)
        {
            A = a;
            B = b;
        }

        public void print()
        {
            Console.WriteLine(A + "," + B);
        }
    }

    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            Sample S = new Sample();
            S.set(10,20);
            S.print();
        }
    }
}

Output:

10,20
Press any key to continue . . .

Explanation:

In the above program, we created a class Sample that contains two private integer data members. Here, we created two methods set() and print() inside the Sample class.

The set() method is used to set values inside the data members, and the print() method is used to print values of data members on the console screen.

Now look at the Program class, the Main() method contains inside the Program class, which is used as an entry point for the program.

In the Main() method, we created an object S of Sample class and set values 10 and 20 into A and B using set() method, and finally print the values of A and B on the console screen.

Question 4:

using System;

namespace Demo
{
    class Sample
    {
        int A=5;
        int B=10;

        public void set(int a, int b)
        {
            A = a;
            B = b;
        }

        public void print()
        {
            Console.WriteLine(A + "," + B);
        }
    }

    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            Sample S = new Sample();
            S.print();
        }
    }
}

Output:

5,10
Press any key to continue . . .

Explanation:

In the above program, we created a class Sample that contains two private integer data members A and B that are initialized with 5 and 10 respectively. Here, we created two methods set() and print() inside the Sample class.

The set() method is used to set values inside the data members, and the print() method is used to print values of data members on the console screen.

Now look at the Program class, the Main() method contains inside the Program class, which is used as an entry point for the program.

In the Main() method, we created an object S of Sample class and print the values of A and B on the console screen.

Question 5:

using System;

namespace Demo
{
    class Sample
    {
        int A=5;
        int B=10;

        public void set(int a, int b)
        {
            A = a;
            B = b;
        }

        public void print()
        {
            Console.WriteLine(A + "," + B);
        }
    }

    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            Sample S1 = new Sample();
            Sample S2 = new Sample();
            
            S1.print();
            S2.print();
        }
    }
} 

Output:

5,10
5,10
Press any key to continue . . .

Explanation:

In the above program, we created a class Sample that contains two private integer data members A and B that are initialized with 5 and 10 respectively. Here, we created two methods set() and print() inside the Sample class.

The set() method is used to set values inside the data members, and the print() method is used to print values of data members on the console screen.

Now look at the Program class, the Main() method contains inside the Program class, which is used as an entry point for the program.

In the Main() method, we created two objects S1 and S2 of Sample class and print the values of A and B on the console screen.





Comments and Discussions!

Load comments ↻





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