C# Programming Interview Questions Tutorial
C# Object Oriented Programming Questions
Q. Can a private virtual method be overridden?
A. No, because they are not accessible outside the class.
Q. What is Encapsulation in C#?
A. Encapsulation is the basic concept of OOP. Encapsulation literally means putting stuffs inside a capsule. In programming terms, encapsulation is the process of putting data and functionality inside a single entity. For example, we put variables/fields (data) and method (functionality) inside a class. This is also referred as data hiding, because when we are putting data inside a class, we decide which stuff we want people to access from that class.
Data hiding is done using access modifier and using accessor and mutator or properties using getter setters.
Q. What is abstraction?
A. Abstraction is a technique of taking something specific and making it less specific.
So, in C#, for class we should make only those properties and methods visible to client, which is required by client to work properly. This is hiding unnessecery details.
From Microsoft docs, abstraction is:- "An abstraction is a type that describes a contract but does not provide a full implementation of the contract.". In C#, Abstractions are usually implemented as abstract classes or interfaces.
Q.What is Polymorphysim?
A. It literally means "many forms". Polymorphism allows you to define one interface and that can have multiple implementations, for example in method overloading, the method name is same, but based on different set of input parameters it can behave differently. It is same like a person can act as employee or as a friend based on place or situation.
Some example of polymorphysim is overloading (compile time polymorphysim), overriding (run time polymorphysim), using interface or abstract class to create new objects, casting the inheritance related classes with each other like upcast (downcast is not allowed in C#).
Q. Select correct form following unary operators which suppports overloading.
- new
- +
- is
- true
- false
A. Correct selection will be 2, 4 and 5.
Q. What it means when a method has an abstract keyword.
A. It means that the inherited class must override that method.
Q. What it means when a method has a virtual keyword.
A. It means that the inherited class can override that method.
Q. How to make a class inheritable but restrict it's specific method to override?
A. We can decorate that specific method with the sealed keyword.
Q. Can we use finally block before catch block?
A. No, it will give us a compile time error. However, you can use finally without a catch block, like try and finally block.
Q. If same method is declared in Parent and Child class, then in below case which method will be called? And why?
ParentClass pc = new ChildClass();
pc.Hello();// Hello is define in both classes
A. In this case method defined in parent class will be called, because here we are creating the object of child class but we are casting it as parent class (upcast), and object can call only those method which exist in the casted type, so parent class method will be called. You can try this code and verify it.
public class Program
{
public static void Main(string[] args)
{
ParentClass pc = new ChildClass();
pc.Hello();
}
}
public class ParentClass
{
public void Hello(){
Console.WriteLine("Hello from parent class");
}
}
public class ChildClass : ParentClass
{
public void Hello(){
Console.WriteLine("Hello from child class");
}
}
Output:
Hello from parent class
Q. What will be output of this?
public class Program
{
public static void Main(string[] args)
{
ParentClass pc = new ChildClass();
pc.Hello2();
}
}
public class ParentClass
{
public void Hello(){
Console.WriteLine("Hello from parent class");
}
}
public class ChildClass : ParentClass
{
public void Hello(){
Console.WriteLine("Hello from child class");
}
public void Hello2(){
Console.WriteLine("Hello2 from clild class");
}
}
A. This will give error. Here we are casting child class as parent class (upcasting) and then we are calling a method that is defined in child class. As we know that an object can call only those method which exist in the casted type, here it will not able to find Hello2 method and it will give error. Error will be "error CS1061: 'ParentClass' does not contain a definition for 'Hello2' and no accessible extension method 'Hello2' accepting a first argument of type 'ParentClass' could be found (are you missing a using directive or an assembly reference?)"
Q. What will be the output of below code?
public class Program
{
public static void Main(string[] args)
{
ChildClass cc = new ParentClass();
cc.Hello();
}
}
public class ParentClass
{
public void Hello(){
Console.WriteLine("Hello from parent class");
}
}
public class ChildClass : ParentClass
{
public void Hello(){
Console.WriteLine("Hello from child class");
}
}
A. It will give error, as Downcasting is not allowed in C#.
Q. What will be the output of below code?
public class Program
{
public static void Main(string[] args)
{
Doo(new ParentClass());
Doo(new ChildClass());
}
public static void Doo(ParentClass obj){
obj.Hello();
}
}
public class ParentClass
{
public void Hello(){
Console.WriteLine("Hello from parent class");
}
}
public class ChildClass : ParentClass
{
public void Hello(){
Console.WriteLine("Hello from child class");
}
}
A. Output will be:
Hello from parent class
Hello from parent class
Here It will also give a compilation warning "warning CS0108: `ChildClass.Hello()' hides inherited member `ParentClass.Hello()'. Use the new keyword if hiding was intended".