Beruflich Dokumente
Kultur Dokumente
Q1) for the following programs write down what will be displayed on the screen if they were
executed:
1) char a[80]="hello", b[80]="yes", c[80]="day";
cout<<strncpy(a,b,1)<<endl<<strcat(a,c)<<endl;
cout<<strcmp(a,b)<<" "<<strlen(a);
______________________________________________________________
______________________________________________________________
__________________________________________________[3.75 marks]
1 of 7 Pages
4) int i=5;
do {
int j=0;
do {
j++;
(i%2) ? cout<<'<':cout<<'>';
} while(j!=i);
cout<<endl;
i--;
} while(i>0);
______________________________________________________________
______________________________________________________________
______________________________________________________________
______________________________________________________________
_________________________________________________ [3.75 marks]
Q2) a) Rewrite the following program without using the command for( ).
_________________________________
for(int i=1;i<100;i++){ _________________________________
x=i*i; _________________________________
cout<<i<<^2<<x<<endl; _________________________________
} _____________________[3.75 marks]
2 of 7 Pages
______________________________________________________________
______________________________________________________________
______________________________________________________________
______________________________________________________________
______________________________________________________________
__________________________________________________[3.75 marks]
3 of 7 Pages
Q3) a) for the following program:
int num, i;
bool P = true;
cout << "Enter a positive integer: ";
cin >> num;
for(i = 2; i <= num / 2; ++i){
if(num % i == 0){
P = false;
break;
}
}
if (P) cout << "This is a **** number";
else cout << "This is not a **** number";
what does the screen display if the user inputs:
1) 6 _________________________________________________________
2) 7 _________________________________________________________
3) 1 _________________________________________________________
[1 mark each]
In your words, what does this program do?
______________________________________________________________
______________________________________________________________
_____________________________________________________[2 marks]
b) for the following program:
int param1, param2;
cout<< "enter the first number" << endl;
cin>> param1;
cout<< "enter the second number" << endl;
cin>> param2;
int i=0, ans=0;
while (param1||param2) {
ans+=(((param1%10) *(param2%10)) %10) *pow (10, i);
param2/=10;
param1/=10;
i++;
}
cout<<ans;
4 of 7 Pages
In your words, what does this program do?
______________________________________________________________
______________________________________________________________
_____________________________________________________[2 marks]
c) for the following program:
char A [80], B [80];
cin.getline(A,80);
cin.getline(B,80);
int r=0, x=0, y=0;
while (1) {
for (int i=0; i<strlen(A); i++) {
for (int j=0; j<strlen(B); j++) {
if(A[i]==B[j]) {
for (int k=j; k<strlen(B); k++)
B[k]=B[k+1];
x=1;
break;
}
}
if(x==1) {
x=0;
continue;
}
else {
y=1;
break;
}
}
if(y==0) r++;
else break;
}
cout<< r;
what does the program display if the user inputs:
1) abcabcabcabc ____________________________________________
2) ahmedmohamed__________________________________________
3) noyes__________________________________________________
[1 mark each]
In your words, what does this program do?
______________________________________________________________
______________________________________________________________
____________________________________________________ [2 marks]
5 of 7 Pages
Q4) a) write a program that takes a string then finds the number of number digits, letters and
spaces in that string. as shown in the example.
Example:
______________________________________________________________
______________________________________________________________
______________________________________________________________
______________________________________________________________
______________________________________________________________
______________________________________________________________
______________________________________________________________
______________________________________________________________
______________________________________________________________
______________________________________________________________
______________________________________________________________
______________________________________________________________
______________________________________________________________
______________________________________________________________
______________________________________________________________
______________________________________________________________
______________________________________________________________
______________________________________________________________
______________________________________________________________
______________________________________________________________
______________________________________________________________
______________________________________________________________
______________________________________________________________
___________________________________________________[7.5 marks]
6 of 7 Pages
b) write a program that takes an integer then finds the length and the reverse of that integer. as
shown in the example.
Example:
______________________________________________________________
______________________________________________________________
______________________________________________________________
______________________________________________________________
______________________________________________________________
______________________________________________________________
______________________________________________________________
______________________________________________________________
______________________________________________________________
______________________________________________________________
______________________________________________________________
______________________________________________________________
______________________________________________________________
______________________________________________________________
______________________________________________________________
______________________________________________________________
______________________________________________________________
______________________________________________________________
______________________________________________________________
______________________________________________________________
______________________________________________________________
______________________________________________________________
______________________________________________________________
______________________________________________________________
______________________________________________________________
______________________________________________________________
___________________________________________________[7.5 marks]
7 of 7 Pages