C# Questions & Answers – Interfaces Implementation

«
»

This section of our 1000+ C# multiple choice questions focuses 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
View Answer

Answer: c
Explanation: None.
Note: Join free Sanfoundry classes at Telegram or Youtube
advertisement
advertisement

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
View Answer

Answer: c
Explanation: None.

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

advertisement
  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
View Answer

Answer: b
Explanation: 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
View Answer

Answer: b
Explanation: None.
advertisement

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
View Answer

Answer: c
Explanation: None.
Output :

 4

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

  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
View Answer

Answer: c
Explanation: 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
Explanation: None.
 
 

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
View Answer

Answer: a
Explanation: 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
Explanation: 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
View Answer

Answer: c
Explanation: None.

Sanfoundry Global Education & Learning Series – C# Programming Language.
Here’s the list of Best Books in C# Programming Language.

To practice all features of C# programming language, here is complete set on 1000+ Multiple Choice Questions and Answers on C#

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.