Looks like no one added any tags here yet for you.
Scope
Where (in which part of the program) a variable can be accessed or used
State the scope of any particular variable (what part of the program it encompasses)
Begins at the variable’s declaration and ends at the end of the method in which the variable is declared
Lifetime
The time period during which the variable exists in memory
State the lifetime of any particular variable (what part of the program it encompasses)
From the beginning of the method in which it is declared to the end of that method
Display the existing decTotal variable in the txtTotal textbox with currency formatting, 0 decimal places.
txtTotal.Text = decTotal.toString(“c0”);
Add together the existing variables named decSubtotal and decItemPrice and assign the result to decSubTotal
decSubTotal = decSubTotal + decItemPrice;
Add together the existing variables named decNum1 and dblNum2 and assign the result to decSum.
decSum = decNum1 + (decimal)dblNum2;
Divide the existing variables named intSum by intCount and assign the result to dblTotal. Do not allow integer division.
dblTotal = (double)intSum / intCount;
Assign a numeric value typed into a textbox named txtInputScore to the integer variable named intTestScore.
intTestScore = int.Parse(txtInputScore.Text);
Build a string containing a person’s full name (separated by spaces: first middle last) and store it in the variable strFullName. The first, middle, and last names can be obtained from the following variables: strFirst, strMiddle, and strLast (contains name only, no spaces)
strFullName = strFirst + “ “ + strMiddle + “ “ + strLast;
Add the full name of person (in “firstname lastname” format) to the lstOutput list box. The first and last names can be obtained from the following variables: strFirst and strLast (contains name only, no spaces).
lstOutput.Items.Add(strFirst + “ “ + strLast);
What structure is used to process a group of radio buttons?
A single if-elseif block
What structure is used to process a group of checkboxes?
A seperate if block for each one
What does lstNums.Items.Count produce?
6
What does lstNums.Items.Text produce?
20 (string)
What does lstNums.SelectedItem produce?
20 (object)
What does lstNums.SelectedIndex produce?
3
What does lstNums.Items[1] produce?
10 (object)
What does lstNums.Items[lstNums.Items.Count -1] produce?
30 (object)
What does lstNums.Items[lstNums.Items.Count -2] produce?
25 (object)
What does lstNums.Items[lstNums.SelectedIndex] produce?
20 (object)
Given that lstNames contains a list of names and lstAges contains a list of ages. Write the code to display the selected name and age (formatted as "Bob is 25 years old") to the txtOutput textbox. You will need to assign the selected items to the appropriate variables and use the variables to build the formatted string that is displayed to txtOutput.
string name;
int age;
name = lstNames.Text;
age = int.Parse(lstAges.Text);
txtOutput.Text = name + “is” + age + “years old”;
Given that lstBox1 and listBox2 both contain lists of grades. Write the code to assign the selected numbers from lstBox1 and listBox2 to the variables num1 and num2. Compute and assign the average to the variable avg. Display the average to the txtOutput textbox (the sum should be formatted as Number, 2 decimal places).
int num1, num2;
double avg;
num1 = int.Parse(lstBox1.Text);
num2 = int.Parse(lstBox2.Text);
avg = (double)num1 / num2;
txtOutput.Text = avg.toString(“n2”);
Given that number has been declared as an Integer variable and assigned a value. Complete the code below by writing an If block to determine if the user input number is a multiple of 5 (hint - use the MOD operator to see if its divisible by 5) and display the result ("yes" or "no") to the txtResult textbox.
int number;
number = int.Parse(txtNumber.Text);
if (number % 5 == 0)
{
txtResult.Text = “yes”;
}
else
{
txtResult.Text = “no”;
Given that age has been declared as a integer variable. Complete the code below to validate and input the user's age from a textbox (txtInput) to an integer variable (age). You must validate that txtInput contains numeric value from 0 to 100 (inlusive). If not valid for either case, give an error message (using a Message Box) and end the procedure (only the procedure, not the whole program).
int age;
if (!int.TryParse(txtInput.Text, out age))
{
MessageBox.Show(“You must enter a valid age”);
return';
if (age < 0 || age > 100)
{
MessageBox.Show(“You must enter an age from 0 to 100”);
}
Write the partial code to the text of the selected item from the lstNames list box (as a string)
lstNames.Text
Write the partial code to the index number of the selected item from the lstNames list box
lstNames.SelectedIndex
Write the partial code to the second item from the lstNames list box
lstNames.Items[1]
Write the partial code to the next to last item from the lstNames list box
lstNames.Items[lstNames.Items.Count -2]
Count backwards from 100 to 0 by 5
for (int i=100; i>=; i-=5);
List all of the multiples of 8, from 8 through 100
for (int i=8; i<=100; i+=8);
Do something with every letter of the string variable word
for (int i=0; i<word.length; i++);
Do something with every letter of the string variable word in reverse order
for (int i=word.length; i>=0; i—);
Do something with every item in the lstStuff list box
for (int i=0; i<lstStuff.Items.Count; i++);
Do something with every other item in the lstStuff list box
for (int i=0; i<lstStuff.Items.Count; i+=2);
Given that lstBox1 contains a list of numbers. Write a For Loop to display every number from lstBox1 in lstBox2.
for (int i=0; i<lstBox1.Items.Count; i++)
{
lstBox2.Items.Add(lstBox1.Items[i].toString());
}
Given that lstBox1 contains a list of numbers. Write a For Loop to display every other number from lstBox1 (starting with the first number) in lstBox2.
for (int i=1; i<lstBox1.Items.Count-1; i++)
{
lstBox2.Items.Add(lstBox1.Items[i].toString());
}
Given that lstBox1 contains a list of numbers. Write a For Loop to display every number from lstBox1 in reverse order in lstBox2.
for (int i=lstBox1.Items.Count-1; i>=0; i—)
{
lstBox2.Items.Add(lstBox1.Items[i].toString());
}
Given that an OpenFileDialog named ofd has been added to the form. Complete the code below to show the dialog and validate that a file was selected. If the user did not select a file (ofd does not return DialogResult.OK), give an error message (via MessageBox) and end the procedure. Otherwise, save the selected filename to the fName variable declared below.
string fName;
if (ofd.ShowDialog() != DialogResult.OK)
{
MessageBox.Show(“You must select a valid file”);
return;
}
fName = ofd.fileName;
Given that the file selected above contains a list of numbers (int data type). Complete the code below to use StreamReader to open the filename stored in the fName variable. Read every line in the file to compute and store the sum of all of numbers to the total variable declare below. You can use the other variables declared below to store each line as you read and convert it. Don’t forget to declare your StreamReader object.
string line;
int num, total = 0;
StreamReader rdr;
rdr = File.OpenText(fName);
while (!rdr.EndOfStream)
{
line = rdr.ReadLine();
num = int.Parse(line);
sum += num;
}
rdr.Close()
Given that the fName string variable from above contains a valid filename. Complete the code below to use StreamWriter to open the filename stored in the fName variable and add the two numbers stored in the num1 and num2 variables to end of the file. Don’t forget to declare your StreamWriter object.
int num1 = 50, num2 = 100;
StreamWriter wtr;
wtr = File.AppendText(fName);
wtr.WriteLine(num1.toString());
wtr.WriteLine(num2.toString());
wtr.Close();
Given that lstNames contains a list of names and that a SaveFileDialog named sfd has been added to the form. Complete the code below to show the dialog and validate that a file was selected. If the user did not select a file (sfd does not return DialogResult.OK), give an error message (via MessageBox) and end the procedure. Otherwise, use StreamWriter to write every name from lstNames to the selected file (overwrite or create new).
if (sfd.ShowDialog() != DialogResult.OK)
{
MessageBox.Show(“You must select a valid location/filename”);
return;
}
StreamWriter wtr;
wtr = File.CreateText(sfd.FileName);
for (int i=0; i<lstNames.Items.Count; i++)
{
wtr.Writeline(lstBox1.Items[i].toString());
}
wtr.Close();
What will be the value of z after the following code is executed?
int x, y;
double z;
x = 15;
y = 2;
z = x / y;
7
What will be the value of z after the following code is executed?
int x, y;
double z;
x = 15;
y = 2;
z = x / y;
7.5
What will be the output of the following code?
string word1, word2, newWord;
word1 = "shower";
word2 = "about";
newWord = word1.Substring(0, 4) + word2.Substring(0, 2);
if (newWord.IndexOf("how") == -1)
{txtBox.Text = "lost";}
elseIf (newWord.Length > 6)
{txtBox.Text = "long";}
elseIf (newWord.IndexOf("how") > -1)
{txtBox.Text = "found";}
elseIf (newWord.Length <= 6)
{txtBox.Text = "short";}
found
What word(s) will appear in the list box when the following code is executed?
int num = 5;
if (num != 2)
{lstBox.Items.Add(“Not Two”);}
elseif (num > 3)
{lstBox.Items.Add(“Greater'“);}
elseif (num == 5)
{lstBox.Items.Add(“Equal”);}
Not Two
What will be the output? Assume the user checks only the chk2 and chk3 checkboxes.
int num = 0;
if (chk1.checked)Num += 4;
if (chk2.checked)Num += 6;
if (chk3.checked)Num += 8;
if (chk4.checked)Num += 10;
txtBox.Text = num.toString();
14
What will be displayed by the following code?
double a, b, c, x';
a = 8;
b = 5;
c = 17;
if (a * 2 > c)
{
x = 5;
}
elseIf (a <= 8)
{
if (b < a Then)
x = 10;
elseIf (c > (a + b))
x = 15;
else
x = 20;
}
Else
{
x =25;
txtBox.Text = x.toString();
10
What numbers will be displayed in the list box by the following code? What is the value of num after the loop terminates?
double num = 2;
while (num < 15)
{
lstBox.Items.Add(num)
num = num + 4;
}
2, 6, 10, 14, num = 18
How many times will HI be displayed when the following lines are executed? What is the value of c after the loop terminates?
int c = 8;
while (c < 20)
{
lstBox.Items.Add(“HI”);
c += 4;
}
3, c = 20
Given that x = 1, y = 20, and z = 3, how many times will the following If block will display “hello” in the listBox?
for (int cntr = c; x <= y; x += z)
{
lstBox.Items.Add(“hello”);
}
7
What sum will be displayed in the list box when the following lines are executed?
int sum = 0;
int num = 1;
while (num < 4)
{
for (int x = 1; x <= 2; x++)
{
sum += x;
}
num += 1;
}
lstBox.Items.Add(sum);
9
Given the following partial program, how many times will “Hello” be displayed?
for (int j = 1; j <= 2; j++)
{
for (int k = 0; k < 3; k++)
{
for (int m = 4; m >= 1; m—)
{
lstBox.Items.Add(“Hello”);
}
}
}
24
What will be displayed in txtBox when the following code is executed?
string word, lttr, newWord = ““;
word = “ABCDEFGHI”;
for (int i = 0; i < word.Length; i += 3)
{
lttr = word.Substring(i, 1);
newWord = newWord + ltter;
}
txtBoxt.Text = newWord;
ADG
Given that the lstLetters listbox contains: A B C D E
What would be the output to the txtWord textbox by the following code segment?
for (int i = 1; i <= lstLetters.Items.Count; i += 2)
{
txtWord.Text += lstLetters.Items[i-1];
}
ACE