Tags: .net and c# interview questions

SQL Server interview questions, c# interview questions, .NET interview questions, ASP.NET interview questions

C#.NET interview questions: - What is the use of “Yield” keyword? Mention atleast one practical scen

C#.NET interview questions: - What is the use of “Yield” keyword? Mention atleast one practical scenario where it can be used?

“Yield helps you to provide custom stateful iteration over .NET collections.”

There are two scenarios where “yield” keyword is useful: -

  • Customized iteration through a collection without creating a temporary collection.

  • Stateful iteration.

Practical Scenario: - Customized iteration through a collection

Let’s try to understand what customized iteration means with an example. Consider the below code.
Let say we have a simple list called as “MyList” which has collection of 5 continuous numeric values 1,2,3,4 and 5. This list is browsed/iterated from  console application from within static void main method.
For now let’s visualize the “main()” method as a caller. So the caller i.e.  “main()” method calls the list and displays the items inside it.  Simple…till now ;-).

staticList<int> MyList = newList<int>();

staticvoid FillValues()
{
          MyList.Add(1);
          MyList.Add(2);
          MyList.Add(3);
          MyList.Add(4);
          MyList.Add(5);
}

staticvoid Main(string[] args) // Caller
{
          FillValues(); // Fills the list with 5 values
foreach (int i in MyList) // Browses through the list
          {
Console.WriteLine(i);
          }
Console.ReadLine();
}
                                              image 1

Now let me complicate this situation let’s say the caller only wants values greater than “3” from the collection.  So the obvious thing as a c# developer we will do is create a function as shown below. This function will have temporary collection. In this temporary collection we will first add values which are greater than “3” and return the same to the caller. The caller can then iterate through this collection.

staticIEnumerable<int> FilterWithoutYield()
{
List<int> temp = newList<int>();
foreach (int i in MyList)
          {
if (i > 3)
              {
                  temp.Add(i);
              }
          }
return temp;
}

                                  image 2

Now the above approach is fine but it would be great if we would get rid of the collection, so that our code becomes simple. This where “yield” keyword comes to help. Below is a simple code how we have used yield.

“Yield” keyword will return back the control to the caller, the caller will do his work and re-enter the function from where he had left and continue iteration from that point onwards. In other words “yield” keyword moves control of the program to and fro between caller and the collection.

staticIEnumerable<int> FilterWithYield()
{
foreach (int i in MyList)
          {
if (i > 3) yieldreturn i;
          }
}

So for the above code following are details steps how the control will flow between caller and collection. You can also see the pictorial representation in the next diagram shown below.

  • Step 1:- Caller calls the function to iterate for number’s greater than 3.

  • Step 2:- Inside the function the for loop runs from 1 to 2 , from 2 to 3 until it encounters value greater than “3” i.e. “4”. As soon as the condition of value greater than 3 is met the “yield” keyword sends this data back to the caller.

  • Step 3:- Caller displays the value on the console and re-enters the function for more data. This time when it reenters, it does not start from first. It remembers the state and starts from “5”. The iteration continues further as usual.

                        image 3

Also see following C#.NET interview questions video on use of Yield keyword: -

SQL Server interview questions, c# interview questions, .NET interview questions, ASP.NET interview questions

Six important WCF interview questions with answers

The below questions are taken from .NET interview question book published by Bpb publication , read more about the book from here .NET interview question book

What is WCF?

Its Microsoft’s programming model for building SOA based applications. It’s a unification of various .net technologies like Com+, Remoting, Web Services and MSMQ. WCF is also based on SOAP (Except WCF REST) like web services and supports wide range
of ws-* specifications which include ws-address, ws-security and many.

What is mean by ABC of WCF?

It’s an abbreviation for Address, Binding and Contract. Together defines the endpoint of a service. It typically answers three questions where, How and What.

Where – Address – Where we get the service?
How – Binding – How we can access the service? (What protocol we should use?).
What – Contract –What we will get from Service?

What is difference between WCF and Web Services?

WCF is evolved from Web Service. Web service can be hosted only via HTTP but WCF supports multiple protocols like http and tcp. WCF is fully compatible SOA based applications supporting all specifications provided in ws-* specifications.

What is mean by Service, Message and client?

Service – Self-contained business functionality which are independent of other services.
Message – Services communicate with each other with the help of messages.
Client – party which consume the service.

What are different things we need to do in order to create a WCF service?

  • Create a service contract (an interface attached with ServiceContract attribute and methods with OperationContract attribute)

  • Create data contract if required any (custom classes with DataContract Attribute and properties with DataMember attribute)

  • Implement the service and define operation contracts.

  • Define end points

  • Host the service.

Explain WCF REST ?

Do watch the below WCF interview questions video created by www.questpond.com

SQL Server interview questions, c# interview questions, .NET interview questions, ASP.NET interview questions

.NET interview questions and answers: - Will the below codes create new instances?

string str = ".NET interview questions"; // Instance 1
string str1 = ".NET  interview questions"; // points to same Instance 1   


No, the above code will not create multiple instances but rather they will point to the same memory instance. If the compiler finds the same value in memory he points the variable to the same instance rather than creating a new one.


The below two code snippets will create multiple memory instances of the variable.

string str = ".NET interview questions"; // Instance 1
string str1 = ".NET  interview questions"; // points to different instances.

string str = ".NET interview questions"; // Instance 1
string str1 = "c# interview questions"; // points to different instances


