Translate to your language...

Showing posts with label interface. Show all posts
Showing posts with label interface. Show all posts

6 October 2015

Most Interesting Questions in C#, ASP.Net and SQL Server, WCF, ASP.Net MVC

Hello Guys,
In this article, we will see the most interesting questions in Microsoft Technologies like in C#, ASP.Net, Sql Server, WCF services, ASP.Net MVC.
Q 1. I have logged in to the ASP.Net website using my credential for the login page. Now I want that when my session is expired, it should logout the application and show the login page. With that It should also logged out from the Windows and I should use the Windows credentials again to reach to my application again. How do you think it can be done?
Ans. This question is related to the Sessions in ASP.Net where the session time out is configured for the current user. And when the session gets expired, we need to set so that it should automatically come back to login page and user need to enter the credentials again.

Session time can be set as:

Q 2. In .Net Framework, Garbage collector is used for the Automatic Memory Management(to free-up the memory which is no longer used by the resources). There are 2 types of memory- the memory which is created using value types(Stack memory) and the Memory created using Reference Types(Heap Memory). Which of these memory will be disposed by the Garbage Collector?
Ans. Garbage Collector is used to free-up the memory of unreferenced objects, the objects which are no longer used by the resources.
Garbage collector only disposes the memory created by using the Reference Types means the objects which are in Heap Memory. The objects which are created using the Value Types will get cleared when they are out of Stack. Stack is the type where the value types gets push when they are created and get popped when they are used. So when the variable is used by the program and gets out of the scope, it will get popped up from the stack.
The Garbage collector will no take care of the stack objects.

Q 3. Abstraction is used to display only essential and necessary features of the Object to the outside world and hiding the unnecessary details. Abstract Classes and Interfaces can be used to achieve abstraction. 
Can I create the Constructor for an Abstract Class? 
Can I write Static methods inside Abstract class?? 
Can I inherit an Abstract class to Another Abstract class? If so, then if we can have an abstract method in the abstract base class the how the child abstract class will behave for the overriding of this method? Can I override the base class abstract method to the abstract child class? If so then do you think, Abstract class support override methods as we are now trying to implement the overriding in the child class which is abstract.
Ans. Abstraction is the way to expose the objects to the outside world as per our requirements. For example, using the public and private members of the class. If we don't want to expose the variable or method which is inside the class,m make them private. Abstraction is the compile time where the compiler gets behaviour of the members during the compilation of the program.
Abstract classes and interfaces are also the ways to achieve abstraction as when we define the Abstract members inside the abstract class or in Interface, we are exposing these members to outside world to use them by their implementation.
Yes, Constructor can be created for the abstract classes. The abstract class works based on the child class memory so when the object of the child class get created, it will call first the abstract class constructor and then then it will call the child class constructor.
Yes,  We can write the Static members(Methods and Properties) inside the abstract class where
 the static members can be called based on the class name. ClassName.MethodName(), ClassName.PropertyName etc.
Yes, Inheritance between the abstract classes is possible where we have both base class and inherited class both are abstract classes. So if we have an abstract method in the abstract base class and then we are inheriting this base class to the abstract child class, the abstract method can be overridden in the child abstract class but it is not mandatory to implement it there. We can also implement the abstract base class method to the last child class. So we can write the virtual, abstract as well as override methods inside the abstract class.

Q.4 Can we write properties inside interfaces? If so then what is the syntax to write a property in the Interface if the name of the property is ID and return type is integer?
Ans. Yes, We can define the public properties inside the Interface but as in interface all the members are public so we don't need to define the property as public.
Syntax: int ID {get; set;}

Q. 5 By default all the members of the Interface are public abstract. Can we write explicitly the 'Public' for the method? Do you think now my program will compile or will give a warning message? If it will give the warning message, then what the text for the warning message. If it will give the compilation error then what error you will get?
Ans. No, We can't write the explicitely 'Public' access specifier inside the interface. If we write the 'public' access specifier inside the interface, it will throw error during the compilation of the program that 'no access specifier are allowed inside the interface'.

25 April 2014

2+ yrs .Net Interview Questions and Answers for Quick Reference

Hai Friends,

In the continuation of the series of the Interview Questions in the Microsoft Technologies, I am posting few more questions which will be helpful for the Quick reference in learning the concepts related to WCF, Assembly,  SQL Server,Interface & Abstract Class etc.


1. What is the difference between Web Services and WCF.

Ans. 

A. WCF Services = Web Services + Remoting + Security + Reliability + Messaging Queue

