Anda di halaman 1dari 4

University of Tripoli / GS200 / Spring2017 / Date:5-7-2017

Electrical and Electronic Engineering Department Total: 40 Marks. Time :3 hours

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]

1- Which statement is true?


(A) boolean is a C++ keyword. (B)123 is a C++ valid identifier.
(C) C++ is a low-level language. (D) none.

2- Assume a = 3 and b = 6, which of the following is true?


(A) a > 3 && b > 12 (B) b * b > a + b && a / b > a % b
(C) b a > b % a (D) none.

3- Which of the following functions is not in the C++ math library?


(A) abs (B) sqrt (C) random (D) floor

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.

5- Which operator has the highest precedence?


(A) . (B) *= (C) ++ (D) %

6- In C++, which keyword that every typed function must have?


(A) const (B) static (C) void (D) none.

7- Which of the following appear in a function header?


(A) the function name, return type, and argument types.
(B) the function name, return type, argument types and argument names.
(C) the function name, return type, and argument names.
(D) the function name and the supplied arguments.

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;

b) int found = 0, count = 5;


if (!found ||--count == 0)
cout << "danger" << endl;
cout<<"++count ="<<( --count )<<endl;
c) int n;
cout<<( n = 4 )<<endl<<( (++n) == 4 )<<endl;
cout<<( n > 3 )<<endl<<( (n--) < 4 )<<endl;
cout<<( n = 0 )<<endl<<( n == 0 )<<endl;
cout<<( n > 0 )<<endl<<( n && 4 )<<endl;
cout<<( n || 4 )<<endl<<( !n )<<endl;

d) int square(int);

int main(){
for ( int x = 1; x < 5; x++ )
cout<< square( x ) <<" ";}

int square(int y){


if ( (y%2) == 0 ) return 0;
else return y*y;}

e) char ch;
char title[] = "LiBYa";
ch = title[1];
title[3] = ch;
cout << title << endl;
cout << ch << endl;

f) int A =1, B =2;


if( ( (A==1) || (B==2) ) && (B==0) )
cout<<"This exam was difficult ";
else cout<<"This exam wasnt easy ";
Q3) a) The program shown below has been written by an amateur C/C++ programmer. The
program should print a number in every loop iteration but it doesnt. what should you do to
correct his mistake?
int n = 10;
while (n > 0) n /= 2;
cout << n* n << endl; [1.5 mark]

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.

example: sum_from_to( 2, 3) the output should be 0.


sum_from_to( 2, 5) the output should be 7. [2.5 marks]

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]

a)13 b)2048 c)2730


In your words, what does this program do? [1 marks]

b) for the following program:


char text[80]; bool s=true;
int l=0, n=0, maxLength;
cin.getline( text, 80);
cin>>maxLength;
for(int i = 0; i < strlen( text); i++ ){
if( isalpha( text[i]) && ( l > 0) )l++;
if( isalpha( text[i]) && (s) ){
s = false;
l++;
}
if( ! isalpha( text[i]) && ( l > 0) ){
s = true;
cout << l << ' ';
if( l <= maxLength) n++;
l = 0;
}
}
if(( l <= maxLength)&&( isalpha(text[strlen(text)-1])))
n++;
cout<<endl<<n;

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;
}

long fun(long t){


if(t>=1) return fun(t-1) + 2*fun(t-2) + 3*fun(t-3) + 4;
else return 10;
}
What does the program display if the user inputs: [1.25 mark each]
a) 2 b) 3 c) 5
Q5) a) Fibonacci sequence (1,1,2,3,5,8,13,21) is a sequence where each number in it is the
sum of the two previous numbers for example (1+1=2, 1+2=3, 2+3=5, 5+8=13, 8+13=21 )
and so on. write a function to calculate the nth position of the series using the iterative
method [2 mark] and the recursive method [2.75 mark].
Example: Fibonacci(0) the output should be 1.
Fibonacci(2) the output should be 2.
Fibonacci(7) the output should be 21.
Q5 b) Write the shortest possible equivalent C++ program for the flow chart below.[4marks]

Anda mungkin juga menyukai