250+ TOP MCQs on Interfaces Implementation and Answers

C# multiple choice questions on implementation of the interfaces in C# Programming Language.

1. What will be the Correct statement in the following C# code?

advertisement

  1. interface a1
  2. {
  3.     void f1();
  4.     int f2();
  5. }
  6. class a :a1
  7. {
  8.     void f1()
  9.     {
  10.     }
  11.     int a1.f2()
  12.     {
  13.     }
  14. }

a) class a is an abstract class
b) A method table would not be created for class a
c) The definition of f1() in class a should be void a1.f1()
d) None of the mentioned

Answer: c
Clarification: None.

2. Choose the wrong statement about ‘INTERFACE’ in C#.NET?
a) An explicitly implemented member could be accessed from an instance of the interface
b) Interfaces are declared public automatically
c) An interface could not contain signature of the indexer
d) None of the mentioned

Answer: c
Clarification: None.

3. What will be the Correct statement in the following C# code?

  1.  interface a1
  2.  {
  3.      void f1();
  4.      void f2();
  5.  }
  6.  class a :a1
  7.  {
  8.      private int i;
  9.      void a1.f1()
  10.      {
  11.      }
  12.  }

a) Class a could not have an instance data
b) Class a is an abstract class
c) Class a fully implements the interface a1
d) None of the mentioned

Answer: b
Clarification: None.

advertisement

4. What will be the Correct statement in the following C# code?

  1.  interface abc
  2.  {
  3.      String FirstName
  4.      {
  5.          get;
  6.          set;
  7.      }
  8.     String LastName
  9.     {
  10.         get;
  11.         set;
  12.     }
  13.     void print();
  14.     void stock();
  15.     int fun();
  16. }

a) Functions should be declared inside an interface
b) It is workable code
c) Properties cannot be declared inside an interface
d) None of the mentioned

Answer: b
Clarification: None.

5. What will be the output of the following C# code?

  1.  interface calc
  2.  {
  3.      void cal(int i);
  4.  }
  5.  public  class maths :calc
  6.  {
  7.      public int x;
  8.      public void cal(int i)
  9.      {
  10.          x = i * i;
  11.      }
  12.  }
  13.  class Program
  14.  {
  15.      public static void Main(string[] args)
  16.      {
  17.          display arr = new display();
  18.          arr.x = 0;
  19.          arr.cal(2);
  20.          Console.WriteLine(arr.x);
  21.          Console.ReadLine();
  22.      }
  23.  }

a) 0
b) 2
c) 4
d) None of the mentioned

Answer: c
Clarification: None.
Output :

 4

6. What will be the output of the following C# code?

advertisement

  1.  interface calc
  2.  {
  3.      void cal(int i);
  4.  }
  5.  class displayA :calc
  6.  {
  7.      public int x;
  8.      public void cal(int i)
  9.      {
  10.          x = i * i;
  11.      }
  12.  }
  13.  class displayB :calc
  14.  {
  15.      public int x;
  16.      public void cal(int i)
  17.      {
  18.          x = i / i;
  19.      }
  20.  }
  21.  class Program
  22.  {
  23.      public static void Main(string[] args)
  24.      {
  25.          displayA arr1 = new displayA();
  26.          displayB arr2 = new displayB();
  27.          arr1.x = 0;
  28.          arr2.x = 0;
  29.          arr1.cal(2);
  30.          arr2.cal(2);
  31.          Console.WriteLine(arr1.x + " " + arr2.x);
  32.          Console.ReadLine();
  33.      }
  34.  }

a) 0 0
b) 2 2
c) 4 1
d) 1 4

Answer: c
Clarification: class displayA executes the interface calculate by doubling the value of item. Similarly class displayB implements the interface by dividing item by item. So, variable x of class displayA stores 4 and variable x of class displayB stores 1.
Output :

 4, 1

7. What will be the output of the following C# code?

  1. interface i1
  2. {
  3.     void fun();
  4. }
  5. interface i2
  6. {
  7.     void fun();
  8. }
  9. public class maths :i1, i2
  10. {
  11.     void i1.fun()
  12.     {
  13.         Console.WriteLine("i1.fun");
  14.     }
  15.     void i2.fun()
  16.     {
  17.         Console.WriteLine("i2.fun");
  18.     }
  19. }
  20. class Program
  21. {
  22.     static void Main(string[] args)
  23.     {
  24.         Sample obj = new Sample();
  25.         i1 i = (i1) obj;
  26.         i.fun();
  27.         i2 ii = (i2) obj;
  28.         ii.fun();
  29.     }
  30. }

a) i1.fun
b)

i2.fun
i1.fun

c) 0
d)

i1.fun
i2.fun

View Answer

Answer: d
Clarification: None.

 

advertisement

8. What will be the correct way to implement the interface in the following C# code?

  1.   interface abc
  2.    {
  3.        string name
  4.        {
  5.            get;
  6.            set;
  7.        }
  8.    }

a)

   class emp :employee
   {
       private string str;
       public string firstname;
       {
           get
           {
               return str;
           }
           set
           {
               str = value;
           }
       }
   }

b)

  class emp :implements person
  {
      private string str;
      public string firstname
      {
          get
          {
              return str;
          }
          set
          {
              str = value;
          }
      }
  }

c)

 class emp: implements person
 {
     private string str;
     public string person.firstname
     {
         get
         {
             return str;
         }
         set
         {
             str = value;
         }
     }
 }

d) None of the mentioned

Answer: a
Clarification: None.

9. What will be the output of the following C# code?

  1.  interface i1
  2.  {
  3.      void f1();
  4.  }
  5.  interface i2 :i1
  6.  {
  7.      void f2();
  8.  }
  9.  public class maths :i2
  10.  {
  11.      public void f2()
  12.      {
  13.          Console.WriteLine("fun2");
  14.      }
  15.      public void f1()
  16.      {
  17.          Console.WriteLine("fun1");
  18.      }
  19.  }
  20.  class Program
  21.  {
  22.      static Void Main()
  23.      {
  24.          maths m = new maths();
  25.          m.f1();
  26.          m.f2();
  27.      }
  28.  }

a) fun2
b) fun1
c)

fun1
fun2

d)

fun2
fun1

View Answer

Answer: c
Clarification: None.

 

10. In order to avoid ambiguity among an interface derived from two base interfaces with same method name(and signature), the right code among the following C# codes is?
a)

  1.  interface i1
  2.  {
  3.      void m1();
  4.  }
  5.  interface i2
  6.  {
  7.      void m1();
  8.  }
  9.  interface i3 :i1, i2
  10.  {
  11.  }
  12.  class c3 :i3
  13.  {
  14.      void i1.m1()
  15.      {
  16.      }
  17.      void i1.m1()
  18.      {
  19.      }
  20.  }

b)

 interface i1
 {
     void m1();
 }
     interface i2
     {
         void m1();
     }
     interface i3 :i1, i2
     {
     }
     class c3 :i3
     {
         void i1.i2.m1()
         {
         }
     }

c)

  interface i1
  {
      void m1();
  }
  interface i2
  {
      void m1();
  }
  interface i3 :i1, i2
  {
  }
  class c3 :i3
  {
      void i1.m1()
      {
      }
      void i2.m1()
      {
      }
  }

d) All of the mentioned

Answer: c
Clarification: None.

Leave a Reply

Your email address will not be published. Required fields are marked *