You are on page 1of 5

ANSWER SHEET OF C++ SENIOR 5 CSC

1. a) Character enclosed in single quotes is character constants in c++. Thus ‘a’ is a character
constant
Characters enclosed in double quotes are string-literals which are array of characters.
b) “=” is the assignment operator whereas “==” is the equality operator./4 marks
2. a) A float, because the height may contain a decimal fraction
b) A char, because (!) is an ASCII character.
c) An int, or more appropriately, an unsigned int, because there will never be a negative number
of student /6 marks
3. an array is a collection of similar data types or elements, addressed by a common variable name./
3 marks
4. -Execute of program starts and ends in main(),
- The main is the driver function of the program
5. An array of character can be initialized at the time of declaration. For example in the following
statement , a 6-element array named as name[6] is initialized by character type of data
char name [6]={‘s’,’h’,’a’,’s’,’h’,’I’}; / 3marks
6. A pointer is a memory variable that stores a memory address. Pointer can have any name that is
legal for other variable and it is declared in the same fashion like other variables but it is always
denoted by ‘*’ operator. / 4 marks
7. The answer is c : void /3marks
8. 51100011 /8 marks
9. Function overloading is a feature of C++ that allows us to create multiple functions with the
same name, so long as they have different parameters. But overloading of functions with different
return types is not allowed. The function call determines which function definition will be
executed. / 8 marks
Example:
#include<iostream.h>
int abslt(int );
float abslt(float );
int main()
{
int intgr=-5;
float flt=-5.56;
cout<<" absoulte value of "<<intgr<<" = "<<abslt(intgr)<<endl;
cout<<" absoulte value of "<<flt<<" = "<<abslt(flt)<<endl;

Page 1 of 5
}
int abslt(int num)
{
if(num>=0)
return num;
else
return (-num);
}
float abslt(float num)
{
if(num>=0)
return num;
else return (-num);
}
10. Pass by value means use a copy of the original value stored in memory - ie allocate some more
memory, copy the value in there and use that memory for the duration of the function. Thus, the
original variable isn't affected by the function.

Pass by reference means use the actual memory storing the value - so if the function changes the
value then the change is immediate and preserved when the function exits.  / 6 marks
11. This function return the smallest of three numbers 5,6,7
The answer is: the output is: 5 / 6 marks

SECTION B: Answer three questions of your choice /30 marks

12. #include<iostream.h>
main()
{
int n[10],sum=0;
for(int i=0;i<10;i++) / 10 marks
{
cin>>n[i];
sum=sum+n[i];
}
cout<<sum;
}
13. #include<iostream.h>
main()
{
int a,b,c;
cout<<" enter three numbers:\n";
cin>>a>>b>>c;
if(a>b)
{
if(a>c)

Page 2 of 5
{
cout<<"the max is:"<<a; / 10 marks
}else
{
cout<<"the max is:"<<c;
}
}else{
if(b>c)
{
cout<<"the max is:"<<b;
}else{
cout<<"the max is:"<<c;
}
}}
14. #include <iostream.h>
const int N=10;

int main()
{
int t[10],i,nb=0; / 10 marks

for(i=0;i<N;i++)
{
cout << "Type an integer: ";
cin >> t[i];
nb+= (t[i]>=10); // note that bool casts to int as 0 and 1
}

cout << "the number of integers greater or equal to 10 is: " << nb <<endl;

return 0;
}
15. #include <iostream.h>
const int N=10;
int main()
{
int t[N],i,j,V;
for(i=0;i<N;i++)
{
cout << "Type an integer: ";
cin >> t[i];
}
cout << "Type the value of V: "; / 10 marks
cin >> V;

for (i=0;i<N;i++)

Page 3 of 5
if (t[i]==V)
{
for (j=i;j<N-1;j++)
t[j]=t[j+1];
t[N-1]=0;
break;
}

for(i=0;i<N;i++)
cout << t[i] << endl;

return 0;
}
16. #include <iostream.h>
int factorial(int n);
int main()
{
int n;
cout << "Type an integer: ";
cin >> n;
cout<<"the factorial is:"<<factorial(n); / 10 marks
}
int factorial(int n)
{
int fact=1;
for(int i=1;i<=n;i++)
{
fact=fact*i;
}
return fact;
}

SECTION C: Answer only one question. / 15 marks

17. #include<iostream.h>
main()
{
int a,n,r,p=0;
cout<<"enter a number to test:";
cin>>n;
a=n;
while(n>0)
{
r=n%10;

Page 4 of 5
p=p*10+r;
n/=10;
}
if(p==a)
{
cout<<" the number is palindrome.";
}else
{
cout<<" the number is not palindrome.";
}
}

18. #include<iostream.h>
main()
{
int sumneg=0,sumpos=0,n[10];
for(int i=0;i<10;i++)
{
cin>>n[i];
if(n[i]>=0)
{
sumpos=sumpos+n[i];
}
else
{
sumneg=sumneg+n[i];
}
}
cout<<" the sum of positive numbers is :"<<sumpos<<endl;
cout<<" the sum of negative numbers is:"<<sumneg<<endl;
}

Page 5 of 5

You might also like