Q:

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

belongs to collection: C#.Net find output programs

0

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.

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();
        }
    }
}

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();
        }
    }
}

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();
        }
    }
}

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();
        }
    }
}

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();
        }
    }
} 

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Answer 1:

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();

Answer 2:

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#.

Answer 3:

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.

Answer 4:

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.

Answer 5:

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.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C#.Net find output programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C#.Net find output programs (Classes & Objects) | ... >>
<< C#.Net find output programs (Structure) | set 3...