B. Not only that, hosting is also one of the powerful feature in WCF where the service can be hosted in Self Hosting, Hosting in Windows Services, Hosting on another Service etc.

C. Supports DataContractSerializer which is fast and can serialize fields and properties.

D. Integrated Logging mechanism.


2. What are the different ways of hosting a WCF service.
Ans.

A. Hosting on Console application

B. Hosting on Windows application

C. Hosting on Windows services

D. Hosting on IIS

E. Hosting on WAS(Windows Activation Service) 


3. Explain the different types of triggers in SQL Server.
Ans.

A. DDL Trigger- Trigger fires on DDL Commands

B. DML Trigger- Trigger fires on DML Commands

C. InsteadOf Trigger- Trigger fires on View updation


4. Difference between BasicHttpBinding and WSHttpBinding.
Ans.

A. BasicHttpBinding does not enable message level security while the WSHttpBinding enables Message level as well as Transport level security.

B. BasicHttpBinding has the default windows authentication while the WSHttpBinding support WS* authentication and security.

C. BasicHttpBinding only supports HTTP to access the service while WSHttpBinding suppors HTTP and HTTPS(secure)

D. Data which travels through the BasicHttpBinding is in XML format and no encryption allowed while the data travels through WSHttpBinding goes in Encrypted format.


5. What are the different types of assemblies available in .NET?
Ans.

A. Private Assembly

B. Shared Assembly

C. Satellite Assembly


6. What is a strong name and how do you give a strong name to an assembly?
Ans.

Unique identification of an assembly called as strong name. By strong name we can differentiate the assemblies which are having the same name.

We can generate the Strong name using the command:

SN -K dllName

7. What is InsteadOf trigger.
Ans.
 

To update the Views, InsteadOf trigger is used. This is the only use of InsteadOf trigger. There is no use except this.


8. Can we use Insert/Update statements in views in SQL SERVER?

Ans. 

No, In general, View made to works only on the Query/Select command. So we can't have DML commands inside the View. So no Insert/update operations are allowed.

But there is a provision where we can make the views as updatable by using the DML statements inside the views but it is more complicated when there are views which are created by using the joins in the queries.
So its not preferable to make the view as updatable.

9. What are abstract classes and Interface and what the difference between the two.
Ans.

When the class does not provides the full functionality, the class must be declared as the abstract class.

There are 2 types of abstract classes-

A. Partial Abstract Class (Abstract Class)- Class which can support the abstract members(methods, properties) as well as concrete members(methods, properties), the class can be declared as the abstract class.

Abstract class works based on the child class memory. We can't create the object of the abstract class, only the reference can be created for the abstract class.

All the abstract members must be overrides in the child class using the Override keywords.


B. Fully Abstract class (Interface) - The collection of abstract members (methods, properties) is called as interface.

Interface contains all the abstract members.


When you have limited scope for the functionality, Abstract class is better but when there is the requirement to implement the global functions, interface is best way to do.

e.g. When u have some feature which can be implemented in several classes and classes are interrelated, use Abstract class but when the feature can be application level and used by various independent classes, use Interface.


10. What are the advantages of Interface over abstract classes?
Ans.

When you have some feature which can be implemented in several classes and classes are interrelated, use Abstract class but when the feature can be application level and used by various independent classes, use Interface.

Interfaces are more rigid than Abstract classes.

e.g. let's say, i have a method called Show() to show the message in various classes, we can have interface for this. Now let's say, I have implemented this interface in 100 classes where I want to use the Show() method to show some message.

Tomorrow there is some requirement to have the new Print Functionality to let's say in 50 various classes. Now the one way is to add this Print() in the same interface so that we can just implement it to those classes. But what about the rest 50, where we don't want this print.. So this is drawback of Interface.

That is the reason the Single Responsibility principle and then the interface Segregation Principle came where a class/interface should have only one responsibility. Like in the above example, there are more responsibility and got the problem.

Abstract class is best when the scope is limited. Let's say I want to calculate the Area of few shapes. So we can say that this is limited as few classes can use this so we can create the abstract class with the abstract method called Area and then implement to those classes by inheriting the abstract class.

The scope of the Abstract class is till the immediate child class. So you can't use it in grand child or grandparent classes.


