Q. What are OOPS principals?
Object oriented is an approach to do programming in C#, Java etc.
OOPS Principals –
1. Abstraction
2. Encapsulation
3. Inheritance
4. Polymorphism
Q. Is inheritance possible if private constructor declared in paren class?
Answer – No.
Q. What is Early Binding and Late Binding?
Early Binding = Method Overloading
Late Binding = Method Overriding
Read about Early Binding and Late Binding in OOPS Concept.
Q. How to call an Abstract Class function from a child class?
Ans – base.functionname();
class Test { static void Main(string[] args) { B b = new B(); b.Greetings(); } } abstract class A { public void Greetings() { Console.Write(“Hello User “); } } class B : A { public void Greetings() { base.Greetings(); Console.WriteLine(“Welcome to the Interview”); } } |
Q. Abstract vs Sealed Class
Abstract Class | Sealed Class |
An abstract class cannot be instantiated. | A sealed class cannot be used as a base class. For this reason, it cannot also be an abstract class. |
The purpose of an abstract class is to provide a common definition of a base class that multiple derived classes can share. | Sealed classes prevent derivation. Because they can never be used as a base class. |
Abstract methods have no implementation, so the method definition is followed by a semicolon instead of a normal method block. Derived classes of the abstract class must implement all abstract methods. | |
When an abstract class inherits a virtual method from a base class, the abstract class can override the virtual method with an abstract method. | |
Q. What is Abstract Class?
Abstract class is a class which contains Abstract or non abstract methods.
See below sample code –
In abstract class TestAbstract you may include abstract method or non abstract method.
Abstract methods are those method which doesn’t contains body i.e. abstract method has just an declaration.
If you have abstract method(s) in an abstract class and if that abstract class is a base class then child class must implement all the abstract methods available in Abstract class.
In below case, if you not implement Test1() method then compiler will throw an error.
class Program:TestAbstract { static void Main(string[] args) { } public override void Test1() { throw new NotImplementedException(); } } abstract class TestAbstract { public virtual void Test() { Console.WriteLine(“Hello”); } public abstract void Test1(); } |
Q. Use of virtual keyword.
Answer – virtual keyword is used to implement method overriding concept.
If you have declared a method with keyword virtual , then this method must override the method in child class. See below code.
public class Program : Class1
{
static void Main(string[] args)
{
}
public override void Method()//Access modifier is changed from protected to public
{
base.Method();
}
}
public class Class1
{
protected virtual void Method() //Here the virtual method is protected
{
Console.Write(“Hello”);
}
}
Q. Constructor vs Destructor
Destructor
It is called automatically when an object is no longer in use. This activity is automatically controlled by Garbage Collector.
Q. Abstract class vs Interface
Interface | Abstract |
An interface has no implementation. It only contains a signature. | Abstract Class cannot be instantiated. It allows other class to inherit from. |
A class cannot have multiple base class, i.e. a class cannot inherit multiple class. But a class can inherit multiple interfaces. i.e. Multiple inheritance supported by the interface. | |
An interface cannot have the implementation for any of its members. | An abstract can or cannot have the implementation of its members. |
Interface members cannot have access modifiers. | Abstract class members can have an access modifier. |
An interface cannot have any fields. | An abstract class can have fields. |
Q. Sealed and Virtual Class
Sealed class prevent itself from being inherited by other class.
Q. What is a Partial Class?
Partial class allows developers to split the class into 2 or more source files.
Below is the example.
Q. Static Constructor
Static constructor will be invoked only once for any number of instance of a class. A static constructor must be parameterless.
O/P- Static Constructor
Default Constructor
Default Constructor
Hello ! C#
Note: Default constructor executed twice, but static constructor invoked only once even non-parametrized constructor called 2 times.
Note: We can not create an object of the class which has a private constructor.
You may like other blogs –
Important ASP.Net Interview Questions and Answers
MVC Interview Questions and Answers
Top Web API interview questions and answers
C# Important interview questions and answers