Download as pdf or txt
Download as pdf or txt
You are on page 1of 17

MEMORANDUM

Programme Diploma in Information Technology


Module Programming 3B
Module Code PRG310
Module NQF Level 7
Credits 10
Test/Exam Special Exam Memo
Semester 2nd
Date Written 19 February 2020

Total marks 100


Duration 2 Hours
Pass mark 50%
Weighting 60%
Examiner Frans Rampai
Moderator Zukile Ndyalivana

This question paper consists of 16 pages including the cover page.

REQUIREMENTS:

Learner Requirements: Stationery and Examination Answer booklet


Equipment Requirements: None

This paper consists of 5 questions:

ALL questions are COMPULSORY. It is in your own interest to write legibly and to present your work
neatly.

PLEASE READ THE ASSESSMENT RULES AND REGULATIONS THAT FOLLOW

Learners are warned that contravening any of the examination rules or disobeying the instructions of an
invigilator could result in the examination being declared invalid. Disciplinary measures will be taken
which may result in the students’ expulsion from Damelin.

Programming 3B Exam Page 1 of 17


Damelin©
ASSESSMENT RULES AND REGULATIONS

Please ensure that you have read and fully understand the following assessment rules and regulations
prior to commencing with your assessment:
1. To be permitted access to the examination, a learner must arrive with:
- an Identity Document or other official proof of identity (for example,
- a student card, passport or driver's licence card with photo); and
- the required exam stationery.
2. No learner may enter the examination room more than 30 minutes after the examination sitting has
commenced and no candidate may leave the room less than one hour after the examination sitting
has commenced.
3. No extra time will be allowed should a student arrive late.
4. All learners must sign the Attendance Register for the examination on arrival.
5. It is the responsibility of learners to familiarise themselves with the examination rules prior to sitting
for the examination.
6. All examinations are to be written on the date and time officially stipulated by the College.
7. It is the responsibility of learners to ensure that they are writing the correct paper and that the
question paper is complete
8. Cell phones must be switched off prior to entering the exam venue. Cell phones and wallets may
be placed under candidates' chairs rather than at the front of the room.
9. Learners may not handle cell phones or wallets during the exam.
10. No weapon of any description may be taken into the assessment room.
11. All personal belongings are to be placed at the front of the examination room. Personal belongings
brought to the examination are at the owner's risk.
12. Smoking is not permitted, and learners will not be allowed to leave the examination room in order
to smoke
13. Once the examination has commenced, all conversation of any form between candidates must
cease until after candidates have left the room, after the examination.
14. Only the official College examination book, as supplied by the College, may be used.
15. Learners must ensure that their student number is written on the answer book.
16. Learners are responsible for ensuring that they follow the instructions in the examination for
submitting their answers.
17. Please read the instruction appearing on the examination paper carefully
18. The number of every question must be clearly indicated at the top of every answer.
19. No pages may be torn out of the answer book. All question papers and scrap paper must be
handed to the invigilator after the examination.
20. Learners finishing earlier are to leave the examination room as quietly as possible on the
instruction of the invigilator and may not talk until outside the building where the examination is
being written.
21. Only under exceptional circumstances will a learner be permitted to leave the examination room
during the examination, and if the invigilator gives permission. An invigilator must accompany the
learner. Only one learner at a time may be absent from the examination room.
22. Candidates may not act dishonestly in any respect.

Programming 3B Exam Page 2 of 17


Damelin©
QUESTION 1 (20 Marks)

Outcomes:

Use ref parameters, out parameters, and parameter arrays

• Create objects
• Create properties, including auto-implemented properties

• Write and use constructors

• Create and use abstract classes

• Understand the Object class

• Describe exceptions, the Exception class, and generating SystemExceptions

1.1 Explain the difference between ref parameters and out parameters (4)
ANS:
ref parameters do not contain the values associated with the parameters, but
they contain the memory addresses where the values are stored. This allows
the method to alter the original variables. √√

Out parameters, like ref parameters, contain memory addresses that are
passed to a method, allowing them to alter the original variables. The big
difference between ref and out parameters is that ref parameters need to
contain a value before calling the method, while out parameters do not. √√
1.2 Discuss object, property, constructor and default constructor (4)
ANS:
Object - is a variable that is used to access the private and public members of its class, its
data type is its class name. √
Property - is a class field used to set and get values of class’ private members√
Constructor – is a method with the same name as its class used for initialisation of class’
private members. √
Default constructor – one that initialise numeric private members to 0 and string private
members to empty string. It exist even when is not visibly declared. √
1.3 Discuss ancestor, derived class, virtual method, and concrete class (4)
ANS:
Ancestor – is a group of parent classes from which a certain class is derived from. √
Derived class- is a child class, one built by inheriting from parent class√
Virtual method - is one whose behaviour is determined by the implementation
in a child class. √
Concrete class - is a non-abstract class from which objects can be
instantiated. √
1.4 Discuss exception and robustness. (4)

Programming 3B Exam Page 3 of 17


Damelin©
ANS:
• An exception is any error condition or unexpected behavior in an
executing program. √
• An Exception is an object delivered by the Exception class. This
Exception class is exposed by the System.Exception namespace.
Exceptions are used to avoid system failure in an unexpected
manner. Exception handles the failure situation that may arise. All the
exceptions in the .NET Framework are derived from the
System.Exception class. √
•Robustness - represents the degree to which a system is resilient to
stress, maintaining correct functioning even in the presence of errors.

• Exception handling is the set of object-oriented techniques used to
manage unexpected errors in an executing program. √
• We use Try….Catch block to ensure exception don’t crush our
programs.
1.5 Discuss dock property and anchor property of a control (4)
ANS:
The Anchor property causes a Control to remain at a fixed distance from the
side of a container when the user resizes it. √√

The Dock property attaches a Control to the side of a container so that the
Control stretches when the container’s size is adjusted. √√

QUESTION 2 (10 Marks)

Outcomes:

• Learn about parameter types and review mandatory value parameters


• Use ref parameters, out parameters, and parameter arrays
• Overload methods
• Learn how to avoid ambiguous methods
• Use optional parameters

2. Correct the code below so that it will produce expected output specified below. (10
1 using System; )
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CSharpSTudyUnit1
{
class Program
{
static void Main(string[] args)
{

int x = 3, y = 4;
Console.WriteLine("Arguments before calling the Display() "

Programming 3B Exam Page 4 of 17


Damelin©
+" method with out parameters \n: " + x + " and " + y);

Console.WriteLine("Arguments After calling the Display method


with out parameters\n a="+ x + " and b=" + y);
Console.ReadLine();

Console.WriteLine(Ov1(2));
Console.WriteLine(Ov1(2,2,2));
Console.WriteLine(Ov1(2,2,"Hello"));
Console.ReadKey();
}
//overloaded method
public static int Ov1(int a)
{ return a; }
public static int Ov1(int a,int b, String x)
{Console.WriteLine(x);
return a + b;
}
public static int Ov1(int a, int b, int c)
{ return a + b+c; }

//how to use params arrays


public static void ParamsTesting(params int[] scores)
{
Console.WriteLine("Params allows passing unknown number of
arguments to a method"
+"\nTesting params by entering unlimited arguments \n"
+"Enter argument and hit enter:");
for (int i=0;i<=scores.Length-1;i++)
{
scores[i] += Convert.ToInt32(Console.ReadLine());
}
for(int j = 0; j < scores.Length; j++)
{
Console.Write(scores[j] + " ");
}
Console.ReadLine();
}

//how to use out parameters


public static void Display(out int a, out int b)
{
Console.WriteLine("Enter the value of a");
a = Convert.ToInt32(Console.ReadLine());

Console.WriteLine("Enter the value of b");


b = Convert.ToInt32(Console.ReadLine());
}

}
}

Hint: focus only within the main method.


Expected output:

Programming 3B Exam Page 5 of 17


Damelin©
ANS:
Note: Markers are advised to use own discretion for allocating ticks since code may differ.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CSharpSTudyUnit1
{
class Program
{
static void Main(string[] args)
{

Programming 3B Exam Page 6 of 17


Damelin©
int x = 3, y = 4;
Console.WriteLine("Arguments before calling the Display() "
+" method with out parameters \n: " + x + " and " + y);
Display(out x, out y);
Console.WriteLine("Arguments After calling the Display method
with out parameters\n a="+ x + " and b=" + y);
Console.ReadLine();

int[] xy= new int[10];


ParamsTesting(xy);
Console.WriteLine(Ov1(2));
Console.WriteLine(Ov1(2,2,2));
Console.WriteLine(Ov1(2,2,"Hello"));
Console.ReadKey();
}
//overloaded method
public static int Ov1(int a)
{ return a; }
public static int Ov1(int a,int b, String x)
{Console.WriteLine(x);
return a + b;
}
public static int Ov1(int a, int b, int c)
{ return a + b+c; }

//how to use params arrays


public static void ParamsTesting(params int[] scores)
{
Console.WriteLine("Params allows passing unknown number of
arguments to a method"
+"\nTesting params by entering unlimited arguments \n"
+"Enter argument and hit enter:");
for (int i=0;i<=scores.Length-1;i++)
{
scores[i] += Convert.ToInt32(Console.ReadLine());
}
for(int j = 0; j < scores.Length; j++)
{
Console.Write(scores[j] + " ");
}
Console.ReadLine();
}

//how to use out parameters


public static void Display(out int a, out int b)
{
Console.WriteLine("Enter the value of a");
a = Convert.ToInt32(Console.ReadLine());

Console.WriteLine("Enter the value of b");


b = Convert.ToInt32(Console.ReadLine());
}

}
}

Programming 3B Exam Page 7 of 17


Damelin©
QUESTION 3 (20 Marks)

Outcomes:

• Create objects
• Create properties, including auto-implemented properties

A property is a member of a class that provides access to a field of a class. It defines how
fields will be set and retrieved. Properties have accessors, set accessors for setting an
object’s fields called setter, get accessors for retrieving the stored values called getter.
Read-only property has only a get accessor. Write only property has a set accessor.
For read/write there must be both get accessor and set assessor.

3.1 Create a class named Pizza. Data fields include a string for toppings (such as pepperoni), an (20)
integer for diameter in inches (such as 12), and a double for price (such as 13.99). Include
properties to get and set values for each of these fields. Create a class named TestPizza.cs
Expected typical output:

ANS:

Note: Markers are advised to use own discretion for allocating ticks since code may
differ.

Programming 3B Exam Page 8 of 17


Damelin©
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PissaStore
{
class Program
{
static void Main(string[] args)
{
//creation of pissa class object given a name p
Pissa p = new Pissa();//instatiation of Pissa class
Console.WriteLine("welcome to Pizza Store:");
Console.WriteLine("Enter pizza diameter:");
p.Diameter = Convert.ToInt32(Console.ReadLine()) ;

Console.WriteLine("Enter pizza topping:");


p.Topping = Console.ReadLine();

Console.WriteLine("Enter pizza price:");


p.Price = Convert.ToDouble(Console.ReadLine());
Console.WriteLine();
Console.WriteLine("\tYour pizza order:");
Console.Write("Size:"+ p.Diameter*2 +"\nTopping:"+ p.Topping
+"\nPrice:"+ p.Price.ToString("c"));
Console.ReadLine();
}
}
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PissaStore
{
class Pissa
{
private string topping;
private int diameter;

Programming 3B Exam Page 9 of 17


Damelin©
private double price;

public string Topping


{
get{ return topping; }
set { topping = value; }
}
public int Diameter
{
get { return diameter; }
set { diameter = value; }
}
public double Price
{
get { return price; }
set { price = value; }
}
}
}

QUESTION 4 (20 Marks)

Outcomes:

• Extend classes

• Override base class members

• Create and use abstract classes

• Create and use interfaces

4.1 Write a program in which you declare classes namely Animal.cs and Lion.cs. Declare (20)
abstract method called Speak() within Animal class.
Implement the Speak() method to return string “roooaaar”, do this inside Lion class.
Create an interface called Ilion.cs which declares method Run(), implement the Run
method to return string “I am a Lion and I can’t run fast!”.
Display the resutls of Run() and Speak() method inside Program.cs.
Program.cs class to show output. Name your program Inheritance.
Note that Program.cs is auto-created.
Expected output:

Programming 3B Exam Page 10 of 17


Damelin©
ANS:
Note: Markers are advised to use own discretion for allocating ticks since code may
differ.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Inheritance
{
class Program
{
static void Main(string[] args)
{
Lion l = new Lion();
Console.WriteLine(l.Run());
Console.WriteLine(l.Speak());
Console.ReadLine();
}
}
}

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Inheritance
{
abstract class Animal
{
public abstract String Speak();
}
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Inheritance
{
class Lion:Animal,ILion
{
public override string Speak()
{
return "roooaaar";
}
public string Run()
{
return "I am a Lion and I can’t run fast!";
}
}
}

Programming 3B Exam Page 11 of 17


Damelin©
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Inheritance
{
public interface ILion
{
string Run();
}
}

QUESTION 5 (30 Marks)


Outcomes:
• Compare traditional and object-oriented error-handling methods
• Use the finally block

• Set a Control’s Font


• Create a Form that contains LinkLabels
• Use Controls
• Write to and read from a sequential access text file
• Search a sequential access text file

An exception is an unexpected or error condition. The programs you write can generate many
types of potential exceptions, such as when you do the following:
• You issue a command to read a file from a disk, but the file does not exist there.
• You attempt to write data to a disk, but the disk is full or unformatted.
• Your program asks for user input, but the user enters invalid data.
• The program attempts to divide a value by 0.
• The program tries to access an array with a subscript that is too large or too small.

5.1 Write a program in which you declare an integer array named “arr” of five integers and store (10)
values: 2,4,5,6,7.
Write a try block in which you place a loop that attempts to access each element of the array,
incrementing a subscript from 0 to 10. Create a catch block that catches the eventual
INdexOutOfRangeException; within which the block, display “Now you’ve gone too far” on the
screen. Your program should display “Exception Handling” regardless of exception been
caught or not. Name the file as GoTooFar.cs

Programming 3B Exam Page 12 of 17


Damelin©
Expected output:

Note: Markers are advised to use own discretion for allocating ticks since code may
differ.

ANS:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GoTooFar
{
class Program
{
static void Main(string[] args)
{
int[] arr = { 2,4,5,6,7};
int index = 0;
try

Programming 3B Exam Page 13 of 17


Damelin©
{
for (int i = 0; i < 10; i++)
{
Console.WriteLine("Hello the array has five
values\n"
+ "Enter array index to see the value:");
index = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("On position:" + index + " the
value is " + arr[index]);
Console.ReadLine();
}
}
catch (IndexOutOfRangeException e)
{
Console.WriteLine("Now you've gone too far");
Console.ReadLine();
}
finally {
Console.WriteLine("Exception Handling");
Console.ReadLine();
}
}
}
}

5.2 In the Visual Studio IDE, design a form that allows a user to select options for the background (20)
color and size and to give to the form a title. The form should look like the one shown below.
After the user clicks the “Save Settings” button, save the color, width, height and title to a file
“CustomiseAForm.txt” which you are to create and save in your application root folder. When
you click the button “Apply Settings” the settings should be applied accordingly to your form,
use below screen shorts as test case and guideline.

Programming 3B Exam Page 14 of 17


Damelin©
ANS:

Note: Markers are advised to use own discretion for allocating ticks since code may
differ.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace CustomiseAForm
{
public partial class Form1 : Form
{
int width,height;
string path,color;

private void btnApplySettings_Click(object sender, EventArgs e)


{
try
{
path = "CustomiseAForm.txt";

Programming 3B Exam Page 15 of 17


Damelin©
string[] filerecords = File.ReadAllLines(path);

if (filerecords[0] == "Blue") this.BackColor=Color.Blue;


if (filerecords[0] == "Red") this.BackColor = Color.Red;
if (filerecords[0] == "Yellow") this.BackColor =
Color.Yellow;
width =Convert.ToInt32(filerecords[1]);
height = Convert.ToInt32(filerecords[2]);
this.Size = new Size(width,height);
this.Text = filerecords[3];

} catch (Exception ex) {

MessageBox.Show(ex.Message);

}
}

public Form1()
{
InitializeComponent();
}

private void btnSaveSettings_Click(object sender, EventArgs e)


{
try {
if (rbBlue.Checked == true) color = "Blue";
if (rbRed.Checked == true) color = "Red";
if (rbYellow.Checked == true) color = "Yellow";

if (chkWidth.Checked == true) width = 450;


if (chkHeight.Checked == true) height = 400;

path = "CustomiseAForm.txt";
if (File.Exists(path))
{
StreamWriter sw = new StreamWriter(path);
if (rbBlue.Checked == true) sw.WriteLine(color);
if (rbRed.Checked == true) sw.WriteLine(color);
if (rbYellow.Checked == true) sw.WriteLine(color);

sw.WriteLine(width);
sw.WriteLine(height);

sw.WriteLine(txtTitle.Text);
sw.Dispose();
MessageBox.Show("settings written successfully!");
}
else { MessageBox.Show("Error 404! File not found!");}
} catch(Exception ex) {
MessageBox.Show(ex.Message);

}
}
}

∞End of Question Paper∞

Programming 3B Exam Page 16 of 17


Damelin©
Programming 3B Exam Page 17 of 17
Damelin©

You might also like