ASP.NET Interview Questions [Resolved]

Posted by Anu_Dgr8 under ASP.NET on 3/26/2012 | Points: 10 | Views : 2135 | Status : [Member] | Replies : 6
1> How do you make a Telephone Directory using a Data Structure?

2>You have a file.You need to get the count of each word in that file.How do you do it?

3>You have a file with the word 'TEST' repeated several times.How do you replace all the occurences of 'TEST' and return the output.

Thanks and Regards,
Hoosha Boosha



Responses

Posted by: Anu_Dgr8 on: 3/27/2012 [Member] Starter | Points: 25

Up
0
Down

Resolved
I figured out the logic for the 2nd question using StreamReader and Hashtable.The code is as follows:

Hashtable openWith = new Hashtable();

if (FileUpload1.FileName.Length != 0)
{
string filepath = Server.MapPath("~") + "\\upload\\" + FileUpload1.FileName;
FileUpload1.SaveAs(filepath);
using (StreamReader sr = new StreamReader(filepath))
{
string line;
while ((line = sr.ReadLine()) != null)
{
string[] WORDS = line.Split(' ');
foreach (string WORD in WORDS)
{
if (!openWith.ContainsKey(WORD))
{

openWith.Add(WORD, 1);//set count to 1 for new words
}
else
{
openWith[WORD] = Convert.ToInt32(openWith[WORD].ToString()) + 1; //increment count by 1 for existing words
}
}


}

}



foreach (DictionaryEntry de in openWith)
{
Response.Write(de.Key);
Response.Write(de.Value);
}

//to get count of each distict word in the file
}


Thanks and Regards,
Hoosha Boosha

Anu_Dgr8, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Anu_Dgr8 on: 3/27/2012 [Member] Starter | Points: 25

Up
0
Down

Resolved
Jmckamal ,

Thanks for the 3rd answer.I tried it.Its working.
Will check out and code for your Answer for 1st Question.



Thanks and Regards,
Hoosha Boosha

Anu_Dgr8, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Anu_Dgr8 on: 3/27/2012 [Member] Starter | Points: 25

Up
0
Down

Resolved
I have coded for the 1st Answer also:The code for the same is as follows:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="TEST4.aspx.cs" Inherits="TEST4" %>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table width="100%" border="0" cellpadding="1" cellspacing="1" runat="server" id="tblSearchPhNo">
<tr>
<td colspan="2">
<asp:Label ID="lblMsg" runat="server" Font-Bold="true" ForeColor="Red"></asp:Label>
</td>
</tr>
<tr>
<td align="right">
<b>Name: </b>
</td>
<td>
<asp:TextBox ID="txtSearchName" runat="server"></asp:TextBox>
<asp:Button ID="btnSearch" runat="server" Text="Search" OnClick="btnSearch_Click" /> &nbsp;&nbsp;
<asp:Label ID="lblPhNo" runat="server"></asp:Label>
</td>
</tr>
</table>
<table width="100%" border="0" cellpadding="1" cellspacing="1" runat="server" id="tblAddNewPhoneNo">
<tr>
<td align="right">
<b>Add Name: </b>
</td>
<td>
<asp:TextBox ID="txtNewName" runat="server"></asp:TextBox>&nbsp;&nbsp;
</td>
</tr>
<tr>
<td align="right">
<b> Add Ph No: </b>
</td>
<td>
<asp:TextBox ID="txtNewPhNo" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td colspan="2" align="center">
<asp:Button ID="btnADDNew" runat="server" Text="Add New" OnClick="btnADDNew_Click" />
</td>
</tr>
<tr>
<td colspan="2" align="center">
<asp:GridView runat="server" ID="GRIDVIEW1" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="key" HeaderText="Name" />
<asp:BoundField DataField="value" HeaderText="Phone No." />
</Columns>
</asp:GridView>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class TEST4 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Dictionary<string, string> phonebook = new Dictionary<string, string>();
Session["Dictionary"] = phonebook;
}
}


