INF164 EXAM PREP (3)
INF164 EXAM PREP (3)
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
D = b2− 4ac
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
Expected output:
5
by Mashigo VM ©2024
INF164 EXAM PREP 2024
6
by Mashigo VM ©2024
INF164 EXAM PREP 2024
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]
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.
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).
//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]
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);
11
by Mashigo VM ©2024
INF164 EXAM PREP 2024
Question 1 [ 15 marks]
10 20 30 40
50 60 70
80 90
15 35 40 45 55
12
by Mashigo VM ©2024
INF164 EXAM PREP 2024
13
by Mashigo VM ©2024
INF164 EXAM PREP 2024
14
by Mashigo VM ©2024
INF164 EXAM PREP 2024
15
by Mashigo VM ©2024
INF164 EXAM PREP 2024
16