# 2008 exam

Posted by ShaunGVW
Announcements Last Post
SoC Curricula 09/30/2017 01:08PM
Demarcation or scoping of examinations and assessment 02/13/2017 07:59AM
School of Computing Short Learning Programmes 11/24/2014 08:37AM
Unisa contact information 07/28/2011 01:28PM
 2008 exam November 13, 2009 05:27AM Registered: 13 years ago Posts: 523 Rating: 0
Anyone willing to answer any of last year's exam questions?
If anyone keen/interested, I can start, and then if people add to it, I can keep editing this original post to keep answers as top post.
Note that I am not accountable for any answers given here, use at own risk, but hopefully with many-user input, errors should be a minimum!
IF YOU NOTICE MISTAKES/ERRORS etc LET ME KNOW!!!!

Question 1 - 16 marks
1.1.1
Write a separate class Count, which has a data member counter, a slot
incrementCounter() and a function getCounter() to return the value of the
counter. Include both header and cpp files. (5)

Language: C++ (QT)
#ifndef COUNT_H #define COUNT_H   #include <QObject>   class Count : public QObject { Q_OBJECT public: Count(); int getCounter(); public slots: void incrementCounter(); private: int counter; };   #endif // COUNT_H

[cpp file]
Language: C++ (QT)
Count::Count() { counter = 0; }   int Count::getCounter() { return counter; }   void Count::incrementCounter() { ++counter; }

1.1.2
Rewrite the class PrimeCount to utilise the new Count class, in such a way
that the class emits a signal foundPrime()when a prime number is found.
Include both header and cpp files. (4)

Language: C++ (QT)
#ifndef PRIMECOUNT_H #define PRIMECOUNT_H   #include <QObject> #include "count.h"   class PrimeCount : public QObject { Q_OBJECT public : PrimeCount(int sNumber, int eNumber); void primeCheck(); signals: void foundPrime(); protected: int sNumber, eNumber; }; #endif   #include "primecount.h"   PrimeCount::PrimeCount(int sNum, int eNum) { sNumber = sNum; eNumber = eNum; }   void PrimeCount::primeCheck() { for(int num = sNumber; num<= eNumber; num++) { int flag =0; for(int i = 2; i< (int)num/2; i++) { if (num%i == 0) { flag = 1; break; } } if(flag == 0) emit foundPrime(); } }

1.1.3
Make changes in the main() function to instantiate the new PrimeCount and
Count classes, to connect the foundPrime() signal to the getCounter() slot
and to output the number of prime numbers found between 10 and 25. (4)

Language: C++ (QT)
#include <QApplication> #include <QDebug> #include <QObject> #include "count.h" #include "primecount.h"   int main(int argc, char **argv) { QApplication app(argc, argv); Count cntr; PrimeCount pc(10,25); app.connect(&pc, SIGNAL(foundPrime()), &cntr, SLOT(incrementCounter())); pc.primeCheck(); int i = cntr.getCounter(); qDebug()<< i; return 0; }

1.2
What is the primary use of signals and slots?
As a means of communications between objects (1)

1.3
One advantage using signals/slots in relation with PrimeCount, Count classes in Q1.1 (2)
Bypasses having to use inheritance

Question 2 - 9 marks
2.1
Identify the Qt widgets (classes) to achieve the above facilities.
QLineEdit
QLabel
QPushButton

2.2
Draw a schematic diagram of how the widgets in Question 2.1 will be arranged using the
QHBoxLayout class?

In a horizontal line such as
Start No. [LineEdit1] End No. [LineEdit2] [PushButton] Result :
2.3
State one of the controller classes in Qt with respect to GUI applications.
QApplication & QAction

2.4
Write a regular expression to ensure that the user enters an int of minimum 1 digit and
a maximum of 3 digits for the start (or end) number (2)

QRegExp rx ("^\\d {1,3}\$" ); //I have experienced without the \$ it still allows other characters after the 3rd digit based on
if (rx.indexIn(str) == 0)
result->setText("True" );