11. What is a sealed class?
Ans.
When the class provides full functionality, we can declare the class as Sealed Class. Full functionality means, the class doesn't need anything from outside like loading the external files etc. The sealed class can’t be inherited and can't be extended. We can create the object of sealed class so instantiation of the sealed class is possible.when the class is self-dependent, the class be made as sealed class. 
Overall when we want the class prevented to be inherited, we can declare the class as sealed class. Also If we want that our method should not be overridable, we can create the methods as sealed method.
E.g.If you see the definitions of the datatypes, they all are sealed by default becasue they cant be extended.
public sealed class String : IComparable, ICloneable, IConvertible, IComparable<string>, IEnumerable<char>, IEnumerable, IEquatable<string>
{
  // Summary:
  //     Represents the empty string. This field is read-only.
  public static readonly string Empty;

  // Summary:
  //     Initializes a new instance of the System.String class to the value indicated
  //     by a specified pointer to an array of Unicode characters.
  //
  // Parameters:
  //   value:
  //     A pointer to a null-terminated array of Unicode characters.
  //
  // Exceptions:
  //   System.ArgumentOutOfRangeException:
  //     The current process does not have read access to all the addressed characters.
  //
  //   System.ArgumentNullException:
  //     value is null.
  //
  //   System.ArgumentException:
  //     value specifies an array that contains an invalid Unicode character, or value
  //     specifies an address less than 64000.
  [CLSCompliant(false)]
  [SecurityCritical]
  public String(char* value);
.... 
Here we can see that String class is of Sealed type.We can create the object of the sealed class as:
String obj = new String();
bu we cant inherit these classes as they are not inheritable.


12. What kind of authentication mechanism you have used in your WCF project.
Ans.

There are various authentication modes which can be used for the WCF service like-

A. No Authentication- When the service doesn't requires any authentication and its public to access and use, this type of authentication mode is used.

B. Windows Authentication – This type of service depends on the windows credential so if the user is having the windows credentials, they can use and access the service.

C. Form Authentication – This type of authentication requires certain and valid user name and password to access the service.

D. Certificate Based – There are the secure cervices where each request needs certain authentication mechanism which can be the 3rd party like .X509 certificates to validate the access requests.

E. Custom Authentication- This can be mixed with two or more authentication mechanism.

F. Token Based Authentication – Depends on the token provided by the service provider. So based on the token, the client can access the service.


13. How do you configure a WCF Service to use Network Authentication?

Ans. 

We need to set the remote settings for this.

Below is the reference url:


14. What is Garbage Collection? How is Garbage Collector invoked in .NET?
Ans.

When the heap memory is filled with the objects and there is no memory left to accommodate the new object, Garbage collector gets called by the CLR. So it's an activity which is initiated by the run time (CLR). When the garbage collector gets called, it tries to find out each referenced objects which are in use by the applications. The objects which are not reachable, it marks them to delete. Based on the reachable objects, the garbage collector prepares the object graph, which has the reference of each reachable object. Now based on the object graph which contain the object generations too, the Garbage collector checks for the generation 0 objects and marks them for deletion and move rest of the objects to generation 1. The heap memory gets compacted for the new object and new object gets placed in the heap memory.

This is the uncertain and un-deterministic process as the Garbage collector doesn't know when it will be called. Its all based on the capacity memory of the heap. When the heap memory get filled and a new object is initiated, the runtime (CLR) calls the Garbage collector.


15. Can you force Garbage Collector to be invoked from c# code?
Ans.

Yes, When we have something called the unmanaged objects(C++, VC++,VB 6.0), then we need to explicitly release the memory. To do this, we can use the Dispose() method to free-up these object.

Sometimes, we need to force the garbage collector to be called to free-up the memory, so we can use the GC.Collect() method to do so.


16. How is var keyword used in .NET.Difference between var and dynamic keywords.
Ans. 

var keyword is newly introduces with the .net 3.5 and it is used to make the assignment for any type of data. It can store any type of data irrespective of its datatype.

So when we don't know that what type of data, the certain process will return, we can use the var keyword.

e.g.

var connection = new SqlConnection(connectionString);

Here the connection variable can store the SQLConnection type.
Dynamic keyword is newly introduces with the .net 4.0 version and is used to keep not only the any type of data but also the reference will be constant.

For the var, the type is made at the compile time while in case of Dynamic; the type is inferred in runtime.

e.g.

var customer = new Customer();

customer.GetCustomers();

Here the compiler will check for the GetCustomers()method. If the GetCustomers() method doesn't exists, it will show error during the compilation.
But for the dynamic,

dynamic  customer = new Customer();

customer.GetCustomers();

Here the compiler will not check for the GetCustomers() method during the compilation. But at the run-time, if the method is not available, it will throw error.
So the main use of dynamic is when we don't want the compiler should check for certain errors during compilation. It will skip the error if they are of dynamic type. 


17. Difference between Stored Procedures and Functions.
Ans.

Stored Procedure and Functions are the database objects which are the pre-compiled names SQL statements.

A. Stored procedure can take in as well as Out parameters while function can take only in parameters'.

B. Stored Procedure may or may not return the value while the Function must return a value. Returning a value in stored procedure depends on the input parameter. If the input parameter is of type out, then it will return the value.

C. We can use the function inside the stored procedure but stored procedure can't be used inside a function.


18. What are the different types of contracts available in WCF
Ans.

A. Service Contract- In Interface

B. Operation Contract - For Operations

C. Data Contract -Based on custom type

D. Message Contract - For Custom message

E. Fault Contract - For Error or Exception Handling


19. What is the static variable and what are the static variables in dot net.
Ans.

When the variable is shared by multiple class members (method, properties etc), we can make that variable as Static (in VB.Net Shared). To access these variables, we don't need to create the object of the class. We can directly call the static class members using the Class name. These variables get loaded when the class gets loaded in to the memory.

20. If I have 2 interfaces with same method same parameter and same return type. Both interface implemented from one Class. Then how can i call to these methods.
Ans.

Two interfaces with the same methods and same parameters can be implemented using the external interface functionality, rather than internal interface.
By default, when we implement the interface, we need to implement all the method to the implemented class.
But if the methods are same, then we can take the help of explicit interface as:
interface inf1
{
 void Print();
 void Show();
}
interface inf2
{
 void Print();
 void Show();
}
Now we will implement these interfaces in a single class:
class MyChildClass: inf1, inf2

{
 public void inf1.Print() // explicit interface call
 {
  //implement Print() method details for the first interface
 }
 public void inf1.Show()
 {
  //implement Show() method details for the first interface
 }
 public void inf2.Print()
 {
  //implement Print() method details for the second interface
 }
 public void inf2.Show()
 {
  //implement Show() method details for the second interface
 }
}

21. Can method overloading and overriding possible in simultaneously in program.
Ans.

Yes, both overloading and overriding concept can be implemented simultaneously between the parent can child classes as:
Class MyBaseClass

{
 public void Print()
 {
   // Print method implementation
 }
 public void Print(string fileName) // overloaded method
 {
   // Print method implementation with the file name
 }
 public virtual void Print()
 {
   // Print method implementation
 }
}
Class ChildClass:MyBaseClass
{
 public override void Print() // overriding implementation
 {
   // Print method implementation
 }
}


22. What is the use of "using" keyword?
Ans.

"using" keyword is used in 2 scenarios:
1. To write the namespace as:
using System.Data.SqlClient;

2. To clean up the memory automatically after the use of object as:
using(var f = new SqlCommand())
{
 // some other iomplementation
}

The above code will delete the memory used by the 'f' object after the completion of the transaction.

23. What is dispose and finalize method and how it works
Ans.

Finalize and Dispose methods comes under the System.GC namespace and used to free up the memory occupied the objects. Garbage collector uses these objects to free up the memory.
When there is no enough memory to allocate the new object in the Heap, Garbage collector gets called automatically to free up the memory. It mainly creates the object graph and finds the objects which are not used since longer time. Based on the generations, it removes the objects.
Dispose method is called manually by programmer to remove the objects.
It uses IDisposable interface which has the Dispose method and this can be implemented in the code.

15 September 2013

OOPs Concepts: When to use

Hai Friends,

This post is regarding the main OOPs concepts and what is there use and when it’s good to use them.

Below are the concepts which are included in this post:

·                     Encapsulation
·                     Abstraction
·                     Overloading
·                     Method Overriding
·                     Constructor
·                     Interface
·                     Inheritance
·                     Abstract class
 
These OOPs concepts you can use while implementing the functionality of your projects in accordance with the requirements and situations.
Hope it will be useful to learn and in the implementation of the projects.
1) In which situation encapsulation is used in your project?
Encapsulation is hiding and binding of the data.
Accessing the public properties by using private variables is one of the good examples of Encapsulation in all the projects.
e.g.
private string _name;
public String Name
{
get{return _name}
set{_name= value;}
}