You can check if they point to the same memory instance or multiple instances by using “ReferenceEqual” which belongs to “object” type. It returns true if the objects point to the same instance.

object.ReferenceEquals(str, str1);


You can also visit our site for more .NET interview question with answers at www.questpond.com

 You can also see this link c# interview question video: - What kind of questions are asked in .NET interviews?


SQL Server interview questions, c# interview questions, .NET interview questions, ASP.NET interview questions

C# interview questions and answers: - How to reverse a string without using reverse function?

In one of the .NET interview question blog’s I had read this c# interview question :- How to reverse a string ?. In the mentioned blog the author has spoken of how to reverse the string using the “reverse” function. But recently in one of the interviews I was asked to reverse the string without using in-built .NET function i.e. without using “reverse” function

Below is a simple code which demonstrates how to reverse the string with a simple for each loop.   So we use the “char” and loop through the string text. As we loop we concatenate in a reverse mode.  See how the “c” is concatenated first.

foreach (char c in text)
{

          reverse = c + reverse; // concantenate the characters in a reverse mode

}

Below is the full c# code of the same.

static void Main(string[] args)
{
      string text = " c# interview questions and answers ";

      string reverse = string.Empty;

      foreach (char c in text)
      {

          reverse = c + reverse; // concantenate the characters in a reverse mode

      }
      Console.WriteLine(reverse);
}



You can also watch the below c# ADO.NET interview question: - Difference between dataset and Datareader ? :-



This is a nice blog which has a nice collection of real time c# interview questions and answers.
SQL Server interview questions, c# interview questions, .NET interview questions, ASP.NET interview questions

.NET interview questions and answers: - What is the use of Click Once?

Consider a situation where you have a windows application and it’s deployed worldwide in lot of client’s machine. Now after sometime you come out with a new version and you would like to update the client application with this new version. For now assume that your end users are not technical, so providing them setup is out of question. That’s where “ClickOnce” technology is useful. It is a technology for deploying windows application. When any new version comes in , you can upload this new version on a  central server  and “ClickOnce” will pull the portion that have changed and update the application accordingly.



set up 2



You can also see the c# and .NET interview question and answers blog.

For more .NET interview questions and answers visit us at www.questpond.com
SQL Server interview questions, c# interview questions, .NET interview questions, ASP.NET interview questions

Can we have different access modifiers on get/set methods of a property?

Yes, we can have different access modifiers. The below code will compile perfectly well.


String


You can also visit for more c# interview questions and answers at http://dotnetinterviewquestion.wordpress.com/category/c-and-net-interview-questions-and-answers/
SQL Server interview questions, c# interview questions, .NET interview questions, ASP.NET interview questions

ASP.NET interview questions: -Can you explain membership and role providers in ASP.Net 2.0?

A membership and role provider helps to automate your authentication and authorization code. When we want to implement user authentication and authorization in any project following are the routine task:-

  • Creation of user and roles tables.

  • Code level implementation for maintaining those tables.

  • User interface for ‘userid’ and ‘password’.

All the above tasks are automated using membership and roles.
Inorder to implement membership and roles run aspnet_regsql.exe from ‘C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727’ folder.
This will create necessary readymade tables like users, roles etc.  You can then use the readymade API of membership and roles to create user and roles as shown in the code snippet.

membership
                                        Figure :- Membership API


membership 2
                                       Figure : - Roles API


Below is an important ASP.NET interview question and answers video:-




 See more stuffs on ASP.NET interview questions

Click here to see more C# and ASP.NET interview questions and answers

SQL Server interview questions, c# interview questions, .NET interview questions, ASP.NET interview questions

C# interview questions: - How can we check which rows have changed since dataset was loaded?

For tracking down changes, Dataset has two methods, which comes to rescue “GetChanges “and “Has Changes”.

Get Changes

Returns dataset, which are changed since it, was loaded, or since Accept changes was executed.

Has Changes

Or abandon all changes since the dataset was loaded use “Reject Changes This property indicates that has any changes been made since the dataset was loaded or accept changes method was executed.

note 2



References
SQL Server interview questions, c# interview questions, .NET interview questions, ASP.NET interview questions

C# interview questions and answers: - Can you write a simple c# code to display Fibonacci series?

These are some strangec# interview questions which are asked in big companies to test your logical thinking ability. Before we move ahead lets first to understand what exactly is Fibonacci series. Fibonacci series are nothing but series of numbers 0,1,1,2,3,5 and so on.

The specialty of this series is that next number is addition of previous two numbers. Below figure explains how Fibonacci series works. We first start with 0 and 1. The next number is addition of “0 + 1” which will give us “1”. The next number will be again addition of previous value which is “1” and the current value which is “1” which will give us “2” and so on.

                                  123 im

Implementing Fibonacci series in c# is a 4 step process. Below image shows the same in a pictorial format.

                                          123 im 2

Step 1:- Define 3 variables “Prev”,”Next” and “Sum”. Initialize these variable with “Prev=1” , “Next = -1” and “Sum=0”.


Step 2:- Add “Prev” and “Next” variable to get the Sum. Display the “Sum” variable.


Step 3:- Swap “Prev” value to “Next” and “Sum” to “Previous”.


Step 4:- Execute “Step 2” and “Step 3” again for the next Fibonacci value.


Below is how the code will look like. Do follow the steps in comment.

123 im 3

The above question is taken from the bestselling c# and .NET interview question book written by Shivprasadkoirala and published by BPB publication.

References