Users Online

· Guests Online: 65

· Members Online: 0

· Total Members: 188
· Newest Member: meenachowdary055

Forum Threads

Newest Threads
No Threads created
Hottest Threads
No Threads created

Latest Articles

C# Questions & Answers ? Abstract Class & Methods

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 its own objects but acts as a base class for its subclass is known as?
a) Static class
b) Sealed class
c) Abstract class
d) None of the mentioned

Answer: c
Explanation: None.

 

 

2. The modifier used to define a class which does not have objects of its own but acts as a base class for its subclass is?
a) Sealed
b) Static
c) New
d) Abstract

Answer: d
Explanation: Here,

 

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 either static or virtual modifiers
c) An abstract method can be declared only in abstract class
d) All of the mentioned

Answer: d
Explanation: None.
 
4. What will be the output of the following C# 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

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

 

 

 

 

5. What will be the output of the following C# 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

Answer: d
Explanation: obj.i = 1 initializes value of i as 1 as it is the abstract member of abstract class ‘A’. Now,’j’ is also a same member as class ‘A’. Since it is initialized the value of 5 when declared in subclass. But since abstract method is redefined in subclass using ‘this’ keyword as this. j = 3, 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 of the following C# 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

 

Answer: c
Explanation: Abstract method implementation is processed in subclass ‘B’. Also the object ‘obj’ of abstract class ‘A’ initializes value of i as 2. The object of class ‘B’ also initializes 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 functions then it is known as?
a) Abstract
b) A simple class
c) Static class
d) None of the mentioned

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

 

 

 

 

 

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

Answer: b
Explanation: None.

 

 

 

 

 

9. What will be the output of the following C# 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

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 executes display() method.
Output :

 

 7, 1.

 

 

 

 

10. What will be the output of the following C# 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

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

 

Comments

No Comments have been Posted.

Post Comment

Please Login to Post a Comment.

Ratings

Rating is available to Members only.

Please login or register to vote.

No Ratings have been Posted.
Render time: 0.78 seconds
10,844,569 unique visits