C# Questions & Answers – Abstract Class & Methods

This section of our 1000+ C# multiple choice questions focuses on abstract class and methods in C# Programming Language.

1. A type of class which does not have it’s own objects but acts as a base class for it’s subclass is known as?
a) Static class
b) Sealed class
c) Abstract class
d) None of the mentioned
View Answer

Answer :c
Explanation : None.

2. The modifier used to define a class which does not have objects of it’s own but acts as a base class for it’s subclass is?
a) Sealed
b) Static
c) New
d) abstract
View Answer

Answer :d
Explanation : abstract class Base
{

}
class derived : Base
{
}
Base b1; /*object of Base class which can never be possible */
Derived d1; /*object of derived class which is possible */

3. Choose the correct statements among the following:
a) An abstract method does not have implementation
b) An abstract method can take only either static or virtual modifiers
c) An abstract method can be declared only in abstract class
d) All of the mentioned
View Answer

Answer :d
Explanation :None.

4. What will be the output for given set of code?

  1.  namespace ConsoleApplication4
  2.  {   
  3.      abstract class A 
  4.      {
  5.          int i;
  6.          public  abstract void display();
  7.      }    
  8.      class B: A 
  9.      {
  10.          public  int j;
  11.          public override void display() 
  12.          {
  13.              Console.WriteLine(j);
  14.          }
  15.      }    
  16.      class Program
  17.      {
  18.          static void Main(string[] args)
  19.          {
  20.              B obj = new B();
  21.              obj.j = 2;
  22.              obj.display();
  23.              Console.ReadLine();
  24.          }
  25.      }
  26.  }

a) 0
b) 2
c) Compile time error
d) 1
View Answer

Answer :b
Explanation :Here in abstract class ‘A’ abstract method display() is declared and it’s full implementation i.e definition is given in subclass of class ‘A’.
Output :2

5. What will be the output for given set of code?

  1.  namespace ConsoleApplication4
  2.  {
  3.      abstract class A
  4.      {
  5.          public int i ;
  6.          public int j ;
  7.          public abstract void display();
  8.      }    
  9.      class B: A    
  10.      {
  11.          public int j = 5;
  12.          public override void display() 
  13.          {
  14.              this.j = 3;
  15.              Console.WriteLine(i + "  " + j);
  16.          }
  17.      }    
  18.      class Program
  19.      {
  20.          static void Main(string[] args)
  21.          {
  22.              B obj = new B();
  23.              obj.i = 1;
  24.              obj.display();     
  25.              Console.ReadLine();
  26.          }
  27.      }
  28.  }

a) 1, 5
b) 0, 5
c) 1, 0
d) 1, 3
View Answer

Answer :d
Explanation : obj.i = 1 initializes value of i as 1 as it is abstract member of abstract class ‘A’. Now,’j’ is also same member as class ‘A’ since it is initialized value of 5 when declared in subclass but since abstract method is redefined in subclass using ‘this’ keyword as this.j = 3.So,method will execute only abstract class member ‘j’ not subclass ‘B’ own defined data member ‘j’.
Output : 1, 3

6. What will be the output for given set of code?

  1. namespace ConsoleApplication4
  2. {   
  3.     abstract class A 
  4.     {
  5.         public int i;
  6.         public abstract void display();
  7.     }    
  8.     class B: A 
  9.     {
  10.         public  int j;
  11.         public int sum;
  12.         public override void display() 
  13.         {
  14.             sum = i + j;
  15.             Console.WriteLine(+i + "\n" + +j);
  16.             Console.WriteLine("sum is:" +sum);
  17.         }
  18.     }    
  19.     class Program
  20.     {
  21.         static void Main(string[] args)
  22.         {
  23.             A obj = new B();
  24.             obj.i = 2;
  25.             B obj1 = new B();
  26.             obj1.j = 10;
  27.             obj.display();
  28.             Console.ReadLine();
  29.         }
  30.     }
  31. }

a) 2, 10
12
b) 0, 10
10
c) 2, 0
2
d) 0, 0
0
View Answer

Answer :c
Explanation: Abstract method implementation processed in subclass ‘B’ also the object ‘obj’ of abstract class ‘A’ initialize value of i as 2.The object of class ‘B’ also intialize value of j as 10.Since, the method display() is called using object of class A which is ‘obj’ and hence i = 2 whereas j = 0 .So, sum = 2.
Output : 2 0
sum is : 2

7. If a class inheriting an abstract class does not define all of its function then it will be known as?
a) abstract
b) A simple class
c) Static class
d) None of the mentioned
View Answer

Answer :a
Explanation : Any subclass of an abstract class must either implement all of the abstract method in the super class or be itself declared abstract.

8. Which of the following modifier is used when an abstract method is redefined by a derived class?
a) Overloads
b) Override
c) Base
d) Virtual
View Answer

Answer :b
Explanation :None.

9. What will be the output for given set of code?

  1.  namespace ConsoleApplication4
  2.  {
  3.      public abstract class A
  4.      {
  5.          public int i = 7;
  6.          public abstract void display();
  7.      }    
  8.      class B: A 
  9.      {
  10.          public int j;
  11.          public override void display() 
  12.          {
  13.              Console.WriteLine(i);
  14.              Console.WriteLine(j);
  15.          }
  16.      }    
  17.      class Program
  18.      {
  19.          static void Main(string[] args)
  20.          {
  21.              B obj = new B();
  22.              A obj1 = new B();
  23.              obj.j = 1;
  24.              obj1.i = 8;
  25.              obj.display();
  26.              Console.ReadLine();
  27.          }
  28.      }
  29.  }

a) 0, 8
b) 1, 8
c) 1, 7
d) 7, 1
View Answer

Answer :d
Explanation : Data member ‘i’ of abstract class A will be preferred over variable initialized and executed by obj1 as obj1.i = 8 as ‘obj’ of class B execute display() method.
Output : 7, 1.

10. What will be the output for given set of code?

  1.  namespace ConsoleApplication4
  2.  {
  3.      class A
  4.      {
  5.          public int i;
  6.          public void display()
  7.          {
  8.              Console.WriteLine(i);
  9.          }
  10.      }    
  11.      class B: A 
  12.      {
  13.          public int j;
  14.          public void display() 
  15.          {
  16.              Console.WriteLine(j);
  17.          }
  18.      }    
  19.      class Program
  20.      {
  21.          static void Main(string[] args)
  22.          {
  23.              B obj = new B();
  24.              obj.j = 1;
  25.              obj.i = 8;
  26.              obj.display();
  27.              Console.ReadLine();
  28.          }
  29.      }
  30.  }

a) 8, 1
b) 8
c) 1
d) 1, 8
View Answer

Answer :c
Explanation :class A & class B both contain display() method, class B inherits class A, when display() method is called by object of class B, display() method of class B is executed rather than that of Class A.
Output: 1.

Sanfoundry Global Education & Learning Series – C# Programming Language.

Here’s the list of Best Reference 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#.

About

Manish Bhojasia, a technology veteran with 17+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer and SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage & Cluster Administration, Advanced C Programming, SAN Storage Technologies, SCSI Internals and Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him below.


Sanfoundry Training Center of Excellence (CoE)

Manish Bhojasia, a technology veteran with 17+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer and SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage & Cluster Administration, Advanced C Programming, SAN Storage Technologies, SCSI Internals and Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him below.







Subscribe Newsletter & Posts
Sanfoundry Internships
Follow Manish & Sanfoundry