Sample Problems for Practice

Transcription

Sample Problems for Practice
Sample Problems for Practice
Note: The practice problems are intended to give you basic idea about most of the topics and types of questions. It does not cover
all topics. Note also that the practice problems included for ASP.NET may also appear in ASP and vice versa. You should read
all topics discussed and problems solved during the lecture.
Please read the file upload and file systems topics posted on the subject’s web page, before you solve the practice problems.
1. Following is the HTML code of a page containing individual check boxes. Write VB.NET code that will display the
selected check boxes on separate lines in a label when the ‘Make Choice’ button is clicked. If egg is selected then
the checked buttons remains checked. If egg is not selected then the checked buttons are unchecked.
Two example screens (egg is selected and egg not selected) are shown below.
Example when the egg was checked
Example when the egg was not checked
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="chkbox.aspx.vb" Inherits="s05test.chkbox"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>chkbox</title>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:CheckBox id="chkegg" style="Z-INDEX: 100; LEFT: 24px; POSITION: absolute; TOP: 144px"
runat="server" Width="120px" Height="16px" Text="egg"></asp:CheckBox>
<asp:Label id="lbloutput" style="Z-INDEX: 106; LEFT: 192px; POSITION: absolute; TOP: 184px"
runat="server" Width="200px" Height="24px"></asp:Label>
<asp:CheckBox id="chkcheese" style="Z-INDEX: 102; LEFT: 24px; POSITION: absolute; TOP: 192px"
runat="server" Width="120px" Height="16px" Text="cheese"></asp:CheckBox>
<asp:CheckBox id="Chkcereal" style="Z-INDEX: 101; LEFT: 24px; POSITION: absolute; TOP: 168px"
runat="server" Width="120px" Height="16px" Text="cereal"></asp:CheckBox>&nbsp;
<asp:Button id="btnchkbox" style="Z-INDEX: 103; LEFT: 24px; POSITION: absolute; TOP: 224px"
runat="server" Width="128px" Height="24px" Text="Make Choice"></asp:Button>
</form>
</body>
</HTML>
2. a. The checkboxes in the above example is created in a checkbox list. A segment of the HTML code is shown
below. Write VB.NET code segment that will display in a label named ‘lblcount’ the total number of check boxes
followed by text associated with each checkbox separated by colon. (e.g. 3: Egg : Cheese : Cereal)
<asp:checkboxlist id="chklst1" style="Z-INDEX: 101; LEFT: 88px; POSITION: absolute; TOP: 80px" runat="server"
Width="80px" Height="88px" EnableViewState="False">
<asp:ListItem Value="Eggv">Egg</asp:ListItem>
<asp:ListItem Value="Cheesev">Cheese</asp:ListItem>
<asp:ListItem Value="Cerealv">Cereal</asp:ListItem>
</asp:checkboxlist>
1
b. Solve the problem in ‘a’ above but the values of the checkboxes should be displayed. (e.g. 3: Eggv : Cheesev
: Cerealv)
c. Write VB.NET code segment that will display the text associated with the selected text boxes separated by
comma in a label named ‘lbloutput’. Do not use a loop.
d. Write VB.NET code segment that will display the text associated with the selected text boxes all on a separate
line in a label named ‘lbloutput’. Use a loop to solve.
e. Explain what will be the outputs for various selections for the following code segment.
lbloutput2.Text = chklst1.SelectedItem.Value
lbloutput2.Text += "<BR>" & chklst1.SelectedValue()
f. Explain the function of the following code segment.
lbloutput3.Text = chklst1.SelectedIndex
g. Explain the function of the following code segment.
If chklst1.Items(0).Selected = True Then
chklst1.Items(1).Selected = True
End If
h. Explain the function of the following code segment.
If chklst1.Items(0).Selected = True Then
chklst1.Items(1).Text = "something"
End If
3. Add a button to the above page with a label ‘Next Page’. When the user clicks on this button the control is
transferred to another web form, whose name is ‘anotherChkList.aspx’. In ‘anotherChkList.aspx’
page the status of the three checkboxes are shown in a tabular form. An example screen shown below:
Egg
selected
Cereal
Not selected
Cheese
selected
4. Complete all of the problems above by replacing the checkboxes with radio.
5. Complete all of the problems above after replacing the checkboxes with multiple choice dropdown list.
6. Complete all of the problems above after replacing the checkboxes with single choice dropdown list.
7. a. Write complete code in ASP.NET using VB.NET that will dynamically create a table of variable rows and
columns. The number of rows and columns will be input from two text boxes. The content of each cell is taken
from corresponding elements of an array myarray(i, j). The values of each element of the array is given by the
formula: myarray(i, j) = i * j + 1.
b. What value(s) will be displayed in the label control lblcount.
pattern = “The old houses in the old town are very old”
lblcount.Text = InStr(Mid(pattern, cnt + 16, Len(pattern)), "very")
c. Write a simple code that dynamically creates a 2 column x 3 row table. The first column has a serial number
and the second column contains a text box in each row (with default text) where data can be entered. The
entered data should be displayed in a label area when clicked on the button “Show”.
8. Write a simple code that dynamically creates a 3 column x 3 row table. The first column has a name, the second
column an email address and the third column contains a button with Send Mail label on it. When the user clicks
on one of the email button then an email is sent to the email address listed in the second column of the same
2
row in which the email button exists. The sent email content is also displayed in a label on the screen. An
example screen output is shown below.
9. Write a VB.NET code segment that will read the content of a text file and display on the screen.
10. Write a VB.NET code segment that will write to a file ‘test.txt’. The test sentence to write is: “Here is the first
line”. It overwrites any previous contents.
11. Write a VB.NET code segment that will read/write to a binary file.
12. The client disk contains files for different subjects with student grades. Each line of the file contains the name of
a student followed by his/her grades in four subjects each separated by comma. Sample example follows:
John, 32, 45, 65, 85
Mary, 65, 85, 32, 35
…….
Joe, 32, 45, 39, 46
Note that the number of students is not known.
Write COMPLETE code to read the data from the file and display on the screen as shown below. If the user
selects the radio and clicks on the ‘yes’ then the data is stored in a database table. Solve the problem using
a) .NET
b) ASP
Following Grades Extracted from file
John 32
45
65
85
Mary 65
85
32
35
…….
Joe
32
45
39
46
Store in the database
Yes
No
SUBMIT
(Hints: You will need to upload the file from the client and the read the contents of the file. Use string functions such as split(),
Instr() etc. to separate the table contents for display on the screen table.)
13. You need to display on a screen the elements as shown in figures below. When the user clicks on the
Interchange’ button, the location of the text box and the image will interchange as shown in the figure i.e. if the
position of the image was on the top of the text box (as shown in fig a.) then position of the image will shift to
3
below the text box (as shown in fig b.) and vice versa. The iname of the image is laptop.jpg and should be
shown displayed on the screen with a size of 100 x 70 pixels. The textbox width should be 15 characters.
To solve the problem use: a) ASP
b) VB.NET
Fig. a
c) JavaScript
fig.b
14. a. What will be the output of the following HTML segment?
<INPUT id="Filetext" type="file" size="22" name="File1" runat="server"><br>
<asp:button id="btnupload" Text="upload" runat="server"></asp:button><br><br>
<asp:label id="lblmylbl" runat="server"></asp:label><br><br>
<asp:radiobuttonlist id="myrad" runat="server">
<asp:ListItem Value="Yes">Yes</asp:ListItem>
<asp:ListItem Value="No">No</asp:ListItem>
</asp:radiobuttonlist>
<asp:button id="btndb" runat="server" Text="Store in DB" Visible="False"></asp:button><br> <br>
<asp:table id="Table1" runat="server" GridLines="Both"></asp:table>
b. Write code segment to upload a file selected in the above web page satisfying the following conditions:
i. it should be uploaded only if it is between 100 bytes and 25 Kbytes.
ii. the saved file will have the same name as on the client side in a folder named “data”, which located in the
same folder as this code.
c. Write one statement that will display a message in the label control of the above web page a message containing
the name and size of the file.
d. Write statements that will read the second line from the file just uploaded above (assume that uploaded file is a
text file).
e. Assume that the content of each line read from the file contains studentName, studentAddress and
phoneNumber which are separated by commas. Write a code segment that will read a line and add them
dynamically to the row of the table in the code in a above. Assume there are three columns of the table.
f.
Write code segment that will check which radio button is selected and display a message in the label on the
status of the radio selection.
15. Write COMPLETE code to read a text file named “test.asp” and replace all occurrences of the word “old” with the
word “new”. It should display on the screen appropriate messages, such as, if the file was not found and
replacements could not be made, or if the file was found and the replacements were successful. It should also
display the number of replacements made.
Write the code using a) ASP.NET
b) ASP
4
16. Given the following code stored in a file named breakfast.aspx. Answer the related questions
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="breakfast.aspx.vb" Inherits="probtest.breakfast"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<body MS_POSITIONING="GridLayout">
<form runat="server" ID="Form1">
<asp:label id="lblOutput" runat="server" />
<h4>Please enter your name: </h4>
<asp:TextBox id="txtName" runat="server">
</asp:TextBox>
<h4>What would you like for breakfast?</h4>
<asp:CheckBoxList id="chkEntree" runat="server">
<asp:listitem runat="server" value="Cereal" />
<asp:listitem runat="server" value="Eggs" />
<asp:listitem runat="server" value="Pancakes" />
</asp:CheckBoxList>
<h4>Feed me:</h4>
<asp:RadioButtonList id="radTime" runat="server">
<asp:listitem runat="server" value="Now" />
<asp:listitem runat="server" value="Later" />
</asp:RadioButtonList>
<h4>Click 'submit' to process</h4>
<input type="submit" value="Submit" />
</form>
</body>
</HTML>
Add a Page_Load() event handler to the above aspx code to display the confirmation message with the
selections made. The screen output should be in the following format. The information in parenthesis in italic will
not appear on the screen. It indicates the content of the blank spaces. The confirmation message should be
shown in the label area.
Thank you very much ___________ (name of the person)
You have chosen ___________________ (breakfast items) for breakfast.
I will prepare for you ________ (time)
17. A database table contains UserId, password and a magic number for each user. To get access to a given
application a user must enter correctly the UserId, Password and the magic number matching a database entry.
Following screens are used to validate a user. Screen Page 2 appears after page 1 is submitted, page 3 appears
after page 2 is submitted and page 4 appears after page 3 is submitted. The database table entry is matched
with the user entries only after the user has entered the magic number on page 3. Only one of the
message “You are denied access” or “Welcome you to our world!” is displayed on page 4 depending if a match
has not been found or if a match is found.
Page 1
Page 2
Enter User ID:
Enter Password:
Submit
Submit
Page 3
Page 4
Enter Magic No:
You are denied access. OR
Welcome you to our world!
Submit
5
Write complete code to obtain the above functionality using:
a) ASP
b) ASP.NET assuming that all the screens are separate webforms.
c) ASP.NET assuming that all the screens are contained in one single webform.
18. a. Given the following code segment.
parttemp =”All thieves are human, but all human are not thieves”
count = InStr(parttemp, "thieves")
parttemp = mid(parttemp, count+3, len(parttemp))
What will be the output value of count and parttemp after execution of the above code.
b. Given the following code segment.
parttemp =”All thieves are human, but all human are not thieves”
Replace(parttemp, “thieves”, “human”)
Replace(parttemp, “human”, “thieves”)
What will be the output value of parttemp after execution of the above code.
19. Given the following code stored in a file. Answer the related questions. (Similar questions may be using ASP
code)
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="breakfast.aspx.vb"
Inherits="probtest.breakfast"%>
<HTML>
<body MS_POSITIONING="GridLayout">
<form runat="server" ID="Form1">
<asp:label id="lblOutput" runat="server" />
<h4>Lunch Ticket: </h4>
<asp:CheckBoxList id="lunch" runat="server">
<asp:listitem runat="server" value="Monday" />
<asp:listitem runat="server" value="Tuesday" />
<asp:listitem runat="server" value="Wednesday" />
</asp:CheckBoxList>
<h4>Payment:</h4>
<asp:RadioButtonList id="payment" runat="server">
<asp:listitem runat="server" value="check" />
<asp:listitem runat="server" value="Visa" />
<asp:listitem runat="server" value="Master" />
</asp:RadioButtonList>
<h4>Click 'submit' to process</h4>
<input type="submit" value="Submit" />
</form>
</body>
</HTML>
a. Write statements to display a message in the above label area depending on the selection of lunch. If
Monday has not been selected the message should be ‘you have not selected Monday’, otherwise the
message should be ‘your selections has been processed’.
b. Write a statement to display the value of the second checkbox (selected or not).
c. Write a statement to find out the number of checkboxes in the group ‘lunch’.
d. Write statements that will display in the lbloutput area values of all radio buttons in the group ‘payment’ all
separated by colons.
6
20. Following code is intended to retrieve customer names from a database table. All customer names should be
displayed on the screen separated by comma. Write the code segments in the text boxes that would be required
to get the above desired output.
<html><head><title>Practice with SQLPractice</title></head>
<body>
<%
strconn = "DRIVER=Microsoft Access Driver (*.mdb);DBQ=" & Server.MapPath("forfinal.mdb")
SET objConn = Server.CreateObject("ADODB.Connection")
objConn.Open strconn
strSQL = "SELECT custname, custaddr, balance FROM customer"
strSQL = strSQL & " WHERE balance >= 100;"
Set objRec = Server.CreateObject ("ADODB.Recordset")
objRec.Open strSQL, objConn
While Not objRec.EOF
Wend
‘Closing statements
%>
</body></html>
7