Books of .Net/Interview Questions Interview Questions and Answers Programs C# Concept OOPS Concept JOBS

Saturday, October 8, 2016

C# Interview Questions and Answers Part 2


Que 1)Can “this” be used within a static method?
Ans -  We can't use this in static method because keyword 'this' returns a reference to the current instance of the class containing it. Static methods (or any static member) do not belong to a particular instance. They exist without creating an instance of the class.
An instance of a class contains a separate copy of all instance fields of the class but there is only one copy of each static field. It is not possible to use this to reference static methods or property accessors.


Que 2)What is the difference between string.Empty and Which one is best to use?
Ans - String.Empty because it is a static variable, rather than "" which has to create a new string.
String.empty will not create any object while "" will create a new object in the memory . Hence string.empty is better in memory management.


Que 3)How can you read an integer value from the keyword when the application is runtime?
Ans -  You can use the console.ReadLine()method to read a value an integer from the keyboard, but console.ReadLine() method returns String value.Therefore, if you want to read an integer value , you need to convert the value returned by console.ReadLine() into an integer value. 
Syntax: int value=convert.ToInt32(console.ReadLine()); 

Que 4) Difference between Abstract and Virtual method
Ans - Virtual methods have an implementation and provide the derived classes with the option of overriding it. 
Abstract methods do not provide an implementation and forces the derived classes to override the method.
So, abstract methods have no actual code in them, and subclasses HAVE TO override the method. 
Virtual methods can have code, which is usually a default implementation of something, and any subclasses CAN override the method using the override modifier and provide a custom implementation.

Example
public abstract class E
{
    public abstract void AbstractMethod(int i);

    public virtual void VirtualMethod(int i)
    {
        // Default implementation which can be overridden by subclasses.
    }
}

public class D : E
{
    public override void AbstractMethod(int i)
    {
        // You HAVE to override this method
    }
    public override void VirtualMethod(int i)
    {
        // You are allowed to override this method.
    }
}

Que 5)Difference b/w Abstract class and an Interface.
Ans-
Probably "Difference Between abstract Class and Interface" is the most frequent question being asked in .Net world .  In this tutorial, I will explain the difference theoretically followed by code snippet. 
Theoretically there are basically 5 differences between Abstract Class and Interface which are listed as below :-

A class can implement any number of interfaces but a subclass can at most use only one abstract class.
An abstract class can have non-abstract Methods(concrete methods) while in case of Interface all the methods has to be abstract.
An abstract class can declare or use any variables while an interface is not allowed to do so.

So following Code will not compile :-


interface TestInterface
{
    int x = 4;  // Filed Declaration in Interface
    void getMethod();
 
    string getName();
}
 
abstract class TestAbstractClass
{
    int i = 4;
    int k = 3;
    public abstract void getClassName();
}

It will generate a compile time error as :-

Error    1    Interfaces cannot contain fields .

So we need to omit Field Declaration in order to compile the code properly.

interface TestInterface
{
    void getMethod();
    string getName();
} 

abstract class TestAbstractClass
{
    int i = 4;
    int k = 3;
    public abstract void getClassName();
}

Above code compiles properly as no field declaration is there in Interface.

An abstract class can have constructor declaration while an interface can not do so.

So following code will not compile :-

interface TestInterface
{
    // Constructor Declaration
    public TestInterface()
    {
    }
    void getMethod();
    string getName();
}
 
abstract class TestAbstractClass
{
    public TestAbstractClass()
    {
    }
    int i = 4;
    int k = 3;
    public abstract void getClassName();
}

Above code will generate a compile time error as :-

Error    1    Interfaces cannot contain constructors  

So we need to omit constructor declaration from interface in order to compile  our code .

Following code compile s perfectly :-

interface TestInterface
{
    void getMethod();
    string getName();
}
 
abstract class TestAbstractClass
{
    public TestAbstractClass()
    {
    }
    int i = 4;
    int k = 3;
    public abstract void getClassName();
}

An abstract Class is allowed to have all access modifiers for all of its member declaration while in interface  we can not declare any access modifier(including public) as all the members of interface are implicitly public.  

Note here I am talking about the access specifiers of the member of interface and not about the interface.

Following code will explain it better :-

It is perfectly legal to give provide access specifier as  Public (Remember only public is allowed)

public interface TestInterface
{
    void getMethod();
    string getName();
}

Above code compiles perfectly.

It is not allowed to give any access specifier to the members of the Interface.

interface TestInterface
{
    public void getMethod();
    public string getName();
}

Above code will generate a compile time error as :-

Error    1    The modifier 'public' is not valid for this item.

But the best way of declaring Interface will be to avoid access specifier on interface as well as members of interface.

interface Test
{
    void getMethod();
    string getName();
}

Que 6)What deffrent betwen method overriding and 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. 
Method Overloading happens at compile time (Early Binding) while Overriding happens at runtime (Late Binding). 
In method overloading, method call to its definition has happens at compile time (Static Binding) while in method overriding, method call to its definition happens at runtime (Dynamic Binding).

Que 7)what is a primitive type in C#
Ans -  primitive type is a data type in c# language which is understandable by the C# compiler . in C# int , string , float etc... data types are considered as primitive types . during compilation usually primitive types are converted into MSIL types.
Ex: if you write int x=20 ; in C# language during compilation the code is converted as shown below .
Int32 x=20;
Note: Int32 is MSIL equivalent for int primitive type .

Que 8)What do you mean by Mutable in C#?
Ans - Mutable means "can change".Mutable type, in C#, is a type of object whose data members, such as properties, data and fields, can be modified after its creation.Stringbuilder is example of this.


Que9)What is the Difference between Early binding and Late binding ?
Ans
1)Early binding also called Compile polymorphism(Static polymorphism) and Late binding also called Runtime polymorphism(dynamic polymorphism). 
2)Compile polymorphism is achieved by using Operator Overloading and Function Overloading while Runtime polymorphism is achieved using Virtual function. 
3) In Compile polymorphism we can get the object type at compile time so we also called it as Early binding, and in Runtime polymorphism we can get the object type at Run time so we also called it as Late binding.


Que 10)What is the difference between Finalize() and Dispose() methods?
Ans -
Finalize is used internally by garbage collector Used to free unmanaged resources like files, database connections, COM etc. held by an object before that object is destroyed. but Dispose() is used by used externally




  Finalize: Used to free unmanaged resources like files, database connections, COM etc. held by an object before that object is destroyed. Internally, it is called by Garbage Collector and cannot be called by user code. It belongs to Object class. Implement it when you have unmanaged resources in your code, and want to make sure that these resources are freed when the Garbage collection happens. There is performance costs associated with Finalize method Dispose: It is used to free unmanaged resources like files, database connections, COM etc. at any time. Explicitly, it is called by user code and the class implementing dispose method must implement IDisposable interface. It belongs to IDisposable interface. Implement this when you are writing a custom class that will be used by other users. There is no performance costs associated with Dispose method.

Yoy may also like,

1)Abstraction and Encapsulation in OOPS

2)Inheritance in OOPS

3)Polymorphism in OOPS

4)Interface in OOPS

5)What is Virtual Function

6)What is Abstract class and Abstract function

7)What is Static Class and Static Members



1) C# Interview Questions and Answers Part3

1 comment :