You are on page 1of 5

NATIONAL UNIVERSITY OF LESOTHO

Department of Mathematics and Computer Science
CS 2431-Programming Fundamentals
Test 01 - Open Book
28th September 2005

Marks: 100

Time: 2 hours

Instructions:
(1)
(2)
(3)

Answer any five (5) questions. Each question carries 20 marks.
Marks are shown at the end of each (sub)question.
This paper consists of 5 printed pages

Question 1:
a) Define the following terms
i) A computer program
ii) A compiler
iii) An interpreter
.................................................................................................................................. [2+2+2]
b) Write a C++ program or a function that uses a FOR loop to find a product of integer
numbers between any two integers,
(
) with a specified incremental
step, k.
........................................................................................................................................ [14]
Question 2:
a) Identify all syntax errors in the following program
#include <iostream >
int m ain()
{
cout << ‘CS2431 Test 01';
}

.......................................................................................................................................... [9]
b) The computation

, is the arithmetic

, for the integers y

and n.
Write a C++ program or function that uses a WHILE loop to compute
.
........................................................................................................................................ [11]
Question 3:
a) Consider the following program segment
int sum = 0;
for(int k = 1; k <= n; k++)
{
cout << “k = “ << k << “ sum = “ << sum << endl;
sum += k;
}

Write down the output of this loop for n = 4.
CS 2431 - Programming Fundamentals

28th Sep 2005

Test 01

Page -1/5-

............................................. [8] b) A factorial of a natural number n is defined as: Using a DO ..................................................................... Assume that age (type int) and gender (type char) would be used as appropriate variable names................. otherwise it issues Policy B............................ [14] CS 2431 .... [12] Question 4: a) An insurance company issues policies depending on the age and gender of its clients... i < = 3.......... The classification is as follows: Mean Mark Classification 65 or above Pass with Credit 50 – 64 Pass below 50 Fail Using an IF statement construct........................................... The classification is based upon the performance mean mark of the student.......... graduates are classified as “Pass with Credit”................................ ..............e....................................................................................... [2+2+2] b) In a college........ Write a C++ IF statement that could be used for issuing these insurance company policies... write a program or a function that accepts a mean mark (type int) from the user and then outputs the appropriate classification message............................................................................. any arithmetic expression evaluating to a non-zero value is regarded as representing the truth value: true............................... Use the C++ cout statement to print out the appropriate message to issue the corresponding policy............. “Pass” or “Fail”.......................................Programming Fundamentals 28th Sep 2005 Test 01 Page -2/5- .............. j++) cout << i << “ ” << j << “ ” << i * j. the company issues Policy A (i......... the most attractive package)...... .......................................................................................... WHILE loop write a program or a function computing n! .. ............ If the client is female and below 40 years of age..................................................................... [10] Question 5: a) Say whether true (T) or false (F) in the following i) A C++ variable can be an underscore character ii) In C++..... j < = i................ iii) The two Boolean constants: true or false are integer expressions...... [10] b) Write down the output for the following nested FOR loop for( int i = 1................. ........................................................... i++) for( int j = 1.

The program then displays the classification of the student based on the calculated OWM and the given classification rules.. The program then allows the user to enter course information for the next student or quit the program........................................ Second Division 50 – 59 Pass 0 – 49 Fail Write a C++ program or function that can classify students at NUL as follows: 1....... 3......... except the least prime......Programming Fundamentals 28th Sep 2005 Test 01 Page -3/5- ... The program then outputs a message whether N is prime or that N is not prime HINT: 1.......................... [20] Question 7: At NUL students are assessed and classified based upon an individual’s overall weighted mean (OWM) which is computed as: The classification is as follows: OWM Classification 75 or above First Class 70 – 74 Second Class..................... [20] CS 2431 ..... All primes are odd.............. 2....... ...... meaning N mod p = 0.. .. 2.......................... N is not prime iff such that p is a co-factor of N.. 3... The program accepts a number............... ............................................ until the user has finished all the courses for this particular student......................... N.... 2..... First Division 60 – 69 Second Class......Question 6: Write a C++ program or function that can determine if the given number is prime or not as follows: 1.... from the user 2... Every non-prime number N can be written as a product of its prime factors only.. The program allows the user to enter each course mark (type int) and its corresponding credit hours (type int) a pair at the time........

CS 2431 .Programming Fundamentals 28th Sep 2005 Test 01 Page -4/5- .

Programming Fundamentals 28th Sep 2005 Test 01 Page -5/5- .CS 2431 .