C# Questions & Answers ? Polymorphism
Posted by Superadmin on August 19 2022 13:18:05

C# Questions & Answers – Polymorphism

 

 

This section of our 1000+ C# multiple choice questions focuses on polymorphism in C# Programming Language.

 

 

 

 

1. The capability of an object in Csharp to take number of different forms and hence display behaviour as according is known as ___________
a) Encapsulation
b) Polymorphism
c) Abstraction
d) None of the mentioned

Answer: b
Explanation: None.

 

 

 

 

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

  1.  public class sample
  2.  {
  3.      public static int x = 100;
  4.      public static int y = 150;
  5.  
  6.  }
  7.  public class newspaper :sample
  8.  {
  9.      new public static int x = 1000;
  10.      static void Main(string[] args)
  11.      {
  12.          console.writeline(sample.x + "  " + sample.y + "  " + x);
  13.      }
  14.  }

a) 100 150 1000
b) 1000 150 1000
c) 100 150 1000
d) 100 150 100

Answer: c
Explanation: sample.x = 100
sample.y = 150
variable within scope of main() is x = 1000
Output :

 

  100  150  1000

 

 

 

 

3. Which of the following keyword is used to change data and behavior of a base class by replacing a member of the base class with a new derived member?
a) Overloads
b) Overrides
c) new
d) base

Answer: c
Explanation: None.
 

4. Correct way to overload +operator?
a) public sample operator + (sample a, sample b)
b) public abstract operator + (sample a,sample b)
c) public static sample operator + (sample a, sample b)
d) all of the mentioned

Answer: d
Explanation: None.

 

 

 

 

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

  1.  public class maths
  2.  {
  3.      public int x;
  4.      public virtual void a()
  5.      {
  6.  
  7.      }
  8.  
  9.  }
  10.  public class subject : maths
  11.  {
  12.      new public void a()
  13.      {
  14.  
  15.      }
  16.  
  17.  }

a) The subject class version of a() method gets called using sample class reference which holds subject class object
b) subject class hides a() method of base class
c) The code replaces the subject class version of a() with its math class version
d) None of the mentioned

Answer: d
Explanation: None.

 

 

 

 

6. Select the sequence of execution of function f1(), f2() & f3() in C# .NET CODE?

  1.  class base
  2.  {
  3.      public void f1() {}
  4.      public virtual void f2() {}
  5.      public virtual  void f3() {}
  6.  }
  7.  class derived :base
  8.  {
  9.      new public void f1() {}
  10.      public override void f2() {}
  11.      public new void f3() {}
  12.  } 
  13.  class Program
  14.  {
  15.      static void Main(string[] args)
  16.      {
  17.          baseclass b = new derived();
  18.          b.f1 ();
  19.          b.f2 ();
  20.          b.f3 ();
  21.      }
  22.  }

a)

   f1() of derived class get executed
   f2() of derived class get executed
   f3() of base class get executed

b)

   f1() of base class get executed
   f2() of derived class get executed
   f3() of base class get executed

c)

   f1() of base class get executed
   f2() of derived class get executed
   f3() of derived class get executed

d)

   f1() of derived class get executed
   f2() of base class get executed
   f3() of base class get executed

 

Answer: b
Explanation: None.

 
 

 

 

 

7. Which of the following statements is correct?
a) Each derived class does not have its own version of a virtual method
b) If a derived class does not have its own version of virtual method then one in base class is used
c) By default methods are virtual
d) All of the mentioned

Answer: c
Explanation: None.

 

 

 

 

8. Correct code to be added for overloaded operator – for the following C# code?

  1.  class csharp
  2.  {
  3.      int x, y, z;
  4.      public csharp()
  5.      {
  6.  
  7.      }
  8.      public csharp(int a ,int b ,int c)
  9.      {
  10.          x = a;
  11.          y = b;
  12.          z = c;
  13.      }
  14.      Add correct set of code here
  15.     public void display()
  16.     {
  17.         console.writeline(x + "  " + y + "  " + z);
  18.     }
  19.     class program
  20.     {
  21.         static void Main(String[] args)
  22.         {
  23.             csharp s1 = new csharp(5 ,6 ,8);
  24.             csharp s3 = new csharp();
  25.             s3 = - s1;
  26.             s3.display();
  27.         }
  28.     }
  29. }

a)

  1.     public static csharp operator -(csharp s1)
  2.     {
  3.         csharp t = new csharp();
  4.         t.x = s1.x;
  5.         t.y = s1.y;
  6.         t.z = -s1.z;
  7.         return t;
  8.     }

b)

  1.     public static csharp operator -(csharp s1)
  2.     {
  3.         csharp t = new csharp();
  4.         t.x = s1.x;
  5.         t.y = s1.y;
  6.         t.z = s1.z;
  7.         return t;
  8.     }

c)

  1.     public static csharp operator -(csharp s1)
  2.     {
  3.         csharp t = new csharp();
  4.         t.x = -s1.x;
  5.         t.y = -s1.y;
  6.         t.z = -s1.z;
  7.         return t;
  8.     }

d) None of the mentioned

Answer: c
Explanation: None.

 

 

9. Selecting appropriate method out of number of overloaded methods by matching arguments in terms of number, type and order and binding that selected method to object at compile time is called?
a) Static binding
b) Static Linking
c) Compile time polymorphism
d) All of the mentioned

Answer: d
Explanation: None.

 

 

 

 

10. Wrong statement about run time polymorphism is?
a) The overridden base method should be virtual, abstract or override
b) An abstract method is implicitly a virtual method
c) An abstract inherited property cannot be overridden in a derived class
d) Both override method and virtual method must have same access level modifier

Answer: c
Explanation: None.