DONE INF164 2021 Semester Test Memo v4 PDF
DONE INF164 2021 Semester Test Memo v4 PDF
INFORMATICS 164
SEMESTER TEST DATE: 11 October 2021
Examiners : Dr Mawela
: Dr Mennega
Mr Mazima Time : 120 min
Mr Mthethwa
Section A M01-M08 7
Section B M01-M08 18
Total 50
Instructions
1. This paper consists of 4 sections.
2. Answer all the questions – there are no optional questions.
3. This is an open book test.
4. Type your answers in the online format presented.
5. Note: Assume implicit conversion is deactivated and all code makes use of strict typing.
"The University of Pretoria commits itself to produce academic work of integrity. I affirm that I am aware of and have read the Rules and Policies of the
University, more specifically the Disciplinary Procedure and the Tests and Examinations Rules, which prohibit any unethical, dishonest or improper conduct
during tests, assignments, examinations and/or any other forms of assessment. I am aware that no student or any other person may assist or attempt to
assist another student, or obtain help, or attempt to obtain help from another student or any other person during tests, assessments, assignments,
examinations and/or any other forms of assessment."
Question 1 (2 Marks)
Question Answer
Consider the figure below which represents an array that may store the sales double[ , ] salesPerYear = new double[4,3]
figures (in rands) of a toy store per year.
Provide the code that will declare this array named: salesPerYear
Review the figure below which represents an array that may store the rainfall double[ , ] rainfallPerYear = new double[3,2]
figures (in millimeters) for Pretoria per year.
Provide the code that will declare this array named rainfallPerYear:
Consider the figure below which represents an array that may store the monthly double[ , ] costsPerMonth = new double[5,4]
expenses (in rands) of a household.
Provide the code that will declare this array named costsPerMonth
Question Answer
Provide the 2 lines of code that will find the number of rows in a DataGridView dgvBooks.Rows.Count;
named dgvBooks AND change the colour of the text of the cell that is in the first dgvBooks.Rows[0].Cells[0].Style.ForeColor = Color.Yellow;
row/first column of the DataGridView to yellow
Give the code to find the number of columns in a DataGridView named dgvToys dgvToys.Columns.Count;
AND change the colour of the cell that is in the first row/first column of the dgvToys.Rows[0].Cells[0].Style.BackColor = Color.Blue;
DataGridView to blue.
Write the code to find the number of columns in a DataGridView named dgvCars dgvCars.Columns.Count;
AND change the colour of the text of the cell that is in the first row/first column of dgvCars.Rows[0].Cells[0].Style.ForeColor = Color.Red;
the DataGridView to red
Question 3 (2 Marks)
Question Answer
a)Explain how the following line of code can cause an exception. (2 If the users do not enter anything or enter a letter instead of a number. √
marks) , an exception will occur since the conversion will not be able to take place. √
int memberCode = Convert.ToInt32(txtMemberCode.Text);
a)Explain how the following line of code can cause an exception. (2 If the users do not enter anything or enter a letter instead of a number. √
marks) , an exception will occur since the conversion will not be able to take place. √
int districtID = Convert.ToInt32(txtDistrictIdentifier.Text);
a)Explain how the following line of code can cause an exception. (2 If the users do not enter anything or enter a letter instead of a number. √
marks) , an exception will occur since the conversion will not be able to take place. √
int studentNumber = Convert.ToInt32(txtStudentNum.Text);
Which of the following statements gives the number of rows in a DataGridView c) dgvSalary.Rows.Count – 1
called dgvSalary (ignoring the * row)?
a. dgvSalary.CurrentCell.Count
b. dgvSalary.CurrentCell.Count – 1
c. dgvSalary.Rows.Count – 1
d. dgvSalary.Rows.Count
e. dgvSalary.Rows.Count – 2
Question 1 (3 Marks)
Question Answer
The block of code below calls a method that does a simple division calculation. In Solution
this case, the programmer forgot that the divisor is zero, hence the program will
crash due to a math exception error. try (1)
{
Alter the code below using the appropriate exception handling concept to ensure
that it does not crash. You should also display a message box in the instance of a }
possible error.
catch (DivideByZeroException) (1)
{
MessageBox.Show("Error caused by dividing by zero."); (1)
}
OR
catch (Exception GeneralException) (1)
{
MessageBox.Show(“General Exception” + GeneralException.Message); (1)
}
The block of code below lets the user select any song of their choice to play using try (1)
the windows media player control. In this case, the last line of the code causes the {
program to crash due to a general exception error.
The block of code below uses the Load File Method to read a “note.rtf” file in the Solution
debug folder. In this case the INF154 student typed in the wrong file name, hence
the program will crash due to a file not found exception. try (1)
{
Alter the code below using the file not found exception handling concept to ensure
that it does not crash. You should also display a message box in the instance of a }
possible error. catch (FileNotFoundException Exception) (1)
{
MessageBox.Show(“FileNotFoundException Exception” + Exception.Message); (1)
}
Question 2 (3 Marks)
Question Answer
Consider the following screenshot of an event handler with an array called Solution
gradesArr and complete the code. When btnSaveGrades is clicked, save the list of
module codes along with the grades in a text file called ‘grades.txt’ on three StreamWriter outputFile = new StreamWriter("grades.txt"); (1)
separate lines. for (int i = 0; i < gradesArr.Length; i++)
{
outputFile.WriteLine(gradesArr[i]); (1)
}
Additional Instructions
Note: use the table option that is available in ClickUP to draw your
datagridview answer. See below:
Draw what the Data Grid View dgvDiagram will look like after the code below has Solution
been executed. Leave out the column and row headers.
2 marks for correct dimension (4columns by 4rows)
4 marks for correct values X’s and O’s
Note: use the table option that is available in ClickUP to draw your
datagridview answer. See below:
Or you can draw it, take picture of your drawing and upload it as part
of your answer.
Draw what the Data Grid View dgvTable will look like after the code below has
been executed. Leave out the column and row headers. Solution
Note: use the table option that is available in ClickUP to draw your
datagridview answer. See below:
Or you can draw it, take picture of your drawing and upload it as part
of your answer.
Question 4 (6 Marks)
Question Answer
Complete the class below that will enable the C# windows form to print the
following information in a dataGridView (Item, Quantity, Price, Total) when the A
Add button is pressed. public Sales(string Item, int Quantity, double Price)
{
Consider the following screenshots of a Sales class and point of sale screen and mItem = Item;
provide code for the following mQuantity = Quantity;
mPrice = Price;
A. the overload constructor which takes in 3 parameters only as shown in the mTotal = CalculateTotal();
POS screen. }
B. method that will calculate the total of a new sale captured called 2 marks for a parameter constructor with correct data types
CalculateTotal. 1 mark for correct assigning of variables
1 mark for method calling statement
B
public double CalculateTotal()
{
A. a binding list which will store objects of the Sales class called salesList. B
(2) Sales newSale = new Sales
(
txtItem.Text,
B – Alternative
Sales newSale = new Sales();
newSale.Item = txtItem.Text;
newSale.Price = Convert.ToDouble(txtPrice.Text);
newSale.Quantity = Convert.ToInt32(txtQuantity.Text);
newSale.Total = newSale.CalculateTotal();
salesList.Add(newSale);
1 mark for creating object eg. newSale or sale
2 marks for assigning controls and using method
1 mark for adding new sale to list
Question 1 (2 Marks)
Question Answer
Why will the code not work? Answer:
The third parameter of the method GetHMS should be a ref parameter
Question 2 (2 Marks)
Question Answer
Consider the following code, which shows the first few lines of a class “Pet” Answer:
being written: public Pet(string name, int age, string type)
{
mName = name;
mAge = age;
mType = type;
}
Consider the following code, which shows the first few lines of a class “Pet” Answer:
being written:
public string Name
{
get { return mName; }
set { mName = value; }
}
Consider the following code, which shows the first few lines of a class “Pet” Answer:
being written: public int Age
{
get { return mAge; }
set { mAge = value; }
}
Complete the code needed for the Age property of the data member mAge.
(2 Marks)
Question 3 (1 Mark)
Question Answer
The markup part of _____ structures the web page when it is opened in a Answer: HTML
browser such as Chrome.
Code running in the web browser is known as ______-side code Answer: client-side
Question 4 (1 Mark)
Question Answer
True or False Answer: True
Question 5 (1 Mark)
Question Answer
Multiple Choice Answer: c
Most of the code to support dynamic web pages, run on the server.
a This is called "server-side programming"
b This is called "back-end scripting"
c Both of the above
d None of the above
Question 1 (3 Marks)
Question Answer
Consider the method and button event handler below (i) calculatePrice(Cost, ref Markup, out Price);(2)
(i) Provide an appropriate calling statement. (ii) R87,04 (1)
(ii) Provide the answer for Price in Rands (what will the amount be when the code is
executed).
Consider the method and button event handler below (i) calculateCostPrice(out Cost, ref MarkupPercent, ref
(i) Provide an appropriate calling statement. Price);(2)
(ii) R86 (1)
(ii) Provide the answer for Cost in Rands (what will the amount be when the code is
executed).
1 Mark double
1 Mark (double Income, doubleExpenses)
1 Mark Correct return
Consider the event handler below: public void calculateweight(double mass, double gravity)
Provide the code for the calculateWeight Method {
weight = mass * gravity;
Formula for Weight: Weight = mass x gravity }
1 Mark void
1 Mark (double mass, double gravity)
1 Mark Correct use of global variable
Consider the event handler below: public int calculateArea(int length, int breadth)
Provide the code for the calculateArea Method {
return length * breadth;
}
1 Mark double
1 Mark (int length, int breadth)
1 Mark Correct return
Question 3 (6 Marks)
Question Answer
Complete the class below: public Plants()
{
Consider the following screenshots of a Plant class and nursery database mName = "";
mSunExposure = "";
and provide code for the following mBloomingSeason ="";
A. Default Constructor for the class (3) mWater = 0;
B. The Nursery is running a promotion to sell plants that bloom in the price = 0 ;
summer. Create a method in the class that checks if the blooming season }
is Summer and returns true or false (3)
B. public bool Christmasgift()
{
if (mBloomingSeason == "Summer")
{
return true;
}
else
{
return false;
}
}
A. The code necessary to retrieve the “newClient” object from the dgvClients.DataSource = ClientList; (1)
AddClientForm, add it to the ClientList and make list the data
source of a DataGridView named “dgvClient” (3) B. newClient = new Client();(1)
B. The code necessary to initialize the newClient object with the newClient.Name = name;
variables and close the form (3) newClient.Age = Age;
newClient.Surname = surname;(1)
this.Close();(1)
Question Answer
When the btnSaveCustomers event handler is clicked it should search through the for (int i = 0; i < 10; i++)
{
datagridview(dgvCustomers) and retrieve the first 10 customers, add them to a for (int j = 0; j <dgvCustomer.Columns.Count; j++)
binding list and write the list to a serialized file name “customers.ser”. {
currentcustomer = new customer();
currentcustomer.ID = Convert.ToInt32(dgvCustomer[j, i].Value);
Provide all the necessary code to make this possible. currentcustomer.Name = dgvCustomer[j, i].Value.ToString();
currentcustomer.Surname = dgvCustomer[j, i].Value.ToString();
currentcustomer.AccountType = dgvCustomer[j,i].Value.ToString();
Consider the Screen shot below:
myCustomersList.Add(currentcustomer);
}
}
FileStream outFile = new FileStream("Customers" + ".ser", FileMode.Create,
FileAccess.Write);
BinaryFormatter bFormatter = new BinaryFormatter();
bFormatter.Serialize(outFile, myCustomersList);
outFile.Close();
datagridview(dgvCustomers) and retrieve the customers with Gold Account if (dgvCustomer[3, i].Value.ToString() == "Gold")
Types, add them to a binding list and write the list to a serialized file name {
for (int j = 0; j < dgvCustomer.Columns.Count; j++)
“GoldCustomers.ser”. {
goldcustomer = new customer();
goldcustomer.ID = Convert.ToInt32(dgvCustomer[j, i].Value);
Provide all the necessary code to make this possible.
}
FileStream outFile = new FileStream("GoldCustomers" + ".ser", FileMode.Create,
FileAccess.Write);
BinaryFormatter bFormatter = new BinaryFormatter();
bFormatter.Serialize(outFile, myCustomersList);
outFile.Close();
}
FileStream outFile = new FileStream("EvenCustomers" + ".ser",
FileMode.Create, FileAccess.Write);
BinaryFormatter bFormatter = new BinaryFormatter();
bFormatter.Serialize(outFile, myCustomersList);
outFile.Close();