Here you are not able to access the private variable _name but its value can be accessible by the public property Name. So a kind of Encapsulation here. Namespace is also an example of Encapsulation which is encapsulating the classes, methods, properties etc.
This is the runtime behaviour of an entity.

2) In which situation abstraction is used in your project?
Hiding the behaviour and showing the necessary things regards an entity can be called as Abstraction. Like for a Car, showing the method like Clutch, Gear etc but hiding its internal functions can be an example of Abstraction. This is the compile time behaviour of the entities.
When there are the specific requirements and that can be flown to their child, then we can use abstraction. it means whatever is essential, we implement to its child classes, else will be inherited automatically.
e.g.
class A
{
public abstract void Add(int a, int b);
protected int x;
public string Name{get; set;};
}
Class B: A
{
 public override void Add(int a, int b)
 {
  Console.WriteLine("Sum of two numbers is:" + int. Parse(a + b).toString());
 }
}
Here the essential method Add is implemented to the child class B. rest all the members will be inherited accordance to their protection levels.

3) In which situation method overloading is used in your project?
The concept where we want the similar functionality but different behaviour, we use overloading.
When we have same functionality to implement with different behaviour, we go with the overloading of method.
e.g. If we have to get the sum of 2 numbers, 3 number and 4 numbers. So in that case we can take only one method name Add with its varied parameters like:

public void Add(int a, int b)
{
 Console.WriteLine("Sum of 2 numbers" + int. Parse(a + b).toString());
}

public void Add(int a, int b, int c)
{
 Console.WriteLine("Sum of 3 numbers" + int. Parse(a + b +c).toString());
}

public void Add(int a, int b, int c, int d)
{
 Console.WriteLine("Sum of 4 numbers" + int. Parse(a + b + c + d).toString());
}

Now according to our requirement, we can call the respective method with its arguments.
Here all the methods are doing sum of the numbers, but their behaviour are different- the first method will give the sum of 2 number while the next one will give the sum of 3 number and the final method will give the sum of 4 numbers.

4) In which situation method overriding is used in your project?
When we may(abstract) or may not(virtual) want to implement the particular method at runtime to its child class according to the requirement, we go with overriding concept.
e.g.

Class X
{
 public virtual void Print()
 {
  Console.WriteLine ("This is the print method. You can override according to your definitions and implementation");
 }
}
Class Y:X
{
 public Override void Print()
 {
  Console.WriteLine("Implementation for Excel print methods");
 }
}

5) In which situation constructor is used in your project?
Constructor is the way to initialize the class with the default values of the members. Constructor is by default initialize with the class.
But if you want to initialize some members when the class in getting instantiate, you can write the constructor and code for initialization inside that constructor.
e.g.

Class MyClass
{int a;
 public MyClass()
 {
   a=10; //initialized with the class instantiation
 }
}

Here the constructor code will get initialized with the class instantiation.

6) In which situation interface is used in your project?
Interface is the collection of abstract members.
So whenever we have the requirements such as it requires through the application at many places, then we create an interface with that method so that we can implement it at our own way according to the requirements.
e.g.
Interface Inf
{
 void Print();
}

Class ExcelClass:Inf
{
 public override void Print()
 {
  //Write code to print in excel
 }
}

Class WordPrintClass: Inf
{
 public override void Print()
 {
  //Write code to print in Word
 }
}
Class PDFPrintClass: Inf
{
 public override void Print()
 {
  //Write code to print in PDF
 }
}

Also to implement the multiple inheritances, we use inheritance.

7) In which situation inheritance is used in your project?
When we want to have some common features for multiple classes then we go for inheritance.
To inherit the properties of base class to their derived class.
8) In which situation abstract class is used in your project?
Abstract class is same as abstract. So go to the Q 2 for the details.


So you can go through the link to get the fair idea about its implementation: 
http://www.dotnetspider.com/forum/ViewForum.aspx?ForumId=295763&

Sr .Net Consultant | MCP Certified | Microsoft Technologies(.Net with Sql Server, MVC, AngularJS

My photo
Kuala Lumpur, Kuala Lumpur, Malaysia
Overall 17.0+ years of experience in software industry as a Programmer/Developer/Architect with Web & Windows application development and Maintenance, including analysis, design, coding, testing and documentation. • Proficient in Microsoft Technologies i.e. Visual Studio with .Net Framework. • Excellent analytical, problem solving, communication, interpersonal skills with the ability to handle multiple tasks or projects working independently or in a team. • Consistent attention to detail with an ability to analyze and interpret the implication of decisions to minimize bugs and create user-friendly applications. • Experience of Collaborating with Clients and Team Members. Ability to convey Technical Information at all levels. • Ability to work with team/management to ensure successful execution of project management tasks including scheduling, scope management, outside dependency coordination and priorities resolution. • Experience in working with global clients like UK, USA, Europe, Australia etc. and teams located remotely (e.g. Sydney, Australia, USA, and Sweden). • Worked with the Swedish client ManpowerGroup at onsite client location Stockholm, Sweden.