protected void btnADDNew_Click(object sender, EventArgs e)
{
if (txtNewName.Text != string.Empty && txtNewPhNo.Text != string.Empty)
{
string NewName = txtNewName.Text;
string NewPhNo = txtNewPhNo.Text;
/*IDictionary is an interface for mapping one type of object or value to another.
* It works like a real dictionary, or a phone book: you have a "key" in mind like a person's name, and when you look it up,
* you get some information that's identified by that key, like an the meaning of the word or phone number.
* Each key can only be listed once, although two different keys are still allowed to have the same value.
* This is also generic in .NET 2.0, so a dictionary whose keys are strings and whose values are integers would be IDictionary<string,int>.*/

Dictionary<string, string> phonebook = (Dictionary<string, string>)Session["Dictionary"];
if (!phonebook.ContainsKey(NewName))
{
phonebook.Add(NewName, NewPhNo);
lblMsg.Text = "Successfully added person in the Phone Book.";
lblMsg.ForeColor = System.Drawing.Color.Blue;
GRIDVIEW1.DataSource = phonebook;
GRIDVIEW1.DataBind();
txtNewName.Text = string.Empty;
txtNewPhNo.Text = string.Empty;

}
else
{
lblMsg.Text = "This person is already in the Phone Book.";
lblMsg.ForeColor = System.Drawing.Color.Red;
}
}
else
{
lblMsg.Text = "Name / Ph No cannot be Empty.";
lblMsg.ForeColor = System.Drawing.Color.Red;

}

}
protected void btnSearch_Click(object sender, EventArgs e)
{
Dictionary<string, string> phonebook = (Dictionary<string, string>)Session["Dictionary"];
if (phonebook.ContainsKey(txtSearchName.Text)) // True
{
string number = phonebook[txtSearchName.Text];
lblPhNo.Text ="Phone No is: "+ number;

lblPhNo.ForeColor = System.Drawing.Color.Blue;

}
else
{
lblMsg.Text = "Not in PhoneBook.";
lblMsg.ForeColor = System.Drawing.Color.Red;
}
}
}


Thanks and Regards,
Hoosha Boosha

Anu_Dgr8, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Anu_Dgr8 on: 3/26/2012 [Member] Starter | Points: 25

Up
0
Down
There's a correction in the 3rd question:

3>You have a file with the word 'TEST' repeated several times.How do you REMOVE all the occurences of 'TEST' and return the output.

Thanks and Regards,
Hoosha Boosha

Anu_Dgr8, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Jmckamal on: 3/26/2012 [Member] Starter | Points: 25

Up
0
Down

1) Answer:

Refer this url : http://www.codeproject.com/Articles/18033/Phone-Directory-Implementation-Using-TRIE

2) Answer:

int count = 0;
string[] Text = File.ReadAllLines("C:/Test.txt");
for (int i = 0; i < Text.Count(); i++)
{
count += Text[i].Length;
}




3) Answer:



public static bool Replace(ref string file, ref string searchFor, ref string replaceWith)
{
try
{
//get a StreamReader for reading the file
StreamReader reader = new StreamReader(file);

//read the entire file at once
string contents = reader.ReadToEnd();

//close up and dispose
reader.Close();
reader.Dispose();

//use regular expressions to search and replace our text
contents = Regex.Replace(contents,searchFor,replaceWith);

//get a StreamWriter for writing the new text to the file
StreamWriter writer = new StreamWriter(file);

//write the contents
writer.Write(contents);

//close up and dispose
writer.Close();
writer.Dispose();

//return successful
return true;
}
catch (Exception)
{
return false;
}
}


Anu_Dgr8, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Anu_Dgr8 on: 3/27/2012 [Member] Starter | Points: 25

Up
0
Down
Jmckamal ,

You did not get my 2nd question:
If i have a file whose content is:
ANU gr8 ANU ANU gr8 gr8
ANU gr8 ANU gr8 gr8 gr8 test
Then output should be as follows: ANU : 5
gr8 : 7
test : 1
(i.e count of each distinct word in the file)

Thanks and Regards,
Hoosha Boosha

Anu_Dgr8, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response