MONITORING AND EVALUATION 1
CAT 1
Question 1
I. How are binary files different from text files in C++ (2 marks)
Text files contain lines (or records) of text and each of these has an end-of-line marker automatically
appended to the end of it whenever you indicate that you have reached the end of a line. There is an
end of line at the end of the text written with the C fwrite() function or in C++ when you <<endl. Binary
end-of line marker is not written when files are not broken up into separate lines or records so the
writing to a binary file.
II. What’s the difference between get() and getline() (2 marks)
gets() works on character array. eg char string[LENGTH]. Syntax: gets(string) while getline() works on
string datatype. eg. string S. Sytax: getline(cin,S)
get() extracts char by char from a stream and returns its value (casted to an integer) whereas getline() is
used to get a line from a file line by line. Normally getline is used to filter out delimiters in applications
where you have a flat file(with thousands of line) and want to extract the output(line by line) using
then do some operation on it. certain delimiter and
III. Define
a. multiple inheritance (2 marks)
It refers to a feature of some object-oriented programming languages in which a class can inherit
behaviors and features from more than one superclass
MONITORING AND EVALUATION 2
b. Dynamic binding? (2 marks)
It refers to linking a procedure call to the code that will be executed only at run time. The code
, which is also known as late associated with the procedure in not known until the program is executed
binding.
c. DBMS (2 marks)
A database management system (DBMS) is a collection of programs that enables you to store, modify,
a database. and extract information from
IV. What is pointer arithmetic? And how is it performed ( 5 marks)
The idea behind pointer arithmetic is that, if you have an int pointer to 1000 and an int pointer to 2000,
. What you're asking is, how many int's and ask for the difference, you're not asking what's 2000-1000
can I fit between the two.
When you have a pointer such as int *p, and you write: *(p + 1) this is the same as writing p[ 1 ]. Writing
p + 1 performs pointer arithmetic, adding not a single byte to the address stored in p, but sizeof( int )
bytes! If you need to work with individual bytes, you can use a typecast: (char *)p + 1 will advance you
exactly a single byte. This can be useful when performing file IO.
When working with a pointer to a single value, most of the time you won't want to use pointer
arithmetic. When you are working with an array, you can use it instead of array notation.
The most valuable thing learnt from pointer arithmetic is that that pointers are just variables that
you can manipulate them like any other variables. happen hold memory addresses, and
MONITORING AND EVALUATION 3
V. What’s the difference between design patterns and frameworks (4 marks)
Design Patterns Frameworks
A design pattern solves many software
architecture issues (about creation, behavior,
different pre-defined and concurrency) with
design. (design being an implementation of
an architecture topic)
A framework is based on the Hollywood
Principle ("Don't call us, we call you"),
where you implement some high level
requirements as specified, and leave the
framework do the rest of the work, calling
. your implementations
Design pattern have a tight scope:
class design patterns (involves
classes)
business design patterns (involves
business workflows)
patterns (involves application design
applications)
Framework has a large scope:
For instance, .NET is a framework
composed of:
a language (C#)
a runtime environment (CLR)
of libraries a collection
A pattern is a best practice, a recipe and a time A framework is a library that provides a
MONITORING AND EVALUATION 4
tested solution to a recurring problem that's
to be the best one among to proved to work and
choose.
foundation layer to use to build
without writing everything application
from scratch
Framework is a skelton of code for developing
particular type of application. For example,
house can be treated as framework because it
like kitchen, have predefined components
bathroom, etc. Further it can be used to use to
develop any type of house.
A design pattern is a solution for particular
problem. For example, design pattern for
reusability.
VI. What’s data manipulation language (1 marks)
A data manipulation language (DML) is a family of computer languages including commands permitting
users to manipulate data in a database. This manipulation involves inserting data into database tables,
data. DML is mostly retrieving existing data, deleting data from existing tables and modifying existing
incorporated in SQL databases.
VII. State the advantages of modelling (5 marks)
Improves the productivity of the development team (e.g. models can be used for a semi-
automatic code-generation)
MONITORING AND EVALUATION 5
Reduces the number of defects in the final code (models facilitate early evaluation of the
system)
Capture and organize the understanding of the system (which btw, eases the integration of new
team members). Models are well suited for documentation
Permit early exploration of alternatives
Increases the decomposition and modularization of the system
Facilitates the system’s evolution and maintenance (e.g. by facilitating tracing back the code to
the original requirements)
Facilitates the reuse of parts of the system in new projects
VIII. What is a stream? Name the streams generally used for file I/O. (5 marks)
The term stream is a general abstraction (concept) of any construct that allows you to simply send any
number of bytes, or to receive any number of bytes.
Streams are generally associated to a physical source or destination of characters, like a disk file, the
keyboard, or the console, so the characters gotten or written to/from our abstraction called stream are
device. For example, file streams are C++ objects to manipulate physically input/output to the physical
and interact with files; Once a file stream is used to open a file, any input or output operation performed
on that stream is physically reflected in the file.
Some of the commonly used stream classes include:
MONITORING AND EVALUATION 6
FileStream – for reading and writing to a file.
IsolatedStorageFileStream – for reading and writing to a file in isolated storage.
MemoryStream – for reading and writing to memory as the backing store.
BufferedStream – for improving performance of read and write operations.
NetworkStream – for reading and writing over network sockets.
PipeStream – for reading and writing over anonymous and named pipes.
CryptoStream – for linking data streams to cryptographic transformations.
CAT 2
Question 1
I. Write a function named "enough" that takes one integer argument, call it "goal" and returns
as its value the smallest positive integer n for which 1+2+3+. . . +n is at least equal to goal . (20
marks)
MONITORING AND EVALUATION 7
Thus, for example,
cout << enough(9) << endl; // will print 4 because 1+2+3+4 9 but 1+2+3<9
DESCRIPTION: Computes and returns the smallest positive integer n for which 1+2+3+...+n equals or
exceeds the value of "goal".
PARAMETER:
Goal The integer which 1+2+3+...+n is required to meet or exceed.
RETURNS: Returns the smallest positive integer n for which 1+2+3+...+n
equals or exceeds "goal". For example,
if 9, then the function will return 4 because goal has the value
1+2+3+4 >= 9 but 1+2+3 < 9. If goal has a
value less than or equal to 1, then the function will
return the value 1.
ALGORITHM: First the value n = 1 is tried to see if 1 >= goal. If that is not true,
then successively larger values of n are added to a summing variable
until that sum reaches or exceeds goal.
int enough (int goal)
{
int n = 1, sum = 1;
MONITORING AND EVALUATION 8
while (sum < goal)
sum += ++n;
return n;
}
Question 2
I. Explain the following with examples
a. Class hierarchy (5 marks)
A set of classes and their interrelationships. One class may be a specialisation (a " subclass " or " derived
class ") of another which is one of its " superclasses " or "base classes". When a method is invoked on an
object it is first looked for in the object's class, then the superclass of that class, and so on up the
hierarchy until it is found. Thus a class need only define those methods which are specific to it, and
superclasses. inherits methods from all its
Examples
class Bicycle {
}
class MountainBike extends Bicycle {
}
or
MONITORING AND EVALUATION 9
class Person {
}
Person { class Employee extends
}
b. Object relationships and associations (5 marks)
Object: a visible or tangible thing of relative stableform; A thing that may be apprehended intellectually.
It is a thing to which thought or action is directed. Relationships are prevalent in system models but
implementation languages do not provide first-class support for them. For example, in Java (and other
must be implemented by hand using references embedded in Object-Oriented Languages), relationships
participants.
They represent logical links between two or more entities or objects. Residence is an example of a
relationship that can exist between the entities City and Employee; Exam is an example of a relationship
that can exist between the entities Student and Course. An instance of a relationship is an n-tuple made
each of the entities involved. up of instances of entities, one for
An association declares that there can be links between instances of the associated types. A link
is a tuple with one value for each end of the association, where each value is an instance of the
type of the end. Put more simply, when any two classes are connected in any way we say they
"Student" studies at a "University". are 'associated'. For example, a
MONITORING AND EVALUATION 10
An association is graphically rendered by a solid line connecting the same or different classes on
a UML class diagram. As with everything else on a class diagram, an association is a statement
about instances of those classes. An occurrence of an association is known as a link.
the rules about what links can exist between instances of Associations between classes represent
those classes.
Associations should usually be named to avoid possible ambiguity. This is especially important
exists between the same two classes. where more than one association
Example, Person works-for Company and Person is-customer-of Company. The association
name is accompanied by a small arrow-head to assist in reading the association correctly (i.e.
"Person works-for Company", not "Company works-for Person"). This arrow-head does NOT
of the association say anything about the navigability
Assignment 1 1. Write a program that converts integers to roman numbers and displays numbers from 1-100
My code is as follows:
#include <stdio.h>
int main( void )
MONITORING AND EVALUATION 11
{
int loop;
int div; /* tens digit */
int mod; /* ones digit */
/* display table headers */
printf( " Roman\nNumeral\t\t\t\tDecimal\n" );
/* loop 100 times */
for ( loop = 1; loop <= 100; ++loop ) {
div = loop / 10; // pulls out tens digit
mod = loop % 10; // pulls out ones digit
/* switch structure for tens digit */
switch ( div ) {
/* print appropriate Roman numeral for tens digit */
case 0:
break;
case 1:
printf( "X" );
break;
case 2:
printf( "XX" );
break;
case 3:
MONITORING AND EVALUATION 12
printf( "XXX" );
break;
case 4:
printf( "XL" );
break;
case 5:
printf( "L" );
break;
case 6:
printf( "LX" );
break;
case 7:
printf( "LXX" );
break;
case 8:
printf( "LXXX" );
break;
case 9:
printf( "XC" );
break;
case 10:
printf( "C" );
MONITORING AND EVALUATION 13
break;
default:
break;
}
/* switch structure for ones digit */
switch( mod ) {
/* print appropriate Roman numeral for ones digit */
case 0:
printf( "\t\t\t\tM\n", loop );
break;
case 1:
printf( "I\t\t\t\tM\n", loop );
break;
case 2:
printf( "II\t\t\t\tM\n", loop );
break;
case 3:
printf( "III\t\t\t\tM\n", loop );
break;
case 4:
printf( "IV\t\t\t\tM\n", loop );
break;
MONITORING AND EVALUATION 14
case 5:
printf( "V\t\t\t\tM\n", loop );
break;
case 6:
printf( "VI\t\t\t\tM\n", loop );
break;
case 7:
printf( "VII\t\t\t\tM\n", loop );
break;
case 8:
printf( "VIII\t\t\t\tM\n", loop );
break;
case 9:
printf( "IX\t\t\t\tM\n", loop );
break;
}
}
return 0;
}
Assignment 2 Write a program that shows Input/Output with files
Use the following classes to perform output and input of characters to/from files:
ofstream: Stream class to write on files
ifstream: Stream class to read from files
fstream: Stream class to both read and write from/to files
MONITORING AND EVALUATION 15
The following example demonstrates the typical use of ofstream:
#include <iostream>
#include <fstream>
using namespace std;
int main () {
ofstream output_file;
output_file.open( "filename.txt" );
output_file << "Hello";
(); output_file.close
return 0;
}
The above sample creates a file named "filename.txt" and inserts a word "Hello" into it. This code is
similar to the way we did output with cout. The output_file represents the file stream object.
// writing on a text file
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ofstream myfile( "example.txt" );
if ( myfile.is_open() ) {
MONITORING AND EVALUATION 16
myfile << "This is a line.\n";
myfile << "This is another line.\n";
myfile.close();
} else {
cout << "Unable to open file";
}
return 0;
}
// reading a text file
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main ()
{
string line;
ifstream myfile( "example.txt" );
if (myfile.is_open()) {
Top Related