Q:

C#.Net find output programs (Interface) | set 1

belongs to collection: C#.Net find output programs

0

Find the output of C#.Net programs | Interface | Set 1: Enhance the knowledge of C#.Net Interface concepts by solving and finding the output of some C#.Net programs.

Question 1:

using System;

namespace Demo
{
    interface IMath
    {
        public void Add(int a, int b);
    }

    class Math : IMath
    {
        public void Add(int a, int b)
        {
            Console.WriteLine("Addition: " + (a + b));
        }
    }

    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            Math M = new Math();

            M.Add(10,20);
        }
    }
}

Question 2:

using System;

namespace Demo
{
    interface IMath
    {
        void Add(int a, int b);
    }

    class Math : IMath
    {
        public void Add(int a, int b)
        {
            Console.WriteLine("Addition: " + (a + b));
        }
    }

    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            Math M = new Math();

            M.Add(10,20);
        }
    }
}

Question 3:

using System;

namespace Demo
{
    interface IMath
    {
        virtual void Add(int a, int b);
    }

    class Math : IMath
    {
        public override void Add(int a, int b)
        {
            Console.WriteLine("Addition: " + (a + b));
        }
    }

    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            Math M = new Math();

            M.Add(10,20);
        }
    }
}

Question 4:

using System;
namespace Demo
{
    interface IMath
    {
        void Add(int a, int b);
        void Sub(int a, int b);
    }

    class Math : IMath
    {
        public override void Add(int a, int b)
        {
            Console.WriteLine("Addition: " + (a + b));
        }
        public override void Sub(int a, int b)
        {
            Console.WriteLine("Subtraction: " + (a-b));
        }
    }

    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            Math M = new Math();

            M.Add(10,20);
	     M.Sub(30,20);
        }
    }
}

Question 5:

using System;

namespace Demo
{
    interface IMath
    {
        void Add(int a, int b);
        void Sub(int a, int b);
    }

    class Math : IMath
    {
        public void Add(int a, int b)
        {
            Console.WriteLine("Addition: " + (a + b));
        }
        public void Sub(int a, int b)
        {
            Console.WriteLine("Subtraction: " + (a-b));
        }
    }

    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            IMath M = new IMath();

            M.Add(10,20);
        }
    }
}

All Answers

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

Answer 1:

Output:

main.cs(7,21): error CS0106: The modifier `public' is not valid for this item

Explanation:

The above program will generate syntax error because we cannot use public or any other access modifier with the method declaration contained in the interface.

Answer 2:

Output:

Addition: 30
Press any key to continue . . .

Explanation:

In the above program, we created an interface IMath that contains the declaration of Add() method then we inherited the IMath interface in Math class and implemented Add() method in the Math class.

Let's look to the Main() method, here I created an object of Math class and called Add() method that will print the addition of given arguments.

Answer 3:

Output:

main.cs(7,22): error CS0106: The modifier `virtual' is not valid for this item

Explanation:

The above program will generate syntax error because we cannot use the virtual modifier with the method declaration contained in the interface.

Answer 4:

Output:

main.cs(12,30): error CS0115: `Demo.Math.Add(int, int)' is marked as an override but no suitable method found to override
main.cs(16,30): error CS0115: `Demo.Math.Sub(int, int)' is marked as an override but no suitable method found to override

Explanation:

The above will generate compile-time errors because we cannot use the override keyword to implement the method of the interface. Actually, there is no need to use the override keyword.

Answer 5:

Output:

main.cs(28,23): error CS0144: Cannot create an instance of the abstract class or interface `Demo.IMath'
main.cs(5,15): (Location of the symbol related to previous error)

Explanation:

The above program will generate a syntax error because of the below statement,

IMath M = new IMath();

In the above statement, we tried to create the instance of an interface, but it is not possible in C#.

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 (Interface) | set 2... >>
<< C#.Net find output programs (Inheritance) | set 3...