Final-term Exam
Q1) a) Write the algorithm then the C++ program to print a list of prime number between 0
and 9999. [2 marks]
b) Choose the correct answer of the following multiple-choice questions. [0.5 mark each]
4- Which of these array definitions will set all the indexed variables to 0?
(A) int array[5]; (B) int array[5] ={}; (C) int array[5] ={0} (D) none.
Q2) What is the exact output of the programs below? Indicate a blank space in the output by
writing the symbol . Indicate a blank line in the output by writing blank line.
[1.5 mark each]
a) int i = 5, j = 6, k = 7, n = 3;
cout << i + j * k- k % n << endl;
cout << i /n << endl;
d) int square(int);
int main(){
for ( int x = 1; x < 5; x++ )
cout<< square( x ) <<" ";}
e) char ch;
char title[] = "LiBYa";
ch = title[1];
title[3] = ch;
cout << title << endl;
cout << ch << endl;
b) Write a function named "sum_from_to" that takes two integer arguments, call them "first"
and "last", and returns as its value the sum of all the integers between first and last inclusive.
c) Write a function named "enough" that takes one integer argument, call it goal and
returns the value the smallest positive integer n for which the summation 1+2+3+. . . +n value
is at least equal to the goal.
example: enough( 9) the output should be 4. because 1+2+3+4>=9 but 1+2+3<9.
enough( 21) the output should be 6. because 1+2+..+6>=21.
enough( -7) the output should be 1. because 1 is the smallest positive integer.
[2.5 marks]
Q4) a) for the following program:
int factors[20], divisor = 2, n, i = 0;
cout<<"please enter a number"<<endl;
cin>>n;
while (n >= 2) {
if (n % divisor == 0) {
factors[i]=divisor;
i++;
cout<<setw(5)<<n<<" | "<<divisor<<endl;
n /= divisor ;
}
else {
divisor++;
}
}
cout<< setw(9) << 1 <<" | ";
what does the screen display if the user inputs: [0.75 mark each]
What does the program display if the user inputs: [0.75 mark each]
a) My name is Mohamed4
b) My name is Mohamed!4
c) The weather was hot last week, but this week it is fine ^_^ 3
In your words, what does this program do? [1 marks]
c) for the following program:
main(){
long a;
cout<<"please enter an integer"<<endl;
cin>>a;
long h=fun(a);
cout<<h;
}