OOPS interview questions and answers in C# .NET
1)What is the difference between Object based programming and Object oriented programming?
-if a language supports only encapsulation then it is called as object based programming
language
ex:vb
-if a language supports encapsulation,inheritance and polymorphism then it is called as
object oriented programming language.
ex:c#,java,c++
2)What are the main concepts of oops?
There are four main concepts in oops
a)Encapsulation
b)Inheritance
c)Polymorphism
d)Abstraction
3)What is encapsulation and its advantage?
-Grouping of related things together is called as encapsulation.
-due encapsulation we can achieve portability.
4)What is Data hiding?
-Hiding unnecessary information from user is called as datahiding.
-due to data hiding we can protect behaviour of component.
-data hideing can be implemented by using access modifiers.
5)What is abstraction?
-Hiding complete implementation details from user is called as abstraction.
-Due to abstraction we can make user to feel easy and confortable while using component.
6)What is the difference between data hiding and abstraction?
-DataHiding is hiding unnecessary information from user,so that we can protect behaviour
of component.
-Abstraction is hiding of complete implementation details,so that component usage becomes
easy.
7)What is the difference between encapsulation and class?
-Encapsulation is oops concept but class is feature of language by which we can implement
encasulation.
8)How to implement encapsulation in C#?
-By using class,struct,interface,enum.
9)What are the access specifiers ? (or) What are the access modifiers?
There are six access modifiers
a)private
b)protected
c)Public
d)internal.
e)protected internal
d)private protected.
10)What is the behavior of protected internal?
-protected internal is combination of two access modifiers.
-with in same assembly it will be like a internal,outside assembly it will be like a
protected.so that we can make us of it in any class of same assembly and only derived
classes outside assembly.
10)What is the behavior of private protected ?
- it is available from C#7.2
-protected internal is combination of two access modifiers.
-within in same assembly by the code in same class and can access it from its derived class
11)What is object?
-it is instance of class.
12)What is min size of object if there is no member in class?
-8bytes
13)What is default access modifier for class?
-internal
14)What is default access modifier for members in class?
-private
15)What is the base class for all classes in .net?
-System.Object.
15)What is the difference between value types and reference types?
Value type store the values in stack.
Value type directly stored the values rater than address.
ex: int y =100;
Reference type store the values in heap.
Reference type store the address of the value ,actually value stored in other location.
ex: string name="Shiva";
16)What is the difference between class and structure?
Class struct
-it is reference type -it is value type
-all access modifiers allowed -protected not allowed.
-all types of methods allowed -Virtual and abstract methods not allowed.
-destructor is allowed -destructor not allowed.
-all types of constructors allowed -default constructor not allowed.
-Inheritance possible -inheritance not posssible
-it can implement interface -it also can implement interface.
17)Is it possible to define method in enum type?
-no
18)How to initialize object?
-in two ways we can initialize object.
a)by using object initializer.
b)By using Constructor.
19)What are accessor and mutator methods?
-a method which is designed to get value from variables of class is called as accessor method.
-a method which is designed to modify values of variables of class is called as mutator method.
20)What is the difference between property and indexer?
-property is member of class or struct which provides flexible mechanism to read and write from and to the variable of class or struct.
-it can be static.
-it can not be overloaded.
-Indexer is also member of class or struct which is also providing flexible mechanism to read and write from and to the variable of class or struct by using subscript and index.
-indexer can not be static.
-always name of indexer must be this,and it can be overloaded
21)Is it possible to define property as static?
-yes
22)Is it possible to define indexer as static?
-no
23)What is the use of base keyword?
-with the help of base keyword we can access base class members from derived class.
-allways base keyword will refer to immediate base class members.
-we can call base class constructor from derived class constructor.
24)What is Constructor?
-it is special method in the class which is invoked automatically when the class has been instantiated.
-it can be used to initialize instance variables of class to some meaningfull initial values
once object has been created.
25)Why name of constructor and class name must be same?
-to make the compiler to identify it easily.
26)Why return type is not allowed for constructor at programmer level?
-constructor should not be used for any other purpose,it must be used for only initialization.
27)What are different types of constuctors available?
-static constructor
-non static constructor
a)default constructor
b)parametric constructor.
28)Explain static constructor?
-A constructor can be defined as static member.
-static constructor should not have access modifier
-it must be parameter less.
-it can not be overloaded.
-it can be used to initialize static variables of class but not non static variables.
-it is invoked one and only once when the class has been loaded into appDomain by class loader.
29)When the static constructor is invoked?
-it is invoked one and only once when the class has been loaded into appDomain by class loader.
30)Is it possible to overload static constructor?
-no
31)What is the need of copy constructor?
-it is used to initialize new object with the help of an existing object when new object has been created.
32)What is destructor and when is it invoked?
-it is also special method which is invoked automatically before object destruction takes place by Garbage collector.
-the name of the destructor and the name of the class must be same.
-it must be preceded by ~ character.
-access modifiers can't be applied
-It must be parameter less
33)What is disadvantage of destructor?
-the destructor in the class is represented by finalize( )
-when GC decide to deallocate the memory then it will check is there any finalize() defined for the object if so then the object will be moved to freechable queue.
- finalize() of object is invoked which intern invokes finalize() of system.Object
-finalize() of system.object invokes GC to deallocate memory,so that it ill be extra burdden to the GC when the destructor has been defined for the class.
34)When the class is loaded in to AppDomain?
- the class can be loaded into the application domain in the following situations
a)when the first object of the class has been created
b)when the static method of the class has been invoked.
35)How to invoke base class constructor in Derived class constructor?
- by using base Keyword.
36)What is namespace?
-It is a logical container which can contain classes, interfaces, enums, deligates, structures
-with the help of the namespace we can avoid name collisions.
- A namespace can contain another name space also.
37)Can we apply access modifier to namespace?
-No
38)Is it possible to declare instace variables in namespace?
-No
39)What is alias name for namespace and when is it required?
-While including too many namespaces in the application with the help of using keyword then there is a chance of duplicate members.
- too avoid the conflict we can asign a temporary name to the duplicate member which is called as alias name for the namespace.
40)What is the difference between namespace and class?
-namespace is logical container which can contain classes,interfaces,enums,structs...
-members of namespace will be either public or internal
-class is feature of language which is used to implement encasulation
-all access modifiers are allowed on members of class
41)What is difference between out and ref parameters?
Out ref
-it is write only varibale in method -it is read and write variable in method.
-it must be preceded by out keyword -it must be preceded by ref keyword
-it need not to be initialized while passing it -it must be initialized while passing to method.
to method - changes are reflected
-Changes are reflected
42)What is the purpose of params key word?
- To pass the array of parameter to the method we can make use of params Keyword.
43)What is binding?
- Replacing the method invokation with its appropriate base reference is called as binding or linking
44)How many types of bindings are available?
- Ther eare two types of binding
a)Static Binding
If the binding process has been carried out during the compilation then it is called as Static binding
b)Dynamic Binding
If the binding process has been carried out at the time of runing the application then it is called as dynamic binding
45)What is Method overloading?
-defining number of methods with same name and different signatures is called as Method Overloading
-Method Overloading is possible with in the same class as well as its derived classes.
-Any types of methods can be Overloaded.
-No Keyword must be required for the method.
46)What is Method overriding?
-Defining number of method with same name, same signature and same return type with differnt functionality is called as method Overriding.
-It is Possible with in the derived classes only
-Only virtual and Abstract methods only can be Overrided
-override Keyword must be used.
47)What is difference between Method overloading and Method overriding?
48.What is polymorphism?
- Having Diffrent forms to the same method is called as Polymorphism.
-With the help of polymorphism we can perform different actions with same method invokation
49)What are the types of polymorphism?
-There are two types of polymorphism
a)static Polymorphism
- If the Polymorphism is implemented by using Static binding then it is Static Polymorphism
-In C# Static polymorphism can be implemented by using method Overloading and operator Overloading
b)Dynamic polymorphism
-If polymorphism is implemented by using dynamic binding then it is called as Dynamic Polymorphism
-In C# Dynamic polymorphism is implemented by using method Overriding
50)What is the difference between abstract class and interface?
Abstract Class Interface
-It is Partially unimplemented class -Fully Unimplemented class
-Complete Abstraction is not Possible -Complete Abstraction is possible
-All Types of members are allowed -Only methods, Properties and indexes are allowed
-Object can not be created -Object cant not be created
-Access modifier can be applied to the members -Access modifiers can not be applied explictly, public is the default access modifier
of the abstract class
-Constrctor, destructor are allowed -Constrctor, destructor are not allowed
51)Can we apply private or protected access modifier to interface?
-No
52)Why access modifiers can not be applied to members of interface?
- By default members of interface are public and members of the interface must be always exposed.
53)Why object can not be created for abstract class or interface?
-Abstract classes are Partially unimplemented so that some of the members are not defined
-Interface is fully unimplemented so that non of the member will have the implementation
-If the object creation is made possible then we need to call the members with out implementation. Thus object creation for abstract class and interface must be avoided
54)Can we declare member of interface as static?
-No
55)Why multiple inheritance is not possible for classes?
-Deriving a new class from two or more bases classes is called multiple inheritance
-when the class is derived from too many base classes then there is a chance of duplicate memberes with full implementation
-when that member is invoked w.r.t derived class object then there would be a ambigious situation.
-to avoid this ambigious situationmultiple inheritance in classes is not allowed in c#.
56)What is private implementation for interface member?
-while implementing the interface members if there are defined w.r.t interface name then it is called as private implimentation
-In this implementation members must be private members of the class so that interface members can be invoked w.r.t interface variable name only but not w.r.t object of implemented class.
-It is also called as explicit implimentation of interface
57)What is sealed class?
-If a class is declared as sealed class then it can not be extended i.e inheritance is not possible for that class
58)What is Extension method and how to define it?
-If a method is defined to provide extra functionality to existing type with out disturbing its originallity then it is called as extension method.
-it is introduced from C# 3.0
-It must be static method
-IT must be defined with in the static class
-it must have the parameter of the type to be extended
-parameter must be preceeded by this keyword
59)Can we Overload destructor?
-No
60)What is the use of Dispose()?
-It is used to invoke the GC dynamically.
61)What is the difference between String and StringBuilder classes?
- Both are used to manipulate and maintain the string
-each method of the string will return another string object
-each method of string builder will manipulate with in the same object with out returning another string object
62)What is the advantage of var keyword?
- it is introduced from C# 3.0
-It is used to declare the local variables
-var type of variable must be intialized during the declaration itself.
-Type of variable will be decided based on its intialization
-It can not be intialized with null, but it can contain the reference of object
-var type of variable can not be passed as a parameter to the method.
63)Can we assign null to var type of variable?
--No
64)Can u allow class to be inherited,but method must be prevented from being overridden?
65)What is the difference between abstract method and virtual method?
abstract method virtual method
-Abstract Keyword must be used -virtual keyword must be used
-It should not have defination in the base class -It must have the defination in the base class
-It must be overridden in the derived classes -It may or may not be in the derived classes
-It must be with in the abstract class -It can be with in the abstract and non abstract classes
66)Can we define virtual method as private member?
-No
67)Can we define abstract method as private member?
-No
68)Can we define abstract method as static?
-No
69)Can we declare private class in namespace?
-No, members of the namespace must be either internal or public
70)What is the difference between new and override keywords?
- New is used to create the object dynamically
-Override key word is used to override either virtual or abstract methods
71)Can we Create object with out defining class?
-Yes, we can create an object by usinf new operator with some intialization.
-Based on intialization approriate annonomous type will be defined by the compiler.
72)What is the purpose of this?
-it is a predefined reference variable which contain the reference of a current object or active object.
- when the names of the parameters and instance variable names are same then instance variables can be differentiated from the parameters by
using this keyword.
-This can be used with instance methods only.
73)What is the difference between Array and ArrayList?
Array ArrayList
-it is typed collection -it is untyped collection
-fixed size -size will be increased dynamically.
-boxing and unboxing not needed -extra burden of boxing and unboxing.
74)What is difference between List and ArrayList?
List ArrayList
-it is generic collection -it is non genaric collection
-boxing and unboxing is not needed -Extra burden of boxing and unboxing
-size will be inbcreased dynamically -size will be increased dynamically
75)What is the diffference between Hashtable and SortedList?
-both the collections are defined as Dictionaries where in elements are stored in the form of (key,value) pair,but in sortedList values are maintained according to keys in ascending order.
76)What is assembly?
-it is language independent,self described portable software component.
77)Is IL language is OO?
-no
78)What are different types of assemblies?
-there are three types of assemblies
a)private assembly
-an assembly with out strong name key is called as private assembly
-when private assembly is used in application,then copy of it will be available as part of application.so that memory will be wasted when too many application are running at a time.
-if some changes are made in assembly then they are not reflected to application until its compilation.
b)public or shared assembly
-an assembly with strong name key and placed in GAC is called as shared assembly or public assembly.
-Advantages:
-no memory wastage if even too many application using same assembly are running.
-if some changes are made to assebly then those changes are automatically reflected to application with out recompilation
c)satilite asembly
-it is private or public assembly with resource file.
-it is used while creating assembly for multi linguistic purpose.
79)What is digital signature of an assembly?
-it is combination of private key and hash algorithem value of aseembly.
-digital signature of an assembly will be checked while installing it into GAC.
80)What is PE file?
-an assembly with win32 header is called as PE file
-it is microprocessor independent.
81)What is ilasm.exe?
-it is a tool which is used to convert assembly into PE file.
82)What is ILDASM.exe?
-it is also a tool which is used to see the content of assemby in text format.
83)What are advantages of genarics?
-Boxing and unboxing burden can be reduced while working with collections.
-Code burden on developer can be reduced.
84)What is boxing and unboxing?
-Converting value type into reference type is called as boxing.
-During boxing type casting will be done implicitly
-Converting reference type to value type is called as unboxing
-during unboxing type casting must be done explicitly
85)What are the disadvantages of boxing and unboxing?
-During boxing and unboxing values must be copied from method stack to managed heap and viceversa.
-During unboxing if typecasting is not done properly then it will raise exception called InvalistCastException.
86)What is delegate?
-it is reference type which represents method.
-delegate object can contain reference of method
-with the help of delegate object we can invoke a method dynamically based on its reference.
87)What are different types of delegates?
there are two types of delegates
a)single cast delegate:
-if delegate object contains only one method reference then it is called as single cast delegate.
b)multi cast delegate:
-if delegate object contains more than one method reference then it is called as multicast delegate.
-with the help of multicast delegate we can invoke multiple methods sequentially with single call.
88)What is lambda expression?
-representing complete method defination with the help of simple expression is called as lambda expression
89)What are diiferent types of lambda expressions?
-there are two types of lambda expressions
a)predicate:
A lambda expression which is designed to return either true or false is called predicate
b)projection:
A lambda expression which is designed to return other than true or false is called projection.
90)What is Annynomous method?
A method which is assumed by the compiler based on the defination of the delegate and defination of the method given by developer is called as annynomous method.
91)What is dll hell problem?
-in windows environment all application dll s are copied to system32 folder of windows directory,so that there is a chance of overwriting one application dll by another application dll.
-if share dll s are used by multiple applications then due to uninstallation of one application another application may be effected which is called as dll hell problem.
92)Explain different parts of assembly version?
Assembly version contains four parts
a)major:this part indicates the new namespace,class ,interface ,delegate insertion into assembly
b)minor:this part indicates the new methods ,properties insertion into assembly
c)Build:this part indicates number of times bugs fixed in assembly
d)revision:this part indicates how many times assembly has been rebuilt irrespective of bugs reported or not
93)What is the purpose of checked and unchecked blocks?
-checked block is used to provide a piece of code where there is a chance of raising OverFlowException.
-Unchecked block does raise OverFlowException.
94)What is typeof()?
-it is operator which is used to extract complete type information dynamically.
-it will return System.Type object.
95)What is difference between function and event in C#?
Events are like triggers or actions that are associated with an object. For example for a button, Click is an event. For DropDownList, SelectedIndexChanged is an event. The code that runs against an event, is known as Event Handler
Methods are a block of statements.Which is called by prgrammers either using by object or classname.
96) Extension method code example?
namespace Extentionmethod
{
Public static class Extention
{
public static int WordCount(this String str)
{
return str.Split(new char[] { ' ', '.', '?' },StringSplitOptions.RemoveEmptyEntries).Length;)
}
}
}
string s = "Hello Extension Methods";
int i = s.WordCount();
97)What is ASP.NET Dynamic Data?
ASP.NET Dynamic Data is a framework. using this you create data-driven ASP.NET Web applications easily.
It does this by automatically discovering data-model metadata at run time and deriving UI behavior from it.
Scaffolding is best example for this.
98)Can we use same method name , what we have in base class without overriding the method?
yes we can ues with help of new keyword.
public class BCl
{
public void DoWork() { WorkField++; }
public int WorkField;
public int WorkProperty
{
get { return 0; }
}
}
public class DerivedClass : BaseClass
{
public new void DoWork() { WorkField++; }
public new int WorkField;
public new int WorkProperty
{
get { return 0; }
}
}
99)Use of sealed keyword in C#?
using sealed keyword stop the class from inheritance.
Another is we can stop inheritence of virtual methods to its derived classes
class A
{
public virtual void dis() { }
public virtual void display()
{
Console.WriteLine("I am from virtaul base class");
}
}
class B : A
{
public override sealed void display()
{
Console.WriteLine(" I am from derived");
}
}
class D : B
{
// Class D not getting the methods of B
}
100)Can we declare class as private in C#?
yes ,only for nested class
class Program
{
static void Main(string[] args)
{
}
private class Private_ClassExample
{
}
}
101) What Do you mean Delegate Invoke/BeginInvoke in C# ?
Delegate.Invoke: Executes synchronously, on the same thread.
Delegate.BeginInvoke: Executes asynchronously, on a threadpool thread.
102)Can we create nullable types based on reference type?
No, we can not create nullable types to reference type, We create nullable types only for value types
103)Can we use dynamic type as method parameter ?
yes.
class Program
{
public void Dispaly(dynamic a)
{
Console.WriteLine(a);
Console.WriteLine("I am from dynamic");
}
static void Main(string[] args)
{
Program Pobj = new Program();
Pobj.Dispaly("Rama");
Console.ReadLine();
}
}
104)Difference between where and any clauses linq in C# ?
Where returns a new sequence of items matching the predicate.
Any returns a Boolean value;
105)Can we do implicit conversation with Var keyword?
No
-explicit conversation is possible.
-with Dynamic keyword its possible.
106)What is the difference between obj and bin folder in C#?
Obj :This folder contain a temporary files or intermediatory files which is generated by compiler during the copilation and it is useful when do next build if source does not have any change.
bin :This folder contains result of the build its contain exe files , xml files etc
107)How to use Multicast Delegates in C#?
Using multicast delegates executing multiple methods at one time that methods may belongs to same class or diffrent classes.
In multicast delegates one delegate points to one function
Added all the delegates using + operator and assign this another delegate
namespace Multicast_Delegates
{
public delegate void muldel();
class GeneralInfo
{
public void DisplayFN()
{
Console.WriteLine("Gummadidala");
}
public void displayLN()
{
Console.WriteLine("Shiva");
}
}
public class Addtional
{
public void Qualification()
{
Console.WriteLine("MCA");
}
}
public class Program
{
static void Main(string[] args)
{
GeneralInfo pobj = new GeneralInfo();
muldel ml, add, res, qul;
ml = pobj.DisplayFN;
add = pobj.displayLN;
Addtional aobj = new Addtional();
qul = aobj.Qualification;
res = add + ml + qul;
res.Invoke();
Console.Read();
}
}
}
108)What is private Constructor in C#?
If class contains atleast one private constructor inheritence not possible to that constructor.
If a class has private constructor and no public constructor, other classes cannot create instances of this class.
Private constructor is used in classes that contain static members only.
109)What is default access modifier for constructor in C# ?
If class is private , default access modifier of constructor is private.
Generally accessibility member is not greater than the type that contains it.
110)How to implementing few methods of a interface in C# ?
public interface I1
{
void m1();
void m2();
void m3();
}
abstract class Program : I1
{
public void m1()
{
}
public abstract void m2();
public abstract void m3();
}
111)What is the difference between constant , readonly , static readonly varibales in C#?
Constant:
Constant variables are represent with const keyword.
we need to assign the values before compilation.
public class ConstantReadonly
{
public const int a = 5;
}
Readonly:
Readonly variables change at runtime with in constructors not with static constructors,
if you want , you can assign it at compile time but you cannot change.
public class ConstantReadonly
{
public readonly int b;
public ConstantReadonly()
{
b = 3;
}
}
Static readonly:
A Static Readonly type variable's values change at runtime with static constructors , not with instance constructors
if you want , you can assign it at compile time but you cannot change.
public class ConstantReadonly
{
public static readonly int b;
static ConstantReadonly()
{
b = 3;
}
}
112)Reverse of a number logic ?
int i = 153;
int n = 0,sum = 0;
while (i > 0)
{
n = n * 10;
n = n+ i%10;
i = i / 10;
}
Console.WriteLine( n);
113)What is exact difference between equality Operator ( ==) and Equals() Method in C#?
== is working based on reference of a variables
Equals() method is working based on content
114)What is declarative and imperative programming?
Declarative programming says write what you want , Lets framework achieve it
List<int> lstNumbers = new List<int>{10,11,12,13,14};
var oddNumbers = lstNumbers.where(x=>x%2 !=0)
Imperative programming says write step by step procedure to achieve something you want
List<int> lstNumbers = new List<int>{10,11,12,13,14};
List<int> oddNumbers = new List<int>();
foreach(var num in lstNumbers)
{
if(num%2 != 0)
oddNumbers.Add(num);
}
115)How many types of serialization available in .Net?
Persistent state of an object to transport is known as serialization. There are three serializing techniques available
1) Binary Serialization
2)Xml and SOAP serialization
3)JSON serialization.
Post a Comment