0% found this document useful (0 votes)
4 views

INF164 EXAM PREP (3)

The document is an exam preparation guide for INF164, covering topics such as Object-Oriented Programming (OOP), file handling, multi-dimensional arrays, and DataGrid view problems. It includes various questions and coding exercises related to these topics, along with expected outputs and instructions for exception handling. The guide is structured into parts with detailed questions aimed at testing students' understanding and application of programming concepts.

Uploaded by

lkgatla21
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

INF164 EXAM PREP (3)

The document is an exam preparation guide for INF164, covering topics such as Object-Oriented Programming (OOP), file handling, multi-dimensional arrays, and DataGrid view problems. It includes various questions and coding exercises related to these topics, along with expected outputs and instructions for exception handling. The guide is structured into parts with detailed questions aimed at testing students' understanding and application of programming concepts.

Uploaded by

lkgatla21
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

by Mashigo VM ©2024

INF164 EXAM PREP 2024

INF164 EXAM PREP 2024

Believe in your preparation; you’re closer to success than you think

1
by Mashigo VM ©2024
INF164 EXAM PREP 2024

PART 1: OOP...........................................................................................................................3

🤡
1. File Handling [29 marks].................................................................................................3
Question 1 [14 marks].............................................................................................3
Steps to Determine the Solution if the Roots Are Real................................................ 3
Question 1.1 [4 marks]........................................................................................... 5
Question 1.2 [10 marks]......................................................................................... 7
Question 2 [4 marks].................................................................................................... 9
Question 3 [6 marks].................................................................................................. 10
Question 4 [5 marks]...................................................................................................11
Question 4.1 [2 marks]..........................................................................................11
Question 4.2 [3 marks]..........................................................................................11
2. Multi dimensional array................................................................................................ 12
Question 1 [ 15 marks]............................................................................................... 12
Questions 1.1 [3 marks]........................................................................................12
Question 1.2 [2 marks]......................................................................................... 12
Question 1.3 [4 marks]......................................................................................... 12
Question 1.4 [6 marks]......................................................................................... 12
3. DataGrid view and arrays related problems (without binding lists).............................. 13
Question 1 [31 marks]................................................................................................ 13
Question 1.1 [4 Marks]......................................................................................... 13
Question 1.2 [6 marks]......................................................................................... 14
Question 1.3 [10 marks]....................................................................................... 15
Question 1.3.1 [4 marks]................................................................................ 15
Question 1.3.2 [3 marks]................................................................................ 15
Question 1.3.3 [3 marks]................................................................................ 15
Question 1.4 [5 marks]......................................................................................... 15
Question 1.5 [6 marks]......................................................................................... 16

2
by Mashigo VM ©2024
INF164 EXAM PREP 2024

PART 1: OOP

1. File Handling [29 marks]

Question 1 🤡 [14 marks]


The nature of roots of a quadratic equation ax2+bx+c=0 can be determined using the
discriminant D, which is calculated as:

D = b2− 4ac

The discriminant tells us the nature of the roots as follows:

1. If D > 0: The roots are real and distinct.


2. If D = 0: The roots are real and equal (also called a repeated root).
3. If D < 0: The roots are complex (not real).

Steps to Determine the Solution if the Roots Are Real

1. Calculate the Discriminant: Find D=b2−4ac


2. Check the Nature of Roots:
○ If D > 0, the equation has two distinct real roots.
○ If D = 0, the equation has one real root (a repeated root).
3. Solve for the Roots (if real):
○ Use the quadratic formula:
−𝑏± 𝐷
x=​​
2𝑎
○ Substitute D and calculate to find the values of x.
4. Interpret the Result:
○ If D > 0 , you’ll obtain two different values for x (two distinct roots).
○ If D = 0, both values will be the same, giving you a repeated root.

Vince was given a set of maths equations, and he decided to add the equations into a txt file
named problems.txt. Note equation is represented by the coefficients of x and it’s comma
separated i.e (in the form a,b,c. Study the code that follows and answer the questions.

3
by Mashigo VM ©2024
INF164 EXAM PREP 2024

Original equations
problems.txt

4
by Mashigo VM ©2024
INF164 EXAM PREP 2024

Initial form design


Names of the controls rtxProblems, rtxSolutions, btnSolution

Question 1.1 [4 marks]


Complete the form load event such that when the form loads the information from the file is
transferred to a textbox. Also state the namespace that must be included for the code to
work properly.

private void Form1_Load(object sender, EventArgs e)


{
//Your code goes here
}

Expected output:

5
by Mashigo VM ©2024
INF164 EXAM PREP 2024

6
by Mashigo VM ©2024
INF164 EXAM PREP 2024

Question 1.2 [10 marks]


Complete the code 1. - 6. under the submit button event handler. Below the code there’s an
expected output. [6 marks]

After completing the code, analyse the code and state what can go wrong (think of exception
handling)? Provide the necessary code that will handle such exception [4 marks]

1. Declare the necessary Stream object


double a = 0, b = 0, c = 0; //to store coefficients of x
double D = 0; //to store the discriminant
double x1 = 0, x2 = 0; //to store your solutions

string line = input.ReadLine();


string result = "";
while (2. Condition)
{
string[] arr = 3. //necessary code to split the current line
a = Convert.ToDouble(arr[0]);
b = Convert.ToDouble(arr[1]);
c = Convert.ToDouble(arr[2]);

D = 4. //code to calculate D

if (D < 0)
{
result += "The equation has non real roots\n";

}
else
{
if (D == 0)
{
result += $"Real roots, repeated. x = {(-1 * b) / (2 *
a)}\n";
}
else
{
x1 = (-1 * b + Math.Sqrt(D)) / (2 * a);
x2 = (-1 * b + Math.Sqrt(D)) / (2 * a);
result += 5. ; // The real roots case
}
}
6. Read the next line