Question 3 - 11 marks
3.1
Implement the changes required (both the header and cpp files) in the PrimeCount

Language: C++ (QT)
#ifndef PRIMECOUNT_H #define PRIMECOUNT_H   #include <QObject> #include <QThread> #include "count.h"   class PrimeCount : public QThread { Q_OBJECT public : PrimeCount(int sNumber, int eNumber); void primeCheck(); signals: void foundPrime(); protected: void run(); int sNumber, eNumber; }; #endif     #include "primecount.h"   PrimeCount::PrimeCount(int sNum, int eNum) { sNumber = sNum; eNumber = eNum; }   void PrimeCount::run() { for(int num = sNumber; num<= eNumber; num++) { int flag =0; for(int i = 2; i< (int)num/2; i++) { if (num%i == 0) { flag = 1; break; } } if(flag == 0) emit foundPrime(); } }

3.2
Implement the changes required in the main() function to have two PrimeCount
threads (one to check prime numbers from 10 to 25 and the second one to check prime
numbers from 30 to 50), one Count instance, and to execute the threads and display
the value of counter. (4)

Language: C++ (QT)
#include <QApplication> #include <QDebug> #include <QObject> #include <QThread> #include "count.h" #include "primecount.h"   int main(int argc, char **argv) { QApplication app(argc, argv); Count cntr; PrimeCount pc1(10,25); PrimeCount pc2(30,50); app.connect(&pc1, SIGNAL(foundPrime()), &cntr, SLOT(incrementCounter())); app.connect(&pc2, SIGNAL(foundPrime()), &cntr, SLOT(incrementCounter())); pc1.start(); int i = cntr.getCounter(); qDebug()<< i; pc2.start(); i = cntr.getCounter(); qDebug()<< i; return 0; }

3.3
What problem might occur when incrementing the counter as stated in Question 3.2. (2)
Both threads access the same counter object, with effect that it counts first the first thread's no. of primes, then it adds on the second thread's no of primes to this first value, getting (in this case) a total of 10 counts, but actually there are only 5 counts between 30 & 50.
NOTE : Something not correct, because it doesn't return 5 then 10, it returns 0, then 0 again

Question 4 - 8 marks
4.1
Identify and motivate a design pattern that can be used to model the file/directory
structure, where directories can contain subdirectories as well as files

Visitor Pattern ...

Visitor is a Behavioral Design Pattern, represents operations to be performed on the elements of a structure

Composite is a Structural Pattern, directories can contain subdirectories(composite), file(leaf)

Function getSize():
In Component class virtual int getSize()
extend getSize() in composite and leaf classes

4.2
Draw an Unified Modeling Language (UML) diagram with appropriate classes and key
functions of the design pattern identified in Question 4.1. Also indicate a function
getSize(), which returns the size of the file or directory in the class diagram

(Something along the lines of...)

Question 5 - 12 marks
5.1.1
Write the necessary program lines to use an appropriate Qt container to handle
dvd1, dvd2, dvd3, dvd4 and dvd5 (given below) as an organised collection.
DVD * dvd1 = new Entertainment("111", "Three Colours: Blue",
145.50, "Drama","PG10" );
DVD * dvd2 = new Entertainment("112", "Three Colours: Red",
145.20,"Drama", "PG10" );
DVD * dvd3 = new Educational("113", "History of South Africa",
100.00, "History", "G10" );
DVD * dvd4 = new Educational("114", "Landscapes of Africa",
200.00, "Geography", "G5" );
DVD * dvd5 = new Entertainment("115", "The Bucket List", 180.00,
"Comedy","PG15" ); (3)

QList <DVD *> DVDList;
DVDList << dvd1 << dvd2 << dvd3 << dvd4 << dvd5;

5.1.2
Implement the operation totalPrice() on the organised collection (answered in
Question 5.1.1) to get the total cost of all the DVDs in the collection. (3)

