OOPs based C Sharp Questions


Back to Notes on .NET

C# General Interview Questions

C# Questions on properties, events etc

Questions on XML, ADO.NET etc

C# Rapid Fire Questions 1

C# Rapid Fire Questions 2

SQL Interview Questions 1

SQL Interview Questions 2

Random .NET Interview Questions

Download Ebooks

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Class Questions

1.What is the syntax to inherit from a class in C#?
Ans.Place a colon and then the name of the base class.
Example: class MyNewClass : MyBaseClass
 
2.Can you prevent your class from being inherited by another class?
Ans.Yes.  The keyword “sealed” will prevent the class from being inherited.
 
3.Can you allow a class to be inherited, but prevent the method from being over-ridden?
Ans.Yes.  Just leave the class public and make the method sealed.
 
4.What’s an abstract class?
Ans.A class that cannot be instantiated.  An abstract class is a class that must be inherited and have the methods overridden.  An abstract class is essentially a blueprint for a class without any implementation.
 
5.When do you absolutely have to declare a class as abstract?
Ans.
1. When the class itself is inherited from an abstract class, but not all base abstract methods have been overridden.
2.  When at least one of the methods in the class is abstract.
 
6.What is an interface class?
Ans.Interfaces, like classes, define a set of properties, methods, and events. But unlike classes, interfaces do not provide implementation. They are implemented by classes, and defined as separate entities from classes.
 
7.Why can’t you specify the accessibility modifier for methods inside the interface?
Ans.They all must be public, and are therefore public by default.
 
8.Can you inherit multiple interfaces?
Ans.Yes.  .NET does support multiple interfaces.
 
9.What happens if you inherit multiple interfaces and they have conflicting method names?
Ans.It’s up to you to implement the method inside your own class, so implementation is left entirely up to you. This might cause a problem on a higher-level scale if similarly named methods from different interfaces expect different data, but as far as compiler cares you’re okay. 
 
10.What’s the difference between an interface and abstract class?
Ans.In an interface class, all methods are abstract - there is no implementation.  In an abstract class some methods can be concrete.  In an interface class, no accessibility modifiers are allowed.  An abstract class may have accessibility modifiers.
 
11.What is the difference between a Struct and a Class?
Ans.Structs are value-type variables and are thus saved on the stack, additional overhead but faster retrieval.  Another difference is that structs cannot inherit.
 
Method and Property Questions

1.What’s the implicit name of the parameter that gets passed into the set method/property of a class?
Ans.Value.  The data type of the value parameter is defined by whatever data type the property is declared as.
 
2.What does the keyword “virtual” declare for a method or property?
Ans.The method or property can be overridden.
 
3.How is method overriding different from method overloading?
Ans.When overriding a method, you change the behavior of the method for the derived class.  Overloading a method simply involves having another method with the same name within the class.
 
4.Can you declare an override method to be static if the original method is not static?
Ans.No.  The signature of the virtual method must remain the same.  (Note: Only the keyword virtual is changed to keyword override)
 
5.What are the different ways a method can be overloaded?
Ans.Different parameter data types, different number of parameters, different order of parameters.
 
6.If a base class has a number of overloaded constructors, and an inheriting class has a number of overloaded constructors;can you enforce a call from an inherited constructor to a specific base constructor?
Ans. Yes, just place a colon, and then keyword base (parameter list to invoke the appropriate constructor) in the overloaded constructor definition inside the inherited class.
 
Events and Delegates

1.What’s a delegate?
Ans.A delegate object encapsulates a reference to a method.
 
2.What’s a multicast delegate?
Ans.A delegate that has multiple handlers assigned to it.  Each assigned handler (method)  

PREV NEXT