Users Online
· Members Online: 0
· Total Members: 188
· Newest Member: meenachowdary055
Forum Threads
Latest Articles
Articles Hierarchy
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
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
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
Explanation: None.
-
namespace ConsoleApplication4
-
{
-
abstract class A
-
{
-
int i;
-
public abstract void display();
-
}
-
class B: A
-
{
-
public int j;
-
public override void display()
-
{
-
Console.WriteLine(j);
-
}
-
}
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
B obj = new B();
-
obj.j = 2;
-
obj.display();
-
Console.ReadLine();
-
}
-
}
-
}
a) 0
b) 2
c) Compile time error
d) 1
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?
-
namespace ConsoleApplication4
-
{
-
abstract class A
-
{
-
public int i ;
-
public int j ;
-
public abstract void display();
-
}
-
class B: A
-
{
-
public int j = 5;
-
public override void display()
-
{
-
this.j = 3;
-
Console.WriteLine(i + " " + j);
-
}
-
}
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
B obj = new B();
-
obj.i = 1;
-
obj.display();
-
Console.ReadLine();
-
}
-
}
-
}
a) 1, 5
b) 0, 5
c) 1, 0
d) 1, 3
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?
-
namespace ConsoleApplication4
-
{
-
abstract class A
-
{
-
public int i;
-
public abstract void display();
-
}
-
class B: A
-
{
-
public int j;
-
public int sum;
-
public override void display()
-
{
-
sum = i + j;
-
Console.WriteLine(+i + "\n" + +j);
-
Console.WriteLine("sum is:" +sum);
-
}
-
}
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
A obj = new B();
-
obj.i = 2;
-
B obj1 = new B();
-
obj1.j = 10;
-
obj.display();
-
Console.ReadLine();
-
}
-
}
-
}
a)
2, 10 12
b)
0, 10 10
c)
2, 0 2
d)
0, 0 0
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
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
Explanation: None.
9. What will be the output of the following C# code?
-
namespace ConsoleApplication4
-
{
-
public abstract class A
-
{
-
public int i = 7;
-
public abstract void display();
-
}
-
class B: A
-
{
-
public int j;
-
public override void display()
-
{
-
Console.WriteLine(i);
-
Console.WriteLine(j);
-
}
-
}
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
B obj = new B();
-
A obj1 = new B();
-
obj.j = 1;
-
obj1.i = 8;
-
obj.display();
-
Console.ReadLine();
-
}
-
}
-
}
a) 0, 8
b) 1, 8
c) 1, 7
d) 7, 1
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?
-
namespace ConsoleApplication4
-
{
-
class A
-
{
-
public int i;
-
public void display()
-
{
-
Console.WriteLine(i);
-
}
-
}
-
class B: A
-
{
-
public int j;
-
public void display()
-
{
-
Console.WriteLine(j);
-
}
-
}
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
B obj = new B();
-
obj.j = 1;
-
obj.i = 8;
-
obj.display();
-
Console.ReadLine();
-
}
-
}
-
}
a) 8, 1
b) 8
c) 1
d) 1, 8
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