5.2
The QMetaObject instance generated by the moc compiler can be used to access
certain properties of a class hierarchy. Implement the necessary function to return the
class name in the hierarchy using the generated QMetaObject instance. For example,
the class names for dvd1 and dvd3 are, Entertainment and Educational
respectively.

Language: C++ (QT)
static QString DVD::ClassName(QObject * obj) { const QMetaObject * meta = obj->metaObject(); return meta->className(); }

5.3
Change the totalPrice() function in Question 5.1.2 to utilise the facility
implemented in Question 5.2 in such a way that DVDs of type Entertainment get a
discount of 10%.

Language: C++ (QT)
QList<DVD*> tempList = findChildren<DVD*>(); foreach( DVD item, tempList) { if (ClassName(item) == "Entertainment" { totalPrice = totalPrice + item->getPrice() *0.9; } else totalPrice = totalPrice + item->getPrice(); } return totalPrice

Question 6 - 9 marks
6.1
Discuss two differences between SAX and DOM XML processing in Qt. (4)
DOM
1. Requires full document to be loaded into memory - not large documents
2. Defines the creation of the tree of objects in memory
SAX
1. As processing document, it can write or dispose of processed data while parsing -> large documents
2. Performs the parsing

Question 7 - 11 marks
7.1
What is achieved by implementing the Singleton design pattern? (1)
Ensures only one instance of the application is running

7.2
Write the necessary code (in both header and cpp files) to apply the Singleton design
pattern in the class below: (8)

#ifndef SINGLE_H
#define SINGLE_H
#include <QObject>
class Single: public QObject
{
Q_OBJECT
public:
static Single *instance();
Single();
~Single();
protected:
Single(); { }
QString transaction;
QString authCode;
};
#endif

//cpp file
#include "Single.h"
Single *Single::instance() {
static Singleton *s_instance = 0;
if( s_instance == 0 ) {
s_instance = new Single();
s_instance->setParent(qApp);
}

return s_instance;
}

Single::Single() {
authCode = "User";
}

Single::~Single(){
}

7.3
Does a user/client of a Singleton class know that it is dealing with a Singleton?

Yes, the user/client knows that it is dealing with a Singleton because the constructor is private and the object cannot be created by merely calling the constructor. Rather the user/client has to call a function that returns a pointer to the already instantiated Singleton object.

Question 8 - 11 marks
Discuss the facilities which ease heap memory management in the following aspects of Qt framework.
â€¢ QObject class (4)
â€¢ A Qt container which contains pointers (4)
â€¢ auto_ptr (3)

Objects that inherit QObject will deleted all its children when it's deleted or itself when it's parent is deleted.
Qt containers implement implicit sharing, or "copy on write", this is an optimization that makes it possible to pass entire containers as values without any significant performance cost.
auto_ptr is used to ensure that the object is always destroyed when the wrapper is, and is a template type, so it can be instantiated for use with any other type. Also referred to as smart pointers.
All of these features reduce memory constraints, performance issues and memory leaks.

Question 9 - 5 marks
9.1
Discuss advantages of Qtâ€™s model-view programming architecture.

9.2
State the roles of the QModelIndex and QListView classes in the model-view
architecture.

The QModelIndex class is used to locate data in a data model. This class is used as an index into item models derived from QAbstractItemModel. The index is used by item views, delegates, and selection models to locate an item in the model.

The QListView class provides a list or icon view onto a model. A QListView presents items stored in a model, either as a simple non-hierarchical list, or as a collection of icons. This class is used to provide lists and icon views that were previously provided by the QListBox and QIconView classes, but using the more flexible approach provided by Qt's model/view architecture.

Question 10 - 8 marks
10.1
An application is being designed in which the error display, based on the user
preference, is one of the following options :
â€¢ It is displayed on the command prompt as the application is executing.
â€¢ It is written to a file to create an error log.
Identify and motivate a design pattern that can be used to model both of the above
error displays. (2)

Serialiser Pattern - Here the output stream is either the command prompt, or a log file. This pattern will handle the dynamics of writing to either, with all the calling class needing to decide on is the output option.
Strategy Pattern??? - The user is given a choice of options, and based on the choice, the program implements a different output.

10.2
Draw an UML diagram with appropriate classes and key functions of the
design pattern identified in Question 10.1. (6)

See post from Rey
 Re: 2008 exam November 13, 2009 07:55AM Registered: 14 years ago Posts: 3,015 Rating: 5
I answered it "life" last year - didn't do a good job at it, that's why I'm here again ...
 Re: 2008 exam November 13, 2009 08:38AM Registered: 10 years ago Posts: 37 Rating: 0
Even though I don't have time to help with this - two exams to go before I get to C++ - I think that Shaun has a good idea. Since there is no memorandum, why not make one?
 Re: 2008 exam November 13, 2009 09:35AM Registered: 13 years ago Posts: 523 Rating: 0
Tonight when I get home I will try updating it more.
 Re: 2008 exam November 13, 2009 10:15AM Registered: 11 years ago Posts: 63 Rating: 0
It's a good idea Shaun

I plan on going through the paper this w/e. Will add my 2 cents later...

(Suggestion: please include the question "summary" for example Question 1 - Rewrite PrimeCount, Question 2 - GUI for Prime Program, Question 3 - Threads... etc. )
 Re: 2008 exam November 13, 2009 01:38PM Registered: 14 years ago Posts: 3,015 Rating: 5
I quickly read through the exam paper - total blank - can definately not remember anything about it.

I'll also work through it and give my feedback.
 Re: 2008 exam November 13, 2009 07:34PM Registered: 13 years ago Posts: 523 Rating: 0
I have often wondered when they say it was the previous year's exam, whether it really was?
 Re: 2008 exam November 13, 2009 08:47PM Registered: 14 years ago Posts: 3,015 Rating: 5
The type of questions are definitely correct - I recognise the format of the exam. But, don't worry, when I walk out of an exam, I normally erase all memory related to the subject and exam. Else I'll get an overload of all the knowledge.
 Re: 2008 exam November 16, 2009 12:51PM Registered: 13 years ago Posts: 41 Rating: 0
@Reanie, lol I do exactly the same, only problem is that information starts 'leaking' out before I even get into the exam.

I really support this idea.
I will try to add as well so as not to be a parasite, however my very poor knowledge at the moment means that I am currently very much like a sponge. My analogy includes the brainless anatomy of the sponge as well. So I am fearful of spouting garbage.

Many thanks to Shawn and all contributors
 Re: 2008 exam November 17, 2009 02:09PM Registered: 14 years ago Posts: 163 Rating: 0
-----
Question 8
Objects that inherit QObject will deleted all its children when it's deleted or itself when it's parent is deleted.
Qt containers implement implicit sharing, or "copy on write", this is an optimization that makes it possible to pass entire containers as values without any significant performance cost.
auto_ptr is used to ensure that the object is always destroyed when the wrapper is, and is a template type, so it can be instantiated for use with any other type. Also referred to as smart pointers.
All of these features reduce memory constraints, performance issues and memory leaks.
-----

This can probably be expanded but it's what I have at the moment.

----
"Flying is learning how to throw yourself at the ground and miss." - Douglas Adams
"Time is a great teacher, but unfortunately it kills all its pupils ..." - Louis Hector Berlioz
I think animal testing is a terrible idea; they get all nervous and give the wrong answers.
 Re: 2008 exam November 17, 2009 02:15PM Registered: 14 years ago Posts: 163 Rating: 0
----
Question 9.2
The QModelIndex class is used to locate data in a data model. This class is used as an index into item models derived from QAbstractItemModel. The index is used by item views, delegates, and selection models to locate an item in the model.

The QListView class provides a list or icon view onto a model. A QListView presents items stored in a model, either as a simple non-hierarchical list, or as a collection of icons. This class is used to provide lists and icon views that were previously provided by the QListBox and QIconView classes, but using the more flexible approach provided by Qt's model/view architecture.
--

As per Qt Class Referrence.

----
"Flying is learning how to throw yourself at the ground and miss." - Douglas Adams
"Time is a great teacher, but unfortunately it kills all its pupils ..." - Louis Hector Berlioz
I think animal testing is a terrible idea; they get all nervous and give the wrong answers.
 Re: 2008 exam November 17, 2009 02:19PM Registered: 14 years ago Posts: 163 Rating: 0
-- EDIT 2009/11/18--- Look bellow I agree with Duncan and Brendon
10.2

From the book for the serializer

But wouldn't it be a factory method for the choice from the user?

Strategy Pattern and Factory pattern. PLEASE READ THE ASSIGNMENT 2 QUESTION 5.5

----
"Flying is learning how to throw yourself at the ground and miss." - Douglas Adams
"Time is a great teacher, but unfortunately it kills all its pupils ..." - Louis Hector Berlioz
I think animal testing is a terrible idea; they get all nervous and give the wrong answers.
 Re: 2008 exam November 17, 2009 09:15PM Registered: 10 years ago Posts: 7 Rating: 0
Question 10
I think the key to the question is:
based on the user preference

Then it would be a Strategy Pattern
A Strategy defines a set of algorithms that can be used interchangeably
 Re: 2008 exam November 17, 2009 10:26PM Registered: 10 years ago Posts: 7 Rating: 0
Question 4
model the file/directory structure

Visitor is a Behavioral Design Pattern, represents operations to be performed on the elements of a structure

Composite is a Structural Pattern, directories can contain subdirectories(composite), file(leaf)

Function getSize():
In Component class virtual int getSize()
extend getSize() in composite and leaf classes

Question 5
Not to sure here

QList <DVD *> DVDList;
DVDList << dvd1 << dvd2 << dvd3 << dvd4 << dvd5;

seems right and i started to answer it like the above but what about the derived classes Entertainment and Educational
Defining the base class DVD then the other 2 classes with their constructors calling on the derived constructor
seems like a lot of work for 3 marks

5.1.2

QList<DVD*> tempList = findChildren<DVD*>()
foreach( DVD item, tempList)
totalPrice = totalPrice + item->getPrice()

5.2
Not to sure here need some input from others
static QString DVD::ClassName(QObject * obj)
{
const QMetaObject * meta = obj->metaObject();
return meta->className();
}

5.3
QList<DVD*> tempList = findChildren<DVD*>();
foreach( DVD item, tempList)
{
if (ClassName(item) == "Entertainment"
{
totalPrice = totalPrice + item->getPrice() *0.9;
}
else
totalPrice = totalPrice + item->getPrice();
}
 Re: 2008 exam November 17, 2009 10:38PM Registered: 13 years ago Posts: 41 Rating: 0
.
 Re: 2008 exam November 17, 2009 10:39PM Registered: 13 years ago Posts: 41 Rating: 0
Question 10.

This question is identical to the first part of Ass2 Q5, which uses the Strategy Pattern. Tut 202 gives the solution on page 22, along with a UML. The class diagrams can be derived from the defined classes.
 Anonymous User Re: 2008 exam November 19, 2009 04:20PM Rating: 0
What is the difference between a visitor pattern and a composite pattern?
Is the only difference that visitor is a specialized composite pattern?
 Re: 2008 exam November 19, 2009 04:51PM Registered: 14 years ago Posts: 163 Rating: 0
Visitor pattern (Behavioural) is used when an operation is to be performed on selected objects in some organized collection/hierarchy. It maintains separation between visiting code and processing code. Implementation through inheritance or Signals and Slots

Composite Pattern (Creational Structural) is used to facilitate the building of complex (composite) objects from simpler (component) parts, by representing the part-whole hierarchies as a tree-like structure. This must be done in such a way that clients do not need to distinguish between simple and more complex parts that comprise the structure.

Composite pattern is something that the Visitor would work on. i.e. traverse the tree like structure performing operations on it's nodes.

----
"Flying is learning how to throw yourself at the ground and miss." - Douglas Adams
"Time is a great teacher, but unfortunately it kills all its pupils ..." - Louis Hector Berlioz
I think animal testing is a terrible idea; they get all nervous and give the wrong answers.
 Re: 2008 exam November 19, 2009 05:12PM Registered: 13 years ago Posts: 41 Rating: 0
I have Composite Pattern down as Structural. - Lets clients treat individual objects and compositions of objects uniformly. e.g. Directories contain entries, some of which might also be other directories.

see http://www.vincehuston.org/dp/
 Re: 2008 exam November 19, 2009 05:18PM Registered: 14 years ago Posts: 163 Rating: 0
Sorry you right..Was reading notes on factory method corrected as such.

----
"Flying is learning how to throw yourself at the ground and miss." - Douglas Adams
"Time is a great teacher, but unfortunately it kills all its pupils ..." - Louis Hector Berlioz
I think animal testing is a terrible idea; they get all nervous and give the wrong answers.
 Re: 2008 exam November 22, 2009 09:28PM Registered: 14 years ago Posts: 33 Rating: 0
Hello

this is a very good idea ShaunGVW thank you so much!
in fact you saved my day (and hopefully my COS311 year!)

but didn't you wipe out a couple of things in Q3.1?

Language: C++ (QT)
public slots: void start(); void stop();   private: bool PrimeCheck(int num);     -----------   void PrimeSearch::run() { for(int num = sNumber; num<= eNumber; num++) { if (PrimeCheck(num)) foundPrime(num); }   bool PrimeSearch::PrimeCheck(int num) { if ((num == 0) || (num == 1)) return false; if(num==2) return true; for (int i =2; i<=(int) num/2; i++) { if(num%i ==0) return false; } return true; } }   void PrimeSearch::start() { QThread::start(); }   void PrimeSearch::stop() { QThread::terminate(); }

or maybe I'm mistaken? especially that I'm quite sure I lost some of my brain cells after the show-them-no-mercy-aggressive-attack of COS321,COS311,COS351... etc...

---------
I'm Year '0110' Computer Student!
 Re: 2008 exam November 22, 2009 10:00PM Registered: 14 years ago Posts: 33 Rating: 0
5.1.2

(somebody correct me if I'm mistaken!)
Language: C++ (QT)
double DVD::totalPrice() { double totalPrice; QList <DVD*> childList = findChildren<DVD*>(); foreach (DVD* dvd, childList) { totalPrice += dvd->getPrice(); } return totalPrice; }
 Re: 2008 exam November 22, 2009 10:01PM Registered: 13 years ago Posts: 523 Rating: 0
I actually just transfered everything from primeCheck to run(). This is where the processing can take place, i.e. why let run() just call another function without any real benefit?
WRT start() and stop(), these functions have more to do with controlling the run() function, i.e. it should actually be checking to see what the boolean state of a flag is, and then either gracefully terminate, or continue processing. The state of this flag would be set in these start/stop functions, i.e. when a button is pressed. Then you would also include them as slots for the signal emitted when this button [say] is pressed, but that really is sprucing it up, and for the marks given, I would say what was given is enough (BUT, I stand corrected). I used the modified version (i.e. signal emitted) from a previous question, and made it multi-threaded.
 Re: 2008 exam November 23, 2009 12:33AM Registered: 14 years ago Posts: 33 Rating: 0
oh I see, how clever of you!

about the first point I just thought that this will make less typing in the exam because as you know we need every second there,

thank you for giving me time to explain, I hope you all the best!
Sorry, only registered users may post in this forum.