Please enable JavaScript.
Coggle requires JavaScript to display documents.
20 QUESTION TEST (2) (Write a section of code that assigns the name "…
20 QUESTION TEST (2)
- Write a section of code that assigns the name "Fred Bloggs" to a variable called StudentName.
-
- A form has a text box called txtUserName. Write a section of code that copies the text property of the control to an appropriate variable.
protected void btnStoreUserName_Click(object sender, EventArgs e)
{
string UserName;
UserName = txtUserName.Text;
}
- Does the computer care what names we give to our objects, controls and variables? Explain why.
Computer is not bothered what names are given but certain C# rules are required to be implemented for variable naming:
- must not begin with a number
- must contain only alphabetic characters, decimal digits, underscores
- not contain periods, commas - No special characters (/*-+_@ &$#%) they have a special meaning in C#
- no spaces in variable names
- not be more than 255 characters long
- not have the same name as a C# keyword, statement, method or command
- must be unique within their scope of declaration.
(E.g. can't declare two variables called intCounter in the same event procedure, nor declare two variables called m_intCounter in the Declarations section of a form.)
- Write a section of cde that assigns the value 20 to a variable called ClassSize.
-
- Declare a variable to store a person's first name initialised with the string "John".
-
- Declare a variable to store a telephone number
-
- what is the = sign and what does it do?
Assignment operator, copies values from one value/variable on the right to another on the left.
- Declare a variable to store the price of a kettle
-
- Declare a variable to store a person's age initialised with the age 23.
-
- What is wrong with the following if statements?
(a)
if Age < 18;
{
lblMessage.Text = "You are too young"
}
(b)
if (Age > 30)
lblMessage.Text = "You are too old";
(a) no brackets around the condition, semicolon shouldn't be after condition, should be "you are too young";
(b) missing curly braces define start and finish of the function.
- Write an "if statement" that reads data into a variable called UserName from an approriate text box. The message "you are Fred" should be displayed in an appropriate label.
protected void btnStoreFred_Cick(object sender, EventArgs e)
{
if(txtUserName.Text == "Fred")
{
string UserName = txtUserName.Text;lblMessage.Text = "You are" + UserName;
}
}
- What is wrong with the following validation?
string Error = "";
try
{
Int32 AgeTemp = Age;
}
catch
{
Error = "Age is not valid";
}
StoreAge(Age);
- User will never see the Error in catch section even if Age is not of Type Int32. Should be displayed in appropriate label (lblError) not the string.
- StoreAge(Age) function is used to store the age but Int32 AgeTemp = Age; already stores the age, no need to store age twice.
- The following function has been created...
void DeleteMembers()
{}
What is wrong with the following code?
Delete Member()
The line of code does not invoke the DeletMembers() function. There is a syntax error with the space between Delete and Member() and the spelling is wrong.The DeleteMembers() funtion appears not to be designed to delete a specific member. Ideally the function declaration should include a MemberID parameter, used to delete a member of choice:
void DeleteMember(MemberID)
{}
- Write a while loop that loops counting backwards from 10 to 0 adding the results to a list box called lstItems.
-
- What would be suitable return data types for the following functions?
Student Exists();
GetFirstName();
GetGroupNumbers();
GetDateofFirstTransaction();
return data types: Boolean, string, Int32, DateTime
- What is wrong with the following loops?
(a)
Int32 Index = 0;
while (Index < 10)
{
Index--;
}
(b)
Int32 Index = 0;
while (Index < 10)
{
}
In first while loop is infinite loop: value condition Index<0 is always true as Index is decremented always and will never be greater than 10.
In second while loop is infinite loop: Index is never increment so value never increases above 0
- Along with an example what do parameters do?
- Parameters are named variables of a particular type used to pass data into functions/procedures. The values passed in are called arguments.
In this example we have a function called Square:
Int32 Length = 3; Int32 Width = 3;
public void Shape(Int32 x, Int32 y)
{
Length = Length + x;
Width = Width + y;
}
- If we create an object from the class we can use the parameters x and y to set values for Length and Width:
Square MyShape = new Square(1,2);The parameters allow us to create an object called MySquare, by passing values of 2 (for x) and 1(for y). This object MySquare will have width = 4, Length = 5.
- Write a function declaration that tells us the number of days in the current year including a parameter allowing us to specify the year.
- Int32 NumberofDays(DateTime YearSpecified)
{}
- The following function needs a suitable parameter. Modify the definition to include something appropriate
void PrintDayReport()
{}
- void PrintDayReport(Int32 NumberofCopies)
{}
- A function accepts a parameter to specify the year and returns the number of days for that year. Write a section of code that assigns it to an appropriate variable.
-