7
by Mashigo VM ©2024
INF164 EXAM PREP 2024

}
MessageBox.Show(result);

Expected Output:

8
by Mashigo VM ©2024
INF164 EXAM PREP 2024

Question 2 [4 marks]
Study the following diagram and complete the code that will use the save dialog to save
content from the ritchtextbox (rtxSolutions) to a file.

private void btnSave_Click(object sender, EventArgs e)


{
//Create an instance of a save file dialog
//Filter the save file dialog for .txt files or rtf files
//open the save file dialog
//save the contents of the ritchtextbox to the file selected on the
Save file dialog
}

9
by Mashigo VM ©2024
INF164 EXAM PREP 2024

Question 3 [6 marks]
Study the following code and complete it such that it produces the code that will transfer
elements from a 2D array to an open file dialog (Note the array can have as many
rows/columns as possible, don’t hardcode).

double[,] array = { {10.1, 20.2, 30.3 }


,{40.4, 50.5, 60.6 },
{ 70.7, 80.8, 90.9} };

OpenFileDialog ofd = new OpenFileDialog();


//show the file dialog

//create an instance of stream writer object that will open the file
selected on the open file dialog

//Code that will transfer the array elements to the file (array elements
must must be space seperated
writer.Close();

Expected output

10
by Mashigo VM ©2024
INF164 EXAM PREP 2024

Question 4 [5 marks]

Study the form on the right and the code


below and answer the questions that follow

string[,] stock = {
{"apple", "banana", "kiwi" },
{"tomato", "potato", "onion" },
{"bread", "buns", "cookies"},
{"snacks", "biscuits", "sweets" }};
int rowIndex = Convert.ToInt32(txtRowIndex.Text);
int columnIndex = Convert.ToInt32(txtColumnIndex.Text);

var item = stock[rowIndex, columnIndex];


MessageBox.Show($"Item at stock[{rowIndex},{columnIndex}] is {item}");

Question 4.1 [2 marks]


What could go wrong with the above code (Think of exception handling).

Question 4.2 [3 marks]


Write the necessary code that will handle issues mentioned in 4.1.

11
by Mashigo VM ©2024
INF164 EXAM PREP 2024

2. Multi dimensional array

Question 1 [ 15 marks]

Questions 1.1 [3 marks]


Write a c# code that will create the following jagged array.

10 20 30 40
50 60 70
80 90
15 35 40 45 55

Question 1.2 [2 marks]


Convert the code in 2.1 to a javascript code.

Question 1.3 [4 marks]


Study the following C# code, write the average method that will calculate the average of the
array.
double[,] array = { { 10, 20, 30},
{ 40, 50, 60},
{70, 80, 90 }};
double res = average(array);

Question 1.4 [6 marks]


Rewrite the code from 2.3 (both the given code and the method) using javascript, use a
foreach to loop through the elements.

12
by Mashigo VM ©2024
INF164 EXAM PREP 2024

3. DataGrid view and arrays related problems (without


binding lists)

Question 1 [31 marks]


Consider the following and answer the questions that follow.

Question 1.1 [4 Marks]


1.1 Complete the following code so that the DataGrid view will be loaded with the
contents of the array.

13
by Mashigo VM ©2024
INF164 EXAM PREP 2024

Question 1.2 [6 marks]


You decide that you want to use Javascript instead of C# to represent this information.
Convert the declaration of the array into a javascript 2d array. Also create a dynamically and
populate it with the contents of this table.

14
by Mashigo VM ©2024
INF164 EXAM PREP 2024

Question 1.3 [10 marks]


Now consider the following form

Question 1.3.1 [4 marks]


Write a code that will determine the total rain fall and display it on a message box.
Question 1.3.2 [3 marks]
Write a code that will calculate the average rain fall for Limpopo and display it on
a message box.
Question 1.3.3 [3 marks]
Write a code that will calculate the average rain fall for March and display it on a
message box.

Question 1.4 [5 marks]


Consider the following even handler, write a code that will determine the
average rainfall for a specific month, the month will be selected using a
combo box named cbxProvince. Complete the code.

15
by Mashigo VM ©2024
INF164 EXAM PREP 2024

Question 1.5 [6 marks]


Write a code that will highlight the highest rainfall for every province. Example
from figure 3, 98.11 will be highlighted for Mpumalanga, 98.19 For Eastern
Cape, 90 for Western Cape and so on.

16

You might also like