Saturday

FUNCTIONS IN C++

Functions
Using functions we can structure our programs in a more modular way, accessing all
the potential that structured programming can offer to us in C++.
A function is a group of statements that is executed when it is called from some
point of the program. The following is its format:
type name ( parameter1, parameter2, ...) { statements }
where:
 type is the data type specifier of the data returned by the function.
 name is the identifier by which it will be possible to call the function.
 parameters (as many as needed): Each parameter consists of a data type
specifier followed by an identifier, like any regular variable declaration (for
example: int x) and which acts within the function as a regular local variable.
They allow to pass arguments to the function when it is called. The different
parameters are separated by commas.
 statements is the function's body. It is a block of statements surrounded by
braces { }.
// function example The result is 8
#include
using namespace std;
int addition (int a, int b)
{
int r;
r=a+b;
return (r);
}
int main ()
{
int z;
z = addition (5,3);
cout << "The result is " << z;
return 0;
}
Write a function to determine whether the year is a leap year or not.
#include
main()
{
int leap_year(year);
int year, lp;

printf("Enter the year:");
scanf ("%d", &year);
lp=leap_year(year);
if (lp)
{
printf("\nThe entered year is a leap year.");
}
else
{
printf("\nThe entered year is not a leap year.");
}
}
leap_year(int y)
{
int lp;
if (y%4==0)
{
lp=1;
}
else
lp=0;
return(lp);
}
Write a general-purpose function to convert any given year into its roman
equivalent.
The following table shows the roman equivalents of decimal numbers:
Decimal:........Roman
1.....................i
5....................v
10..................x
50..................l
100................c
500...............d
1000.............m
Example:
Roman equivalent of 1988 is mdcccclxxxviii
Roman equivalent of 1525 is mdxxv
This program is a big lengthy owing to the use of Case Statements. This program can
also be rewritten using Arrays, which will reduce the length considerably.

#include
main()
{
int year;
int convert (int year);
{
printf("Note:Enter a four year digit year.\n\n");
printf("Enter the year that you wanna convert to Roman: " );
scanf ("%d", &year);
if (year> 1999)
{
printf("Invalid Year.Please enter again.\n\n");
}
}
convert(year);
}
convert(int year)
{
int i;
printf("\nYear converted to Roman:");
i=(year/1000); //thousands place
if(i==1)
{
printf("m");
}
i=((year/100)%10); //hundreds place
switch (i)
{
case 1:
printf("c");
break;
case 2:
printf("cc");
break;
case 3:

printf("ccc");
break;
case 4:
printf("cd");
break;
case 5:
printf("d");
break;
case 6:
printf("dc");
break;
case 7:
printf("dcc");
break;
case 8:
printf("dccc");
break;
case 9:
printf("dcccc"); //this part you may think is wrong..9 -> cm
break; //but i have taken a hint from the example in the question.
}
i=((year/10)%10); //tens place
switch(i)
{
case 1:
printf("x");
break;
case 2:
printf("xx");
break;
case 3:
printf("xxx");
break;
case 4:
printf("xl");
break;
case 5:
printf("l");
break;
case 6:
printf("lx");
break;
case 7:
printf("lxx");
break;
case 8:
printf("lxxx");
break;
case 9:
printf("lxxxx"); //had it not been for this example, it would have been xc
break;
}
i=year%10; //ones place
switch(i)
{
case 1:
printf("i");
break;
case 2:
printf("ii");
break;
case 3:
printf("iii");
break;
case 4:
printf("iv");
break;
case 5:
printf("v");
break;
case 6:
printf("vi");
break;
case 7:
printf("vii");
break;
case 8:
printf("viii");
break;
case 9:
printf("ix");
break;
}
printf ("\n\n");
return 0;
}
#include
int main(void)
{
int i = 1, j = 2;
void exchange(int, int);
printf("main : i = %d j = %d\n", i, j);
exchange(i,j);
printf("main : i = %d j = %d\n", i, j);
return 0;
}
void exchange(int i, int j)
{
int t;
t = i, i = j, j = t;
printf("exchange: i = %d j = %d\n", i, j);
}


#include
int main(void)
{
int i = 1, j = 2;
void exchange(int *, int *);
printf("main : i = %d j = %d\n", i, j);
exchange(&i,&j);
printf("main : i = %d j = %d\n", i, j);
return 0;
}
void exchange(int *ip, int *jp)
{
int t;
t = *ip, *ip = *jp, *jp = t;
printf("exchange: i = %d j = %d\n", *ip, *jp);
}
The following output is produced
main : i = 1 j = 2
exchange: i = 2 j = 1
main : i = 2 j = 1


Write a function power(a,b), to calculate the value of a raised to b.
#include
main()
{
int power (a,b);
int a, b, result;
printf("Enter the value of a and b:");
scanf ("%d %d", &a, &b);
result=power(a,b);
printf("%d raised to %d is %d", a, b, result);
}
power (int a, int b)
{
int calculation=1, calc;
for (calc=1; calc <=b; calc++)
{
calculation=calculation*a;
continue;
}

return(calculation);
}
A positive integer is entered through the keyboard.
Write a function to obtain the prime factors of this number.
For example, prime factors of 24 are 2, 2, 2 and 3, whereas prime factors of
35 are 5 and 7.
#include
main()
{
int number;
int prime(int number);
int primefactor(int number);
printf("Enter the number whose prime factors are to be calculated:");
scanf ("%d", &number);
primefactor(number);
}
//The following function detects a Prime number.
prime(int num)
{
int i, ifprime;
for (i=2; i<=num-1; i++)
{
if (num%i==0)
{
ifprime=0;
}
else
ifprime=1;
}
return (ifprime);
}

//The following function prints the prime factors of a number.
primefactor(int num)
{
int factor,ifprime;
for (factor=2; factor<=num;)
{
prime(factor); //so that the factors are only prime and nothing else.
if (ifprime)
{
if (num%factor==0) //diving by all the prime numbers less than the number itself.
{
printf("%d ", factor);
num=num/factor;
continue;
}
else
{
factor++;//this cannot be made a part of the for loop
}
}
}
return 0;
}
Write a function that calculates both Area and Perimeter/ Circumference of
the Circle, whose Radius is
entered through the keyboard.
#include
main()
{
int radius;
float area, perimeter;
printf("\nEnter radius of a circle:");
scanf ("%d", &radius);
areaperi (radius, &area, &perimeter);
printf("Area=%f", area);
printf("\nPerimeter=%f", perimeter);
}
areaperi(int r, float *a, float *p)
{

*a=3.14*r*r;
*p=2*3.14*r;
}
Write a function which receives a float and an int from main(), finds the
product
of these two and returns the product which is printed through main().
#include
main()
{
int i;
float j, prod;
float product (int x, float y);
printf("Enter the i(int) and j(float):");
scanf ("%d %f", &i, &j);
prod = product(i,j);
printf("Product:%f", prod);
}
product (int x, float y)
{
float product;
product = x*y;
return (product);
}
Write a function that receives 5 integers and returns the sum, average and
standard
deviation of these numbers. Call this function from main() and print the
results in main().
#include
#include
int calc (float a, float b, float c, float d, float e, float *sum, float *avg,float
*sd);
int main()
{
float a, b, c, d, e, sum=0.0, avg=0.0;
float sd=0.0;
printf("Enter Five Numbers:");

scanf("%f %f %f %f %f",&a,&b,&c,&d,&e);
calc (a, b, c, d, e, &sum, &avg, &sd);
printf("\nSum=%f", sum);
printf("\nAverage=%f", avg);
printf("\nStandard Deviation=%f\n", sd);
getchar();
return 0;
}
calc (float a, float b, float c, float d, float e, float *sum, float *avg, float *sd)
{
float Calc=0.0;
*sum = a+b+c+d+e;
*avg = *sum / 5.0;
Calc += ( a - *avg) * ( a - *avg);
Calc += ( b - *avg) * ( b - *avg);
Calc += ( c - *avg) * ( c - *avg);
Calc += ( d - *avg) * ( d - *avg);
Calc += ( e - *avg) * ( e - *avg);
*sd = sqrt((double)Calc/5.0);
}
Write a recursive function to obtain the first 25 numbers of a Fibonacci
sequence. In a Fibonacci sequence the sum of two successive terms gives
the
third term. Following are the first few terms of the Fibonacci sequence:
1 1 2 3 5 8 13 21 34 55 89 ...
#include
main()
{
static int prev_number=0, number=1; // static: so value is not lost
int fibonacci (int prev_number, int number);
printf ("Following are the first 25 Numbers of the Fibonacci Series:\n");

printf ("1 "); //to avoid complexity
fibonacci (prev_number,number);
}
fibonacci (int prev_number, int number)
{
static int i=1; //i is not 0, cuz 1 is already counted in main.
int fibo;
if (i==25)
{
printf ("\ndone"); //stop after 25 numbers
}
else
{
fibo=prev_number+number;
prev_number=number; //important steps
number=fibo;
printf ("\n%d", fibo);
i++; // increment counter
fibonacci (prev_number,number); //recursion
}
}
Write a program to calculate the factorial of a number. Use the concept of
recursion
instead of using loops.
#include
main()
{
int a, fact;
printf("\nEnter any number: ");
scanf ("%d", &a);
fact=rec (a);

printf("\nFactorial Value = %d", fact);
}
rec (int x)
{
int f;
if (x==1)
return (1);
else
f=x*rec(x-1);
return (f);
}
//1
main()
{
printf("\nOnly stupids use c?");
display();
}
display()
{
printf("\nFools too use C!");
main();
}
Answer-//1) infinite loop of both the statements
//2
main()
{
printf("\nC to it that C survives.");
main();
}
Answer- //2)The message " C to it that C survives" is iterated infinitesimally!
//3
main()
{
int i=45, c;

c=check(i);
printf("\n%d", c);
}
check (int ch)
{
if (ch>=45)
return (100);
else
return (10*10);
}
Answer-//3) 100
main()
{
int i=45, c;
c=check(i*1000);
printf("\n%d", c);
}
check (int ch)
{
if (ch>=40000)
return (ch/10);
else
return (10);
}
Answer--//4) 4500
We can see how the main function begins by declaring the variable z of type int.
Right after that, we see a call to a function called addition. Paying attention we will
be able to see the similarity between the structure of the call to the function and the
declaration of the function itself some code lines above:
The parameters and arguments have a clear correspondence. Within the main
function we called to addition passing two values: 5 and 3, that correspond to the int
a and int b parameters declared for function addition.
The following line of code:
return (r);

finalizes function addition, and returns the control back to the function that called it
in the first place (in this case, main). At this moment the program follows it regular
course from the same point at which it was interrupted by the call to addition. But
additionally, because the return statement in function addition specified a value: the
content of variable r (return (r);), which at that moment had a value of 8. This value
becomes the value of evaluating the function call.
So being the value returned by a function the value given to the function call itself
when it is evaluated, the variable z will be set to the value returned by addition (5,
3), that is 8. To explain it another way, you can imagine that the call to a function
(addition (5,3)) is literally replaced by the value it returns (8).
The following line of code in main is:
cout << "The result is " << z;
That, as you may already expect, produces the printing of the result on the screen.
Scope of variables
The scope of variables declared within a function or any other inner block is
only their own function or their own block and cannot be used outside of
them. For example, in the previous example it would have been impossible
to use the variables a, b or r directly in function main since they were
variables local to function addition. Also, it would have been impossible to
use the variable z directly within function addition, since this was a variable
local to the function main.
Therefore, the scope of local variables is limited to the same block level in
which they are declared. Nevertheless, we also have the possibility to declare

global variables; These are visible from any point of the code, inside and
outside all functions. In order to declare global variables you simply have to
declare the variable outside any function or block; that means, directly in the
body of the program.
And here is another example about functions:
// function example
#include
using namespace std;
int subtraction (int a, int b)
{
int r;
r=a-b;
return (r);
}
int main ()
{
int x=5, y=3, z;
z = subtraction (7,2);
cout << "The first result is " << z <<
'\n';
cout << "The second result is " <<
subtraction (7,2) << '\n';
cout << "The third result is " <<
subtraction (x,y) << '\n';
z= 4 + subtraction (x,y);
cout << "The fourth result is " << z <<
'\n';
return 0;
}
The first result is 5
The second result is 5
The third result is 2
The fourth result is 6
For example, the first case (that you should already know because it is the same
pattern that we have used in previous examples):
z = subtraction (7,2);
cout << "The first result is " << z;
If we replace the function call by the value it returns (i.e., 5), we would have:
z = 5;
cout << "The first result is " << z;
As well as
cout << "The second result is " << subtraction (7,2);

has the same result as the previous call, but in this case we made the call to
subtraction directly as an insertion parameter for cout. Simply consider that the
result is the same as if we had written:
cout << "The second result is " << 5;
since 5 is the value returned by subtraction (7,2).
In the case of:
cout << "The third result is " << subtraction (x,y);
The only new thing that we introduced is that the parameters of subtraction are
variables instead of constants. That is perfectly valid. In this case the values passed
to function subtraction are the values of x and y, that are 5 and 3 respectively,
giving 2 as result.
The fourth case is more of the same. Simply note that instead of:
z = 4 + subtraction (x,y);
we could have written:
z = subtraction (x,y) + 4;
with exactly the same result. I have switched places so you can see that the
semicolon sign (;) goes at the end of the whole statement. It does not necessarily
have to go right after the function call. The explanation might be once again that you
imagine that a function can be replaced by its returned value:
z = 4 + 2;
z = 2 + 4;
Functions with no type. The use of void.
If you remember the syntax of a function declaration:
type name ( argument1, argument2 ...) statement
you will see that the declaration begins with a type, that is the type of the function
itself (i.e., the type of the datum that will be returned by the function with the return
statement). But what if we want to return no value?
Imagine that we want to make a function just to show a message on the screen. We
do not need it to return any value. In this case we should use the void type specifier
for the function. This is a special specifier that indicates absence of type.
// void function example
#include
using namespace std;
I'm a function!

void printmessage ()
{
cout << "I'm a function!";
}
int main ()
{
printmessage ();
return 0;
}
void can also be used in the function's parameter list to explicitly specify that we
want the function to take no actual parameters when it is called. For example,
function printmessage could have been declared as:
void printmessage (void)
{
cout << "I'm a function!";
}
Although it is optional to specify void in the parameter list. In C++, a parameter list
can simply be left blank if we want a function with no parameters.
What you must always remember is that the format for calling a function includes
specifying its name and enclosing its parameters between parentheses. The nonexistence
of parameters does not exempt us from the obligation to write the
parentheses. For that reason the call to printmessage is:
printmessage ();
The parentheses clearly indicate that this is a call to a function and not the name of a
variable or some other C++ statement. The following call would have been incorrect:
printmessage;
Arguments passed by value and by reference.
Until now, in all the functions we have seen, the arguments passed to the functions
have been passed by value. This means that when calling a function with
parameters, what we have passed to the function were copies of their values but
never the variables themselves. For example, suppose that we called our first
function addition using the following code:
int x=5, y=3, z;
z = addition ( x , y );
What we did in this case was to call to function addition passing the values of x and
y, i.e. 5 and 3 respectively, but not the variables x and y themselves.

This way, when the function addition is called, the value of its local variables a and b
become 5 and 3 respectively, but any modification to either a or b within the function
addition will not have any effect in the values of x and y outside it, because variables
x and y were not themselves passed to the function, but only copies of their values
at the moment the function was called.
But there might be some cases where you need to manipulate from inside a function
the value of an external variable. For that purpose we can use arguments passed by
reference, as in the function duplicate of the following example:
// passing parameters by reference
#include
using namespace std;
void duplicate (int& a, int& b, int& c)
{
a*=2;
b*=2;
c*=2;
}
int main ()
{
int x=1, y=3, z=7;
duplicate (x, y, z);
cout << "x=" << x << ", y=" << y << ",
z=" << z;
return 0;
}
x=2, y=6, z=14
The first thing that should call your attention is that in the declaration of duplicate
the type of each parameter was followed by an ampersand sign (&). This ampersand
is what specifies that their corresponding arguments are to be passed by reference
instead of by value.
When a variable is passed by reference we are not passing a copy of its value, but
we are somehow passing the variable itself to the function and any modification that
we do to the local variables will have an effect in their counterpart variables passed
as arguments in the call to the function.
To explain it in another way, we associate a, b and c with the arguments passed on
the function call (x, y and z) and any change that we do on a within the function will
affect the value of x outside it. Any change that we do on b will affect y, and the

same with c and z.
That is why our program's output, that shows the values stored in x, y and z after
the call to duplicate, shows the values of all the three variables of main doubled.
If when declaring the following function:
void duplicate (int& a, int& b, int& c)
we had declared it this way:
void duplicate (int a, int b, int c)
i.e., without the ampersand signs (&), we would have not passed the variables by
reference, but a copy of their values instead, and therefore, the output on screen of
our program would have been the values of x, y and z without having been modified.
Passing by reference is also an effective way to allow a function to return more than
one value. For example, here is a function that returns the previous and next
numbers of the first parameter passed.
// more than one returning value
#include
using namespace std;
void prevnext (int x, int& prev, int& next)
{
prev = x-1;
next = x+1;
}
int main ()
{
int x=100, y, z;
prevnext (x, y, z);
cout << "Previous=" << y << ", Next="
<< z;
return 0;
}
Previous=99, Next=101
Default values in parameters.
When declaring a function we can specify a default value for each parameter. This
value will be used if the corresponding argument is left blank when calling to the
function. To do that, we simply have to use the assignment operator and a value for
the arguments in the function declaration. If a value for that parameter is not passed
when the function is called, the default value is used, but if a value is specified this
default value is ignored and the passed value is used instead. For example:
// default values in functions 6

#include
using namespace std;
int divide (int a, int b=2)
{
int r;
r=a/b;
return (r);
}
int main ()
{
cout << divide (12);
cout << endl;
cout << divide (20,4);
return 0;
}
5
As we can see in the body of the program there are two calls to function divide. In
the first one:
divide (12)
we have only specified one argument, but the function divide allows up to two. So
the function divide has assumed that the second parameter is 2 since that is what we
have specified to happen if this parameter was not passed (notice the function
declaration, which finishes with int b=2, not just int b). Therefore the result of this
function call is 6 (12/2).
In the second call:
divide (20,4)
there are two parameters, so the default value for b (int b=2) is ignored and b takes
the value passed as argument, that is 4, making the result returned equal to 5
(20/4).
Overloaded functions.
In C++ two different functions can have the same name if their parameter types or
number are different. That means that you can give the same name to more than
one function if they have either a different number of parameters or different types
in their parameters. For example:
// overloaded function
#include
using namespace std;
int operate (int a, int b)
{
return (a*b);
}
10
2.5

float operate (float a, float b)
{
return (a/b);
}
int main ()
{
int x=5,y=2;
float n=5.0,m=2.0;
cout << operate (x,y);
cout << "\n";
cout << operate (n,m);
cout << "\n";
return 0;
}
In this case we have defined two functions with the same name, operate, but one of
them accepts two parameters of type int and the other one accepts them of type
float. The compiler knows which one to call in each case by examining the types
passed as arguments when the function is called. If it is called with two ints as its
arguments it calls to the function that has two int parameters in its prototype and if
it is called with two floats it will call to the one which has two float parameters in its
prototype.
In the first call to operate the two arguments passed are of type int, therefore, the
function with the first prototype is called; This function returns the result of
multiplying both parameters. While the second call passes two arguments of type
float, so the function with the second prototype is called. This one has a different
behavior: it divides one parameter by the other. So the behavior of a call to operate
depends on the type of the arguments passed because the function has been
overloaded.
Notice that a function cannot be overloaded only by its return type. At least one of
its parameters must have a different type.
Arguments passed by value and by reference.
Until now, in all the functions we have seen, the arguments passed to the functions
have been passed by value. This means that when calling a function with
parameters, what we have passed to the function were copies of their values but
never the variables themselves. For example, suppose that we called our first
function addition using the following code:
int x=5, y=3, z;
z = addition ( x , y );
What we did in this case was to call to function addition passing the values of x and
y, i.e. 5 and 3 respectively, but not the variables x and y themselves.

This way, when the function addition is called, the value of its local variables a and b
become 5 and 3 respectively, but any modification to either a or b within the function
addition will not have any effect in the values of x and y outside it, because variables
x and y were not themselves passed to the function, but only copies of their values
at the moment the function was called.
But there might be some cases where you need to manipulate from inside a function
the value of an external variable. For that purpose we can use arguments passed by
reference, as in the function duplicate of the following example:
// passing parameters by reference
#include
using namespace std;
void duplicate (int& a, int& b, int& c)
{
a*=2;
b*=2;
c*=2;
}
int main ()
{
int x=1, y=3, z=7;
duplicate (x, y, z);
cout << "x=" << x << ", y=" << y << ",
z=" << z;
return 0;
}
x=2, y=6, z=14
The first thing that should call your attention is that in the declaration of duplicate
the type of each parameter was followed by an ampersand sign (&). This ampersand
is what specifies that their corresponding arguments are to be passed by
reference instead of by value.
When a variable is passed by reference we are not passing a copy of its value, but
we are somehow passing the variable itself to the function and any modification that
we do to the local variables will have an effect in their counterpart variables passed
as arguments in the call to the function.
To explain it in another way, we associate a, b and c with the arguments passed on
the function call (x, y and z) and any change that we do on a within the function will
affect the value of x outside it. Any change that we do on b will affect y, and the
same with c and z.

That is why our program's output, that shows the values stored in x, y and z after
the call to duplicate, shows the values of all the three variables of main doubled.
If when declaring the following function:
void duplicate (int& a, int& b, int& c)
we had declared it this way:
void duplicate (int a, int b, int c)
i.e., without the ampersand signs (&), we would have not passed the variables by
reference, but a copy of their values instead, and therefore, the output on screen of
our program would have been the values of x, y and z without having been modified.
Passing by reference is also an effective way to allow a function to return more than
one value. For example, here is a function that returns the previous and next
numbers of the first parameter passed.
// more than one returning value
#include
using namespace std;
void prevnext (int x, int& prev, int& next)
{
prev = x-1;
next = x+1;
}
int main ()
{
int x=100, y, z;
prevnext (x, y, z);
cout << "Previous=" << y << ", Next="
<< z;
return 0;
}
Previous=99, Next=101
Default values in parameters.
When declaring a function we can specify a default value for each parameter. This
value will be used if the corresponding argument is left blank when calling to the
function. To do that, we simply have to use the assignment operator and a value for
the arguments in the function declaration. If a value for that parameter is not passed
when the function is called, the default value is used, but if a value is specified this
default value is ignored and the passed value is used instead. For example:
// default values in functions
#include
using namespace std;
int divide (int a, int b=2)
6
5

{
int r;
r=a/b;
return (r);
}
int main ()
{
cout << divide (12);
cout << endl;
cout << divide (20,4);
return 0;
}
As we can see in the body of the program there are two calls to function divide. In
the first one:
divide (12)
we have only specified one argument, but the function divide allows up to two. So
the function divide has assumed that the second parameter is 2 since that is what we
have specified to happen if this parameter was not passed (notice the function
declaration, which finishes with int b=2, not just int b). Therefore the result of this
function call is 6 (12/2).
In the second call:
divide (20,4)
there are two parameters, so the default value for b (int b=2) is ignored and b takes
the value passed as argument, that is 4, making the result returned equal to 5
(20/4).
Overloaded functions.
In C++ two different functions can have the same name if their parameter types or
number are different. That means that you can give the same name to more than
one function if they have either a different number of parameters or different types
in their parameters. For example:
// overloaded function
#include
using namespace std;
int operate (int a, int b)
{
return (a*b);
}
float operate (float a, float b)
{
return (a/b);
}
10
2.5

int main ()
{
int x=5,y=2;
float n=5.0,m=2.0;
cout << operate (x,y);
cout << "\n";
cout << operate (n,m);
cout << "\n";
return 0;
}
Recursive Functions:-
Recursive is the property that functions have to be called themselves
For Example;- n!=n(n-1)*(n-2)*(n-3)……..*1
Functions Questions-
Find the output of the following program
class Sample
{
public:
int *ptr;
Sample(int i)
{
ptr = new int(i);
}
~Sample()
{
delete ptr;
}
void PrintVal()
{
cout << "The value is " << *ptr;
}
};
void SomeFunc(Sample x)
{
cout << "Say i am in someFunc " << endl;
}
int main()
{
Sample s1= 10;
SomeFunc(s1);
s1.PrintVal();
}
Answer:
Say i am in someFunc
What do you mean by inline function?

The idea behind inline functions is to insert the code of a called function at the point
where the function is called. If done carefully, this can improve the applications
Performance in exchange for increased compile time and possibly (but not always)
an increase in the size of the generated binary executables.
What do you mean by binding of data and functions?
Encapsulation.
What is abstraction?
Abstraction is of the process of hiding unwanted details from the user.
What is encapsulation?
Packaging an object’s variables within its methods is called encapsulation.
What is friend function?
As the name suggests, the function acts as a friend to a class. As a friend of a class,
it can access its private and protected members. A friend function is not a member of
the class. But it must be listed in the class definition.
What are virtual functions?
A virtual function allows derived classes to replace the implementation provided by
the base class. The compiler makes sure the replacement is always called whenever
the object in question is actually of the derived class, even if the object is accessed
by a base pointer rather than a derived pointer. This allows algorithms in the base
class to be replaced in the derived class, even if users don't know about the derived
class.
What does extern mean in a function declaration?
Using extern in a function declaration we can make a function such that it can used
outside the file in which it is defined.
An extern variable, function definition, or declaration also makes the described
variable or function usable by the succeeding part of the current source file. This
declaration does not replace the definition. The declaration is used to describe the
variable that is externally defined.
If a declaration for an identifier already exists at file scope, any extern declaration of
the same identifier found within a block refers to that same object. If no other
declaration for the identifier exists at file scope, the identifier has external linkage.
How do I declare an array of N pointers to functions returning pointers to
functions returning pointers to characters?
Answer1
If you want the code to be even slightly readable, you will use typedefs.
typedef char* (*functiontype_one)(void);
typedef functiontype_one (*functiontype_two)(void);
functiontype_two myarray[N]; //assuming N is a const integral
Answer2

char* (* (*a[N])())()
Here a is that array. And according to question no function will not take any
parameter value.
What does extern mean in a function declaration?
It tells the compiler that a variable or a function exists, even if the compiler hasn’t
yet seen it in the file currently being compiled. This variable or function may be
defined in another file or further down in the current file.
How do I initialize a pointer to a function?
This is the way to initialize a pointer to a function
void fun(int a)
{
}
void main()
{
void (*fp)(int);
fp=fun;
fp(1);
}
Write a fucntion that will reverse a string.
char *strrev(char *s)
{
int i = 0, len = strlen(s);
char *str;
if ((str = (char *)malloc(len+1)) == NULL)
/*cannot allocate memory */
err_num = 2;
return (str);
}
while(len)
str[i++]=s[–len];
str[i] = NULL;
return (str);
}


Object Oriented Programming
Data Abstraction:-is the act of representing the essential features without including
the background details and explanations.
Data Hiding:-is a related concept to data abstraction. Unessential features or
background details are hidden from the world.
Data Encapsulation:-is the wrapping up of data and associated functions in one
single until,while implementing abstraction at the same time.
Objects:-represents an identifiable entity with some characteristics and behaviour.
What do you mean by inheritance?
Inheritance is the process of creating new classes, called derived classes, from
existing classes or base classes. The derived class inherits all the capabilities of the
base class, but can add embellishments and refinements of its own.
Base Class:-is the class, which gets inherited from other classes. It is also called
Super class.
Derived class is the class which inherits from other classes. It is also called
Subclass.
What is polymorphism? Explain with an example?
"Poly" means "many" and "morph" means "form". Polymorphism is the ability of an
object (or reference) to assume (be replaced by) or become many different forms of
Object.
Example: function overloading, function overriding, virtual functions. Another
example can be a plus
‘+’ sign, used for adding two integers or for using it to concatenate two strings.
OPP is approach for writing software in which data and behaviour are packaged
together as classes whose instances are objects.
A class is a named software representation for an abstraction.
What is function overloading and operator overloading?
Function overloading: C++ enables several functions of the same name to be
defined, as long as these functions have different sets of parameters (at least as far
as their types are concerned). This capability is called function overloading. When an
overloaded function is called, the C++ compiler selects the proper function by
examining the number, types and order of the arguments in the call.
Function overloading is commonly used to create several functions of the same name
that perform similar tasks but on different data types.
Operator overloading allows existing C++ operators to be redefined so that they
work on objects of user-defined classes. Overloaded operators are syntactic sugar for
Equivalent function calls. They form a pleasant facade that doesn't add anything
fundamental to the language (but they can improve understandability and reduce
Maintenance costs).
Very Short Answer Questions:-1-2 Marks
Fill in the blanks;
(i)………….. is a named collection of attributes and behaviour related to modeling a
given entity for some particular purpose?

(ii) …………..is a programming approach where data and behaviour are encapsulated
to gether as…………
(iii) An instance of a class is called ……………
(iv) A class enforces data hiding through …………and……………members.
(v) Polymorphism is implemented in c++ through ……..and……………
Answer:- 1) OPP, Classes 2) Abstraction 3) Object 4) Private, Protected 5) Virtual
functions, overloading.
What is abstract class?
Ans:- Abstract class is a class that defines an interface, but does not necessary
provide implementation for all its member functions. No objects of an abstract class
exit i.e, it cannot be instantited.
What is concrete class?
Ans:- A Concrete class is a derived class of an abstract class that implements all the
missing functionality . A concrete class can be instantiated i.e. its objects can be
created.
What is the importance of the function overloading?
Ans:- Function overloading not only implements polymorphism but also reduces
number of comparisons in a program and thereby makes the program run faster.
How objects implemented in c++?
Ans:- The object is implemented in software terms as follows:-
(1) Characteristics / attributes are implemented through member variables or
data items of the object.
(2) Behaviour is implemented through member functions called method.
(3) It is given a unique name to give it identify.
Why do you think function overloading must be a part of an object oriented
language?
Ans: - A Function overloading must be a part of an object oriented language as it is
the feature that implements polymorphism in an object oriented language. That is
the ability of an object to behave differently circumstances can effectively be
implemented in programming through function overloading.
Find the syntax error, if any in the following program.
Include
Void main()
Int R; w=90;
While w>60
{
R=W-50;
Switch(W)
{
20: cout<<”Lower range”<30: cout<<”Middle range”<40: cout<<”Higher range”<}
}
}
Find the syntax error, if any in the following program.
#include
void main()

{
int X,Y,
cin>>X;
for(Y=0;Y<10 br="">{ (X==Y)
cout<else
cout>>4;
}
}
Find the syntax error, if any in the following program.
Include
Void main()
{
int P[ ]= {90,60,30,10}; Q;Number=4;
Q=9;
For[int i=Number-1;>=0;i--]
Switch(i)
{
case0:
case 3: cout>>P[i]*Q<case 1:
case 2: cout<
}
}
Give the output of the following program:-
#include
int global=10;
void sunil(int &x, int y)
{
x=x-y;y=x*10;
cout<}
void main()
{
int global=7;
sunil(::global,global);
cout<sunil(global,::global);
cout<}
Ans:- 3,30
7,3
4,40
4,3
Give the output of the following program:-
void main()
{
char*name=”ProOcessoR”
for(int x=0,x{ if(islower(name[x]))

name(x)=toupper(name[x]);
else
if(isupper(name[x]))
if(x%2==0)tolower
name[x]=(name[x]);
else
name(x)=name[x-1];
}
puts(name);
}
Ans:- pRo OEesOr
What is function overloading? Use the concept of the function overloading
to compute area of rectangle (given length of two sides), area of a triangle
(given length of the three sides using Hero’s formula) and area of circle
(given length of a radius).
#include
#include
#include
float area(float a, float b)
{
return a*b;
float area(float a, float b, float c)
{
float s, ar;
s=(a+b+c)/2;
ar=sqrt(s*(s-a)*(s-b)*(s-c));
return ar;
}
float area(float a)
{
return 3.1423*a*a;}
int main()
{
clrscr();
int choice, S1,S2,S3;
float ar;
do
{
cout<<”\n Area Main Menu\n”;
cout<<”\n Rectangle\n”;
cout<<”\n Triangle\n”;
cout<<”\n Circle\n”;
cout<<”\n Exit\n”;
cout<<”\n Enter your choice(1-4):-”;
cin>>choice; cout<switch(choice)

PREVIOUS YEAR COMPUTER SCIENCE CBSE BOARD QUESTION ANSWERS OF CHAPTER 1 - CLASS XII

1. C++ REVISION TOUR
2012 Delhi:
1.a) Give the difference between the type
casting and automatic type conversion. Also,
give a suitable C++ code to illustrate both. 2
Ans. Automatic Type Conversion: It is an
implicit process of conversion of a data from
one data type to another. It is performed by the
compiler. It is also called as type promotion.
For example:
int N = 65;
char C = N; // Automatic type conversion
cout<Output:
A
Type Casting: It is an explicit process of
conversion of a data from one type to another.
(It is performed by the programmer.)
For example
int A=1, B=2;
float C = (float)A/B; //Type Casting
cout<Output:
0.5
b) Which C++ header file(s) are essentially
required to be included to run/execute the
following C++ source code (Note: Do not
include any header file, which is/are not
required): 1
void main( )
{ char TEXT[ ]=”SomeThing”;
cout<<”Remaining SMS Chars:”<<
160-strlen(TEXT)<}
Ans: iostream.h (for cout)
string.h (for strlen( ));
(f) Observe the following program and find out,
which output(s) out of (i) to (iv) will not be
expected from the program? What will be the
minimum and the maximum value assigned to
the variable Chance?
#include
#include
void main( )
{ randomize( );
int Arr[ ] ={9,6},N;
int Chance=random(2)+10;
for(int C=0;C<2 br="">{ N=random(2);
cout<}
}
(i) 9#6#
(ii) 19#17#
(iii) 19#16#
(iv) 20#16#
Ans: (i) 9#6#
Minimum Value: 10
Maximum Value: 11
2011 Delhi:
1. (a) What is the difference between Local
Variable and Global Variable? Also, give a
suitable C++ code to illustrate both. 2
Ans:
Local Variables: Local variables are those
variables which are declared within a function
or a compound statement and these variables
can only be used within that function/scope.
Global Variables: Global variables are those
variables which are not declared within any
function or scope. So, these variables can be
accessed by any function of the program.
Example:
#include
#include
int G; // Global variable declared
void Fun ( )
{i
nt L=25;// Local variable of function Fun ( ) assigned value 25
G=5; // Global Variable is accessed and assigned value 5
cout<cout<}
void main ( )
{
Fun ( ) ; // Function call
G = G + 5; // Global variable is incremented by 5
cout<}
(b) Write the names of the header files, which
is/are essentially required to run/execute the
following C++ code: 1
void main ( )
{
char C, String [ ] = "Excellence Overload";
for (int I=0; String [ I ] ! = '\ 0'; I ++ )
if (String [I] ==' ')
cout<else
{
C=toupper(String[I]);
cout<}
}
Ans: iostream.h ctype.h

(c)Rewrite the following program after
removing the syntactical errors (if any).
Underline each correction. 2
#include[iostream.h]
typedef char Text(80) ;
void main ( )
{
Text T= "Indian";
int Count=strlen(T) ;
cout<}
Ans:
#include
#include
typedef char Text [80];
void main ( )
{
Text T= "Indian";
int Count=str1en(T);
cout<}
(d)Find the output of the following program: 3
#inc1ude
void ChangeArray(int Number, int ARR[ ], int Size)
{f
or (int L =0; Lif (LARR [L] +=L;
e1se
ARR [L] *=L;
}
void Show (int ARR [ ], int Size)
{f
or (int L=0; L(L%2!=0)?cout<cout<}
void main ( )
{ int Array [
]
=
{
30, 20, 40, 10, 60, 50};
ChangeArray (3, Array, 6) ;
Show (Array, 6) ;
}
Ans: 30
21#42
30#240
250#
(f) Go through the C++ code shown below, and
find out the possible output or outputs from the
suggested Output Options (i) to (iv). Also, write
the least value and highest value, which can be
assigned to the variable Guess. 2
#include
#include
void main ( )
{r
andomize ( ) ;
int Guess, High=4;
Guess=random{High)+ 50 ;
for{int C=Guess ; C<=55 ; C++)
cout<}(
i) 50 # 51 # 52 # 53 # 54 # 55 #
(ii) 52 # 53 # 54 # 55
(iii) 53 # 54 #
(iv) 51 # 52 # 53 # 54 # 55
Ans: (i) 50 # 51 # 52 # 53 # 54 # 55 #
Least value 50
Highest value 53


2011 Outside Delhi:
1. (a) What is the difference between Type
Casting and Automatic Type conversion? Also,
give a suitable C++ code to illustrate both.
Ans: Refer 2012 Delhi
(b) Write the names of the header files, which
is/are essentially required to run/execute the
following c++ code:
void main ( )
{ char CH,Text[ ] ="+ve Attitude";
for (int I=0 ; Text[I] ! ='\0' ;I++)
if (Text[I]== ' ')
cout<else
{ CH=toupper (Text [I]) ;
cout<}
}
Ans: iostream.h, ctype.h
(c) Rewrite the following program after
removing the syntactical errors (if any).
Underline each correction. 2
include
typedef char [80] String;
void main ( )
{ String S= "Peace";
int L=strlen(S) ;
cout<}
Ans:
#include
#include
typedef char String [80];
void main ( )
{
String S = "Peace";
int L= strlen(S) ;
cout<}

(d) Find the output of the following program:3
#include
void SwitchOver(int A [ ], int N, int Split)
{f
or (int K=0 ; Kif (KA(K]+ =K;
else
A [K]*=K;
}
void Display (int A [ ], int N)
{f
or (int K=0 ; K(K%2==0)?
cout<}
void main ( )
{ int H[ ]= {
30,40,50,20,10,5};
SwitchOver (H, 6, 3);
Display (H, 6);
}
Ans:
30%41
52%60
40%25
(f) Go through the C++ code shown below, and
find out the possible output or outputs from the
suggested Output Options (i) to (iv). Also, write
the minimum and maximum values, which can
be assigned to the variable MyNum. 2
#include
#include
void main ( )
{ randomize (
)
;
int MyNum, Max=5;
MyNum = 20 + random (Max) ;
for (int N=MyNum; N<=25;N++)
cout} (
i) 20*21*22*23*24*25
(ii) 22*23*24*25*
(iii) 23*24*
(iv) 21*22*23*24*25
Ans (ii) 22*23*24*25*
Minimum value 20
Maximum value 24
2010 Delhi:
1. (a) What is the difference between automatic
type conversion and type casting? Also, give a
suitable C++ code to illustrate both. 2
Ans: Refer 2012 Outside Delhi
(b) Which C++ header file(s) will be essentially
required to be included to run/ execute the
following C++ code? 1
void main( )
{
int Eno=123, char Ename[ ]=”Rehan Swamp”;
cout<}
Ans. (i) iostream.h (ii) iomanip.h
(d) Find the output of the following program : 3
#inc1ude
struct POINT
{ int X, Y, Z;
};
void StepIn(POINT & P, int Step=1)
{ P.X+=Step;
P.Y-=Step;
P.Z+=Step;
}
void StepOut(POINT & P, int Step=1)
{ P.X-=Step;
P.Y+=Step;
P.Z–=Step;
}
void main ( )
{ POINT P1={15, 25, 5}, P2={10, 30, 20};
StepIn(P1);
StepOut(P2,4);
cout<;
cout<;
StepIn(P2,12);
cout<;
}
Ans.
16, 24, 6
6, 34, 16
18, 22, 28
(e) Find the output of the following program : 2
#include
#include
void ChangeIt(char Text[ ], char C)
{ for (int K=0;Text[K]!='\0';K++)
{ if (Text[K]>='F' && Text[K]<='L')
Text[K]=tolower (Text[K]);
else
if (Text[K]=='E' || Text[K]=='e')
Text[K]=C;
else
if (K%2==0)
Text[K]=toupper(Text[K]);
else
Text[K]=Text[K-1];

}
}
void main ( )
{
char OldText[ ]="pOwERALone";
ChangeIt(OldText,'%');
cout<<"New
TEXT:"<}
Ans.
New TEXT : PPW % RRllN%
(f) The following code is from a game, which
generates a set of 4 random numbers. Yallav is
playing this game, help him to identify the
correct option(s) out of the four choices given
below as the possible set of such numbers
generated from the program code so that he
wins the game. Justify your answer. 2
#include
#include
const int LOW=15;
void main ( )
{ randomize( ) ;
int POINT=5, Number;
for (int 1=1;I<=4;I++)
{ Number=LOW+random(POINT) ;
cout<POINT--;
}
} (
i) 19:16:15:18:
(ii) 14:18:15:16:
(iii) 19:16:14:18
(iv) 19:16:15:16:
Ans.
(iv) 19:16:15:16:
Justification is as follows:
I POINT random Minimum Maximum
(POINT) Number Number
1 5 0 to 4 15 19
2 4 0 to 3 15 18
3 3 0 to 2 15 17
4 2 0 to 1 15 16
The only option that satisfies these values is option (iv).
2010 Outside Delhi:
1. (a) What is the difference between call by
value and call by reference? Also, give a
suitable C++ code to illustrate both. 2
Ans.
Call by value: The formal parameter makes a
copy of actual parameter. It does not make the
changes In actual parameter If the changes are
done In formal parameters.
Call by reference: The formal parameter Is an
alias of actual parameter. Thechanges made In
the formal parameter are reflected In actual
parameter. It is preceded by &.
void Calculator(int A,int & B )
{
A++;
a+=A;
}
Here A is called by value and B is called by
reference.
(b) Which C++ header file(s) will be essentially
required to be included to run/execute the
following C++ code: 1
void main()
{i
nt Rno=24; char Name [] =” Amen Singhania”;
cout<}
Ans.
iostream.h, iomanip.h
(d) Find the output of the following program: 3
#include
struct THREE_D
{ int X,Y,Z;
};
void MoveIn(THREE_D &T, int Step=l)
{ T.X+=Step;
T.Y-=Step;
T.Z+=Step;
}
void MoveOut(THREE_D &T, int Step=l)
{ T.X-=Step;
T.Y+=Step;
T.Z-=Step;
}
void main ()
{ THREE_D Tl={lO,20,5},T2={30,lO,40};
MoveIn(T1);
MoveOut(T2,5) ;
cout<cout<l;
MoveIn(T2,l0);
cout<;
}
Ans. 11, 19, 6
25, 15, 35
35, 5, 45
(e) Find. the output of the following program: 2
#include
#include
void MyCode (char Msg [], char CH)
{ for (int (Cnt=O;Msg[Cnt]!=’\0';Cnt++)

{ if (Msg[Cnt]>=’B’ && Msg[Cnt]<=’G’)
Msg[Cnt]=tolower(Msg[Cnt]);
else
if (Msg[Cnt]==’A’|| Msg[Cnt]==’a’)
Msg[Cnt]=CH;
else
if (Cnt%2==0)
Msg[Cnt]=toupper(Msg[Cnt]);
else
Msg[Cnt]=Msg[Cnt-l];
}
}
void main ()
{ char MyText [] =” ApEACeDriVE”;
MyCode(MyText,’@’);
cout<<“NEW TEXT:”<}
Ans.
NEW TEXT :@@e@ccddIIe
(f) The following code is from a game, which
generates a set of 4 random numbers. Praful is
playing this game, help him to identify the
correct option(s) out of the four choices given
below as the possible set of such numbers
generated from the program code so that he
wins the game. Justify your answer. 2
#include
#include
const int LOW=25;
void main ()
{
randomize() ;
int P01NT=5,Number;
for (int I=1;I<=4;I++)
{
Number=LOW+random(POINT);
cout<P0INT--;
}
} (
i) 29:26:25:28:
(ii) 24:28:25:26:
(iii) 29:26:24:28:
(iv) 29:26:25:26:
Ans.
(iv) 29:26:25:26:
Justification is as follows:
I POINT Number
Minimum Maximum
1 5 25 29
2 4 25 28
3 3 25 27
4 2 25 26
The only option that satisfies these values is option (iv).
2009 Delhi:
1. (a) What is the difference between call by
value and call by reference? Give an example in
C++ to illustrate both. 2
Ans:
Call by value is used to create a temporary
copy of the data coming from the actual
parameter into the formal parameter. The
changes done in the function in formal
parameter are not reflected back in the calling
environment. It does
not use ‘&’ sign.
Call by reference is used to share the same
memory location for actual and formal
parameters and so changes done in the function
are reflected back in the calling environment. It
uses ‘&’ sign.
void Compute(int A, int &B)
{ A++;
B++;
cout<<”In the function”<cout<<”A=”<
}
void main ()
{ int I=50,J=25;
cout<<”Before function call “<cout<<”I=”<Compute (I,J) ;
cout<<”After function call “<cout<}
OUTPUT
Before function call
I=50&J=25
In the function
A=51&B=26
After function call
I=50&J=26
1(b) Write the names of the header files to
which the following belong: 1
(i) puts ( ) (ii) sin ( )
Ans (i) stdio.h (ii) math. h
1.(e) Find the output of the following program:2
#include
#include
void Encode (char Info [ ], int N) ;
void main ( )
{ char Memo [ ] = “Justnow” ;
Encode (Memo, 2) ;
cout<}
void Encode (char Info [ ], int N)
{ for (int I = 0 ; Info[I] !=‘\0’ ; 1++)
if (1%2= =0)
Info[I] = Info[I] –N ;
else if (islower(Info[I]))
Info[I] = toupper(Info[I]) ;

else
Info[I] = Info[I] +N ;
}
Ans
HuqTlOu
(f) Study the following program and select the
possible output from it : 2
#include
#include
const int LIMIT = 4 ;
void main ( )
{ randomize( ) ;
int Points;
Points = 100 + random(LIMIT) ;
for (int P=Points ; P>=100 ; P– –)
cout<
cout<} (
i) 103#102#101#100#
(ii) 100#101#102#103#
(iii) 100#101#102#103#104#
(iv) 104#103#102#101#100#
Ans
(i) 103#102#101#100#
2009 Outside Delhi:
1. (a) What is the difference between Actual
Parameter and Formal Parameter? Give an
example in C++ to illustrate both types of
parameters. 2
Ans A parameter used in the function call is
known as Actual Parameter. It is used to send
the data to function. A parameter used in the
function definition is known as Formal
Parameter, It is used to accept the data from
actual parameter.
void Seventimes(int A) //A is formal parameter
{ cout<<7 br="">}
void main ()
{ int P=6;
Seventimes(P); //p is actual parameter
}
Other answer for the same question:
Ans)The parameters in the function call
statement (or calling function) are called as
Actual Parameters.
The parameters in the function
definition (or called function) are called as
Formal Parameters.
Eg:
void manip(int x, int y)
{ ---
---
}
void main( )
{
int a,b;
----
manip(a,b);
}
Here a,b are Actual Parameters and
x,y are Formal Parameters.
1.(b) Write the names of the header files to
which the following belong: 1
(i) setw( ) (ii) sqrt( )
Ans
(i) iomanip.h (ii) math.h
1.(e) Find the output of the following program:2
#include
#include
void Secret (char Mig[ ], int N);
void main ( )
{ char SMS[ ] = “rEPorTmE” ;
Secret{SMS,2);
cout<}
void Secret(char Msg[ ], int N)
{ for (int C=0; Msg[C] !=’ \0' ;C++)
if (C%2==0)
Msg[C] = Msg[C]+N;
else if (isupper(Msg[C]))
Msg[C] = tolower(Msg[C]);
else
Msg[C] = Msg[C]-N;
}
Ans
teRmttoe
(f) Study the following program and select the
possible output from it : 2
#include
#include
const int MAX=3 ;
void main ( )
{ randomize( ) ;
int Number ;
Number = 50 + random{MAX) ;
for (int P=Number; P>=50; P– –)
cout<
cout<}(
i) 53#52#51#50#
(ii) 50#51#52#
(iii) 50#51#
(iv) 51#50#
Ans
(iv) 51#50#
(Solution: MAX value is 3. That’s why
random(MAX) can produce 0 or 1 or 2.
(random(N)will produce no.between 1 to n-1). The
Number value may be 50 or 51 or 52. The P value
starts from Number, upto 50,each time decreases by
1.
So Possible outputs are as follows:
52#51#50#

51#50#
50#.
As the output 51#50# is available in given
answers, so 51#50# is the answer.)
2008 Delhi:
1.b)Name the header files that shall be needed
for the following code: 1
void main( )
{ char String[ ] = “Peace”;
cout << setw(2)<}
Ans) iomanip.h, iostream.h
1. c)Rewrite the following program after
removing the syntactical error(s) if any.
Underline each correction. 2
#include
void main( )
{ First = 10, Second = 20;
Jumpto(First;Second);
Jumpto(Second);
}
void Jumpto(int N1, int N2 = 20)
{ N1=N1+N2;
count<>N2;
}
Ans)
#include
void Jumpto(int N1,int N2=20); //Prototype missing
void main( )
{ int First = 10, Second = 20; //Data type missing
Jumpto(First,Second); //Comma to come instead of ;
Jumpto(Second);
}
void Jumpto(int N1, int N2)
{ N1=N1+N2;
cout<}
d)Find the output of the following program;3
#include
#include
void main( )
{ char Text[ ] = “Mind@work!”;
for(int I=0; Text[I]!=’\0’;I++)
{ if(!isalpha(Text[I]))
Text[I]=’*’;
else if(isupper(Text[I]))
Text[I]=Text[I]+1;
else
Text[I] = Text[I+1];
}
cout<}
Ans: Text[ ] =
When I=0
Since Text[0] is ‘M’, Upper Case Letter,
(isupper(Text[I]) will becomes true.
So Text[I] =Text[I]+1
So Text[0]=Text[0]+1
Text[0] =77(ASCII Value of M) + 1 = 78
=N(78 is ASCII Value of N)
Now the String Text[ ] =
When I=1
Since Text[1] is ‘i’, Which is a character, but
which is not Upper case,
else part will be executed.
Ie Text[I]=Text[I+1]
Here Text[1]=Text[1+1]
=Text[2]
Ie ‘n’ will be stored in place of ‘i’
Now the String Text[ ] =
When I=2
Since Text[2] is ‘n’, Which is a character, but
which is not Upper case, else part will be
executed.
Ie Text[I]=Text[I+1]
Here Text[2]=Text[2+1]
=Text[3]
Ie ‘d’ will be stored in place of ‘n’
Now the String Text[ ] =
When I=3
Since Text[3] is ‘d’, Which is a character, but
which is not Upper case, else part will be
executed.
Ie Text[I]=Text[I+1]
Here Text[3]=Text[3+1]
=Text[4]
Ie ‘@’ will be stored in place of ‘d’
Now the String Text[ ] =
When I=4
Since Text[4] is ‘@’, Since which is not an
alphabet,
(!isalpha(Text[I])) will becomes true.
Ie if(!isalpha(Text[I]))
Text[I]=’*’;

Ie Text[4]=’*’
Ie ‘*’ will be stored in place of ‘@’
Now the String Text[ ] =
When I=5
Since Text[5] is ‘W’, Upper Case Letter,
(isupper(Text[I]) will becomes true.
So Text[I] =Text[I]+1
So Text[5]=Text[5]+1
Text[5] =87(ASCII Value of W) + 1 = 88
=X(88 is ASCII Value of X)
Now the String Text[ ] =
When I=6
Since Text[6] is ‘o’, Which is a character, but
which is not Upper case, else part will be
executed.
Ie Text[I]=Text[I+1]
Here Text[6]=Text[6+1]
=Text[7]
Ie ‘r’ will be stored in place of ‘o’
Now the String Text[ ] =
When I=7
Since Text[7] is ‘r’, Which is a character, but
which is not Upper case, else part will be
executed.
Ie Text[I]=Text[I+1]
Here Text[7]=Text[7+1]=Text[8]
Ie ‘k’ will be stored in place of ‘r’
Now the String Text[ ] =
When I=8
Since Text[8] is ‘k’, Which is a character, but
which is not Upper case, else part will be
executed. Ie Text[I]=Text[I+1]
Here Text[8]=Text[8+1]
=Text[9]
Ie ‘!’ will be stored in place of ‘k’
Now the String Text[ ] =
When I=9
Since Text[9] is ‘!’, Since which is not an
alphabet, (!isalpha(Text[I])) will becomes true.
Ie if(!isalpha(Text[I]))
Text[I]=’*’;
Ie Text[9]=’*’
Ie ‘*’ will be stored in place of ‘!’
Now the String Text[ ] =
Output: Nnd@*Xrk!*
e) Find the output of the following program:
2
#include
void main( )
{ int U=10,V=20;
for(int I=1;I<=2;I++)
{ cout<<”[1]”<cout<<”[2]”<<++V<<”&”<}
}
Ans: Output:
[1]10&15
[2]21&13
[1]11&16
[2]22&14
f) In the following program, find the correct
possible output(s) from the options:
2
#include
#include
void main( )
{ randomize( );
char City[][10]=
{“DEL”,”CHN”,”KOL”,”BOM”,”BNG”};
int Fly;
for(int I=0; I<3 br="">{ Fly=random(2) + 1;
cout<}
}
Outputs:
(i) DEL : CHN : KOL:
(i) CHN: KOL : CHN:
(ii) KOL : BOM : BNG:
(iii) KOL : CHN : KOL:
Ans)Since random(2) gives either 0 or 1, Fly value
will be either 1 or 2. (random(n) gives you any
number between 0 to n-1)
City[1] is “CHN”. City[2] is “KOL”.
Since I value from 0 to 2 (ie<3 3="" br="" iterations="" will="">takes place. So the possible output consists 3 strings
separated by :, each of them may be either “CHN” or
“KOL”.
So the possible output will be
(ii) CHN : KOL : CHN:
(iv) KOL :CHN : KOL:
1.a) What is the difference between #define and
const? Explain with suitable example. 2

Ans: While they both serve a similar purpose,
#define and const act differently. When using
#define the identifier gets replaced by the
specified value by the compiler, before the code
is turned into binary. This means that the
compiler makes the substitution when you
compile the application.
Eg: #define number 100
In this case every instance of “number” will be
replaced by the actual number 100 in your code,
and this means the final compiled program will
have the number 100 (in binary).
#define with different types of data:
*The #define preprocessor allows u s to define
symbolic names and constants.
Eg: #define PI 3.14159
*The #define allows you to make text
substitutions before compiling the program.
Eg: #define MAX 70
* Before compilation, if the C++ preprocessor
finds MAX as one word, in the source code, it
replaces it with the number 70.
* The #define preprocessor can be used in the
creation of macros (code substitution).
Eg: #define SQUARE(x) x*x
Before compilation, if the C++
preprocessor finds SQUARE(x), where x is any
value in the source code, it replaces it with its
square (ie x*x). Here a macro substitutes text
only; It does not check for data types.
On the other hand, when we use const
and the application runs, memory is allocated
for the constant and the value gets replaced
when the application is run.
Syntax: const type variable_name=value;
Eg: const int a=10;
The value of a constant is fixed
and in the above example, the value for a in
entire program is 10 only. You cannot change
the value of a, since it is declared as constant.
Difference between #define and const in
declaration:.
1.#define: #define symbolic_constant value.
Eg: #define number 100 //No semicolon ,no
equal to symbol.
2.const: const type variable_name=value;
Eg: const number=100; //Semicolon, equal to
symbol.
2008 Outside Delhi:
1.b) Name the header files that shall be needed
for the following code:
1
void main( )
{ char word[]=”Exam”;
cout<}
Ans: iostream.h, iomanip.h
1.c) Rewrite the following program after
removing the syntax error(s) if any. Underline
each correction. 2
#include
void main( )
{ One=10,Two=20;
Callme(One;Two);
Callme(Two);
}
void Callme(int Arg1,int Arg2)
{ Arg1=Arg1+Arg2;
Count<>Arg2;
}
Ans:
void Callme(int Arg1,int Arg2=20);
#include
void main( )
{ int One=10,Two=20;
Callme(One,Two); //Given ; instead of ,
Callme(Two);
}
void Callme(int Arg1,int Arg2)
{
Arg1=Arg1+Arg2;
cout<}
1.d)Find the output of the following program:3
#include
#include
void main( )
{ char Mystring[ ] = "what@OUTPUT!";
for(int I=0; Mystring[I]!='\0';I++)
{ if(!isalpha(Mystring[I]))
Mystring[I]='*';
else if(isupper(Mystring[I]))
Mystring[I]=Mystring[I]+1;
else
Mystring[I] =Mystring[I+1];
}
cout<Ans:
Output: hat@*PVUQVU*
e) Find the output of the following program: 2
#include
void main( )
{ int A=5,B=10;
for(int I=1;I<=2;I++)
{ cout<<”Line1”<
cout<<”Line2”<<++B<<”&”<}
}
Ans: Output:

Line15&8
Line211&9
Line16&9
Line212&10
f) In the following program, find the correct
possible output(s) from the options: 2
#include
#include
void main( )
{ randomize( );
char Area[ ][10]={“NORTH”,”SOUTH”,”EAST”,”WEST”};
int ToGo;
for(int I=0; I<3 br="">{ ToGo=random(2) + 1;
cout<}
}
Ans: Outputs:
(i) SOUTH : EAST : SOUTH :
(ii) NORTH : SOUTH : EAST :
(iii) SOUTH : EAST : WEST :
(iv) SOUTH : EAST : EAST :
Ans)Since random(2) gives either 0 or 1, ToGo
value will be either 1 or 2. (random(n) gives you any
number between 0 to n-1)Area[1] is
“SOUTH”.Area[2] is “EAST”. Since I value from 0
to 2 (ie<3 3="" br="" iterations="" place.="" so="" takes="" the="" will="">possible output consists 3 strings separated by :, each
of them may be either “SOUTH” or “EAST”.
So the possible output will be
(i) SOUTH : EAST : SOUTH :
(iv) SOUTH : EAST : EAST :
1.a) What is the purpose of using a typedef
command in C++?Explain with suitable
example 2
Ans: C++ allows you to define explicitly new
data type names by using the keyword typedef.
Using typedef does not actually create a new
data class, rather it defines a new name for an
existing type. This can increase the portability
of a program as only the typedef statements
would have to be changed. Typedef makes your
code easier to read and understand. Using
typedef can also aid in self documenting your
code by allowing descriptive names for the
standard data types.
The syntax of the typedef statement is
typedef type name;
Where type is any C++ data type and name is
the new name for this type. This defines
another name for the standard type of C++. For
example, you could create a new name for float
values by using the following statement:
typedef float amount;
This statement tells the compiler to recognize
amount as an alternative name for float. Now
you could create float variables using amount.
amount loan, saving, installment;
Using typedef does not replace the standard C+
+ data type name with the new name, rather the
new name is in addition to the existing name.
You still can create float variables using float.
Once a new name has been defined by typedef,
it can be used as a type for another typedef also.
Eg: typedef amount money;
Now, this statement tells the compiler to
recognize money as another name for amount,
which itself is another name for float. Typedef
does not create any new data types rather
provides an alternative name for standard types.
Reference provides an alias name for a variable
and typedef provides an alias name for a data
type.
2007 Delhi :
1.a) Differenctiate between a Run Time Error
and Syntax Error. Also give suitable examples
of each in c++. 2
Ans: Run Time Errors: Errors that occur
during the execution of a program are called as
run time errors. It is caused of some illegal
operation taking place or inavailability of
desired or required conditions for the execution
of the program. For instance, if a program is
trying to open a file which does not exist or it
could not be opened, it results into an execution
error. Similarly, if enough memory is not
available or an expression is trying to divide a
number by zero are run-time errors.
Eg: Division by zero. c=a/b ;
User will give the values of a and b at the time
of program execution. If he give the value of b
as ‘0’ , then division by zero, ie a run time error
occurs.
Syntax Errors:Syntax errors occur when rules
of a programming languages (syntax) is
misused. Ie when a grammatical rule of C++ is
violated.
Eg (i) c=a+b In this statement,
since there is no semicolon at the end of the
statement, there will occurs a syntax error.
(ii) cin<
stream insertion operator (<<) has given instead
of stream extraction operation(>>), there will
occurs a syntax error.
1.b) Name the header file(s) that shall be needed
for successful compilation of the following C++
code. 1
void main( )
{ char String[20];
gets(String);
strcat(String,”CBSE”);
puts(String);
}
Ans) stdio.h string.h

1. c) Rewrite the following program after
removing the syntactical error(s) if any.
Underline each correction. 2
#include
const int Max 10;
void main()
{ int Numbers[Max];
Numbers = {20,50,10,30,40};
for(Loc=Max-1;Loc>=10;Loc--)
cout>>Numbers[Loc];
}
Ans)
#include
const int Max = 10 ;/ /Constant Variable ‘Max’ must be
// initialized.Declaration Syntax Error
void main( )
{ int Numbers[Max]={20,50,10,30,40};
for(Loc=Max-1;Loc>=0;Loc--)
cout>>Numbers[Loc];
}
e)Find the output of the following program. 3
#include
void Withdef(int HisNum=30)
{ for(int I=20;I<=HisNum;I+=5)
cout<cout<}
void Control(int &MyNum)
{ MyNum+=10;
Withdef(MyNum);
}
void main()
{ int YourNum=20;
Control(YourNum);
Withdef();
cout<<”Number=”<}
Ans:
Output:
20,25,30,
20,25,30,
Number=30
f)In the following C++ program what is the
expected value of MyMarks from options (i) to
(iv)given below. Justify answer.
2 #
include
#include
void main( )
{ randomize( );
int Marks[]={99,92,94,96,93,95},MyMarks;
MyMarks = Marks [1+random(2)];
cout<} (i)99 (ii)94 (iii)96 (iv) None of the above.
Ans: Output:
(ii) 94
2007 Outside Delhi:
1.a) Differentiate between a Logical Error and
Syntax Error. Also give suitable examples of
each in C++. 2
Ans: Logical Error: A logical error is that
error which causes a program to produce
incorrect or undesired output.
An incorrectly implemented algorithm
or use of a variable before its initialization, or
unmarked end for a loop, or wrong parameters
passed are causes logical errors. These must be
handled carefully.
For instance, if we are trying to print the
table of a number 5 and if we say
counter=1;
while(counter>8)
{ cout<counter=counter+1;
}
Here the loop would not be executed even once
as the condition (counter>8) is not fulfilled at
all. Therefore, no output will be produced.
Such an error is logical error.
Syntax Error: Syntax errors occur when rules
of a programming languages (syntax) is
misused. Ie when a grammatical rule of C++ is
violated.
Eg (i) c=a+b
In this statement, since there is no semicolon at
the end of the statement, there will occurs a
syntax error.
(ii)cin<
insertion operator (<<) has given instead of
stream extraction operation(>>), there will
occurs a syntax error.
1.b) Name the header file(s) that shall be needed
for successful compilation of the following C++
code. 1
void main( )
{ char Text[40];
strcpy(Text,”AISSCE”);
puts(Text);
}
Ans:
string.h, stdio.h
1.c) Rewrite the following program after
removing the syntactical error(s), if any.
Underline each correction. 2
#include
const int Size 5;
void main( )
{ int Array[Size];
Array={50,40,30,20,10};
for(Ctr=0;Ctrcout>>Array[Ctr];

}
Ans)
#include
const int Size=5;
void main( )
{ int Array[Size];
Array={50,40,30,20,10};
for(Ctr=0;Ctrcout<}
1.e)Find the output of the following program3
#include
void Indirect(int Temp=20)
{ for(int I=10;I<=Temp;I+=5)
cout<cout<}
void Direct(int &Num)
{ Num+=10;
Indirect(Num);
}
void main( )
{ int Number=20;
Direct(Number);
Indirect( );
cout<<”Number =”<}
Ans: Output:
10,15,20,25,30,
10,15,20,
Number =30
f) In the following C++ program what is the
expected value of Mysore from options (i) to
(iv) given below.Justify your answer. 2
#include
#include
void main( )
{ randomize( );
int Score[ ] = {25,20,34,56,72,63},Myscore;
cout<}
(i) 25 (ii) 34 (iii) 20 (iv) None of the above.
Ans: Expected Output:
(i) None of the above.
2006 Delhi:
1.a) Name the header file to which the
following below: (i) abs( ) (ii)
isupper( ) 1
Ans) (i) abs( ) - math.h, stdlib.h, complex.h
(ii)isupper( ) - ctype.h
1.e) Find the output of the following program 2
#include
void main( )
{ long NUM=1234543;
int F=0,S=0;
do
{ int R=NUM % 10;
if (R %2 != 0)
F += R;
else
S += R;
NUM / = 10;
} while (NUM>0);
cout<} Ans: Output: 2
1.b) Illustrate the use of #define in C++ to
define a macro. 2
Ans: The #define preprocessor can be used in
the creation of macros (code substitution).
Eg: #define SQUARE(x) x*x
Before compilation, if the C++ preprocessor
finds SQUARE(x), where x is any value in the
source code, it replaces it with its square (ie
x*x). Here a macro substitutes text only; It does
not check for data types.
1.C) Rewrite the following program after
removing the syntactical error(s), if any.
Underline each correction. 2
#include
void main( )
{ struct STUDENT
{
char stu_name[20];
char stu_sex;
int stu_age=17;
}student;
gets(stu_name);
gets(stu_sex);
}
Ans:
#include
#include
void main( )
{ struct STUDENT
{ char stu_name[20];
char stu_sex;
int stu_age;
//Initialization of variables inside a structure is not allowed.
}student;
gets(student.stu_name);
cin>>student.stu_sex);
//A single character cannot be read using gets
}
1.f) What are Nested Structures? Give an
example. 2

Ans: Nested structures are structures as
member of another structure. For example, the
date of birth is astructure within the structure of
a student as shown below. These types of
structures are known as nested structures.
Eg1:
struct date
{ int dd;
int mm;
int yy;
};
struct student
{ char name[20];
int roll;
date dob;
int marks;
};
The member of a nested structure is referenced
from the outermost to innermost with the help
of dot operators.
student stud;
Then the members of the nested structure can be
accessed as stud.dob.mm=10;
Eg2:
struct addr
{ int houseno;
char area[26];
char city[26];
char state[26];
};
struct emp
{ int empno;
char name[26];
char design[16];
addr address;
float basic;
}worker;
2006 Outside Delhi:
1.a) Name the header file to which the
following belong:
(i) pow ( ) (ii)random( ) 1
Ans:
(i) abs( ) - math.h, stdlib.h, complex.h
(ii)random( ) - stdlib.h
1.e)Find the output of the following program 2
#includevoid main( )
{ long Number=7583241;
int First = 0, Second =0;
do
{ int R=Number%10;
if(R%2 ==0)
First += R;
else
Second += R;
Number / = 10;
} while (Number > 0);
cout<}
Ans: Output -2
1.C) Rewrite the following program after
removing the syntactical error(s), if any.
Underline each correction. 2
#include
void main( )
{ struct movie
{ char movie_name[20];
char movie_type;
int ticket_cost=100;
}MOVIE;
gets(movie_name);
gets(movie_type);
}
Ans:
#include
#include
void main( )
{ struct movie
{ char movie_name[20];
char movie_type;
int ticket_cost;
//Initialization of variables inside a structure is not allowed.
}MOVIE;
gets(MOVIE.movie_name);
cin>>MOVIE.movie_type;
//A single character cannot be read using gets
}
2005 Delhi:
1.a) Differentiate between a Call by Value and
Call by Reference, giving suitable examples of
each. 2
Ans: Call by value: In call by value method,
the called function creates a new set of variables
and copies the values of arguments into them.
The function does not have access to the
original variables (actual parameters) and can
only work on the copies of values it created.
Passing arguments by value is useful when the
original values are not to be modified.
In call by reference method, a
reference to the actual argument (original
variable) is passed to the called function.
(Reference is an alias for a predefined variable.
Ie the same variable value can be accessed by
any of the two names: the original variable’s
name and the reference name.) Thus, in call by
reference method, the changes are reflected
back to the original values. The call by
reference method is useful in situations where
the values of the original variables are to be
changed using a function.
Example:

void Calculator(int A,int & B )
{
A++;
a+=A;
}
Here A is called by value and B is called by
reference.
1. b) Name the header files to which the
following belong: (i) abs( ) (ii) strcmp( ) 1
Ans) (i) abs( ) - stdlib.h, math.h, complex.h
(ii) strcmp( ) - string.h
1. c) Rewrite the following program after
removing the syntactical error(s), if any.
Underline each correction. 2
#include
const int Multiple 3;
void main( )
{ value = 15;
for(int Counter = 1;Counter = <5 -="2)<br" ounter="" value="">if(Value%Multiple = = 0)
cout<cout<else
cout<}
Answer:
#include
const int Multiple=3;
void main( )
{ int Value = 15;
for(int Counter = 1;Counter <=5;Counter ++, Value -=
2)
if(Value%Multiple == 0)
{ cout<cout<}
else
cout<}
1.e) Find the output of the following program 2
#include
#include
#include
void Convert(char Str[ ],int Len)
{ for(int Count=0;Count{ if(isupper(Str[Count]))
Str[Count]=tolower(Str[Count]);
else if (islower(Str[Count]))
Str[Count]=toupper(Str[Count]);
else if(isdigit(Str[Count]))
Str[Count]=Str[Count]+1;
else Str[Count]=’*’;
}
}
void main( )
{
char Text[ ]=”CBSE Exam 2005”;
int Size = strlen(Text);
Convert(Text,Size);
cout<for(int C=0,R=Size – 1;C<=Size/2;C++,R--)
{
char Temp=Text[C];
Text[C]=Text[R];
Text[R]=Temp;
}
cout<}
Ans:
Output:
cbse*eXAM*3116
6113*MXAe*esbc
1. f) Observe the following program
SCORE.CPP carefully, if the value of Num
entered by the user is 5, choose the correct
possible output(s) from the options from (i) to
(iv), and justify your option. 2
//Program: SCORE.CPP
#include
#include
void main( )
{ randomize( );
int Num,Rndnum;
cin>>Num;
Rndnum = random(Num) + 5;
for(int N = 1;N<=Rndnum;N++)
cout<}
Output Options:
(i) 1 2 3 4 (ii) 1 2
(iii) 1 2 3 4 5 6 7 8 9
(iv) 1 2 3
Ans: Expected Output:
(iii) 1 2 3 4 5 6 7 8 9
1.d) Find the output of the following program:
#include
struct MyBox
{ int Length,Breadth,Height;
};
void Dimension(MyBox M)
{ cout<cout<}
void main( )
{ MyBox B1={10,15,5},B2,B3;
++B1.Height;
Dimension(B1);
B3=B1;
++B3.Length;
B3.Breadth++;

Dimension(B3);
B2=B3;
B2.Height+=5;
B2.Length--;
Dimension(B2);
}
2005 Outside Delhi :
1.b) Name the header files to which the
following belong: (i) puts( )
(ii)isalnum( ) 1
Ans)(i) puts( ) - stdio.h
(isalnum( ) - ctype.h
1.c) Rewrite the following program after
removing the syntactical error(s), if any.
Underline each correction. 2
#include
const int dividor 5;
void main( )
{ Number = 15;
for(int Count=1;Count=<5 -="3)<br" ount="" umber="">if(Number % dividor = = 0)
cout<cout<else
cout<Ans:
#include
const int dividor= 5;
void main( )
{ int Number = 15;
for(int Count=1;Count<=5;Count++,Number -= 3)
if(Number % dividor = = 0)
{ cout<cout<}
else
cout<}
1.e) Find the output of the following program 2
#include
#include
#include
void Change(char Msg[],int Len)
{
for(int Count=0;Count{ if(islower(Msg[Count]))
Msg[Count] = toupper(Msg[Count]);
else if(isupper(Msg[Count]))
Msg[Count] = tolower(Msg[Count]);
else if (isdigit(Msg[Count]))
Msg[Count]=Msg[Count]+1;
else Msg[Count] = ‘*’;
}
}
void main( )
{
char Message[ ]=”2005 Tests ahead”;
int Size=strlen(Message);
Change(Message,Size);
cout<for(int C=0,R=Size – 1; C<=Size/2;C++,R--)
{ char Temp=Message[C];
Message[C]=Message[R];
Message[R]=Temp;
}
cout<}
Ans: Output:
3116*tESTS*AHEAD
DAEHA*SSTEt*6113
1.f) Observe the following program
GAME.CPP carefully, if the value of Num
entered by the user is 14, choose the correct
possible output(s) from the options from (i) to
(iv), and justify your option. 2
//Program:GAME.CPP
#include
#include
void main( )
{ randomize( );
int Num,Rndnum;
cin>>Num;
Rndnum=random(Num)+7;
for(int N=1;N<=Rndnum;N++)
cout<}
Output Options:
(i) 1 2 3 (ii) 1 2 3 4 5 6 7 8 9 10 11
(iii) 1 2 3 4 5 (iv) 1 2 3 4
Ans: Expected Output
(ii) 1 2 3 4 5 6 7 8 9 10 11
1.d) Find the output of the following program:
#include
struct Package
{ int Length,Breadth,Height;
};
void Occupies(Package M)
{ cout<cout<}
void main( )
{ Package P1={100,150,50},P2,P3;
++P1.Height;
Occupies(P1);
P3=P1;
++P3.Lengh;
P3.Breadth++;
Occupies(P3);
P2=P3;

P2.Height+=50;
P2.Length--;
Occupies(P2);
}
2004:
1.b) Write the names of the header files to
which the following belong:
(i) gets( ) (ii) strcmp( ) (iii)abs( ) (iv)isalnum( )
Ans: (i)gets( ) - stdio.h
(ii)strcmp( ) - string.h
(iii)abs( ) - math.h,
stdlib.h,complex.h
(iv)isalnum( ) - ctype.h
1.e) What will be the output of the following
program
#include
void main( )
{ int var1=5,var2=10;
for(int i=1,i<=2;i++)
{ cout<cout<}
}
Ans: Output:
5 9
9 7
7 7
7 9
f)Write definition for a function
SumSequence( ) in C++ with two arguments/
parameters – double X and int n. The function
should return a value of type double and it
should perform sum of the following series.
1/x- 3!/x2 + 5!/x3 – 7!/x4 + 9!/x5 - ------upto n terms.
Note: The symbol ! represents Factorial of a
number ie 5!= 1 X 2 X 3 X 4 X 5.
#include
#include
#include
double SumSequence(int x1,int n1);
void main()
{ int x;
int n;
clrscr();
cout<<"Enter the vaue of X and N";
cin>>x>>n;
cout<<”\nThe sum of the series =
“<getch();
}
double SumSequence(int x1,int n1)
{
double sum=0;
int c=0;
for(int i=1;i<=(2*n1);i=i+2)
{ int f=1;
for(int j=1;j<=i;j++)
{ f=f*j;
}
c=c+1;
if(c%2==1)
{ sum=sum+f/(pow(x1,c));
}
else
{ sum=sum-f/(pow(x1,c));
}
}
return sum;
}
1.c) Rewrite the corrected code for the
following program. Underline each correction
if any.
#include
structure Supergym
{ int member number;
char membername[20];
char membertype[]=”HIG”;
};
void main( )
{ Supergym person1,person2;
cin>>”Member Number: “;
cin>>person1.membhernumber;
cout<<”Member Name: “;
cin>>person1.membername;
person1.member type = “MIG”;
person2=person1;
cin>>”Member Number;“
<cin<<”Member Name”
<cin<<”Member Number:”
<}
Ans:
#include
#include
struct Supergym
{ int membernumber;
char membername[20];
char membertype[4];
};
void main( )
{ Supergym person1,person2;
cin>>"Member Number: ";
cin>>person1.membernumber;
cout<<"Member Name: ";
cin>>person1.membername;
strcpy(person1.membertype,"MIG");
person2=person1;
cin>>"Member Number;"

>>person2.membernumber;
cin>>"Member Name"
>>person2.membername;
cin>>"Member Number:"
>>person2.membertype;
}
2003:
1.a) What is the difference between global
variables and local variables? Give an example
to illustrate the same .2
Ans: The local variables are the variables
defined within any function (or block) and are
hence accessible only within the block in which
they are declared. In contrast to local variables,
variables declared outside of all the functions in
a program are called global variables. These
variables are defined outside of any function, so
they are accessible to all functions. These
functions perform various operations on the
data. They are also known as External
Variables.
Eg: #include
int a,b;
void main()
{ float f;
---;
---;
}
In the above program segment, a and b are
global variables, we can access a and b from
any function. f is local variable to function
main( ), we can access f from main( ) only.
1.b) Name the header file, to which the
following built-in function belongs:
(i) strcmp( ) (ii)getc( ) 1
Ans: (i) strcmp( ) - string.h
(ii) getc( ) - stdio.h
1.c) Rewrite the following program after
removing all the syntax error(s) if any.2
#include
void main( )
{ int P[ ]={90,10,24,15};Q,Number=4;
Q=9;
for[int I=Number-1;I>=0,I--]
switch(I)
{ case 0;
case 3:cout>>P[I]*Q<case 1:
case 2: cout<
}
}
Ans:
#include
void main( )
{ int P[ ]={90,10,24,15},Q,Number=4;
Q=9;
for(int I=Number-1;I>=0;I--)
switch(I)
{ case 0:
case 3:cout<
case 1:
case 2: cout<
}
}
1.e) Write the output of the following program:
#include
int Execute(int M)
{ if(M%3==0)
return M*3;
else
return M+10;
}
void Output(int B=2)
{ for(int T=0;Tcout<cout<}
void main( )
{ Output(4);
Output( );
Output(3);
}
f) Write a C++ function SUMFUN( ) having
two parameters Y(of type double) and m(of type
integer) with a result type as double to find the
sum of the series given below:
Y + Y3 / 2! + Y5 /3! + ------ + Y 2m-1 / m!
#include
#include
#include
double SUMFUN(int y1,int m1);
void main()
{ int y;
int m;
clrscr();
cout<<"Enter the vaue of Y and M";
cin>>y>>m;
cout<<”\nThe sum of the series =
“<getch();
}
double SUMFUN(int y1,int m1)
{ double sum=0;
double upper;
for(int i=1;i<=m1;i++)
{ int f=1;
for(int j=1;j<=i;j++)
{ f=f*j;
}
upper=pow(y1,(i*2-1));
sum=sum+upper/f;
}
return sum;
}

1.d) Give the output of the following program:
#include
struct Pixel
{ int C,R;
};
void Display(Pixel P)
{
cout<<”col”<
}
void main( )
{
Pixel X={40,50},Y,Z;
Z=X;
X.C+=10;
Y=X;
Y.R+=20;
Z.C-=15;
Display(X);
Display(Y);
Display(Z);
}
2002:
1.b)Name the header files of C++ to which the
following functions belong: 2
(i)get( ) (ii)open( )
(iii)abs( ) (iv)strcat( )
Ans: (i)get( ) - iostream.h
(ii)open( ) - fstream.h
(iii)abs( ) - math.h, stdlib.h
(iv)strcat( ) - string.h
1.c)Find the syntax error(s), if any, in the
following program. 2
#include
void main( )
{ int x;
cin>>x;
for( int y=0,y<10 br="" y="">cout<}
Ans:
#include
void main( )
{ int x;
cin>>x;
for( int y=0;y<10 br="" y="">cout<}
1.d)Write the output of the following program2
void main( )
{ int x=5,y=5;
cout<cout<<”,”;
cout<- -="" br="" x="">cout<<”,”;
cout<}
Ans: Output:
5,3,4,4
1.e)Write the output of the following program 3
#include
void X(int &A,int &B)
{ A=A+B;
B=A-B;
A=A-B;
}
void main( )
{ int a=4,b=18;
X(a,b);
cout<
}
Ans: Output:
18,4
f)Write a function called zero_Small() that has
two integer arguments being passed by
reference and sets the smaller of the two
numbers to 0. Write the main program to access
this function.4
#include
#include
void zero_Small(int &A,int &B)
{ if(AA=0;
else
B=0;
}
void main( )
{ clrscr();
int a,b;
cout<<”Enter any two values…”;
cin>>a>>b;
cout<<"Initial values of a and b are ";
cout<
zero_Small(a,b);
cout<";
cout<
cout<cout<<"\nPress any key to continue...";
getch();
}
2001:
1.b) Name the header file to be included for the
use of the following built in functions:
(i)getc( ) (ii)strcat() 1
Ans: (i) getc( ) - stdio.h
(ii)strcat( ) - string.h
1.e) Give the output of the following program:
#include

#include
int g=20;
void func(int &x,int y)
{ x=x-y;
y=x*10;
cout<}
void main( )
{ int g=7;
func(g,::g);
cout<func(::g,g);
cout<}
Ans: Output:
-13,-130
-13,20
33,330
-13,33
1.f) Write a function named SUMFIN( ), with
arguments x, N, which returns the sum of N
terms of the following series.: 4
x – x^3/3 + x^5/5 – x^7/7 + x^9/9
#include
#include
#include
double SUMFIN(int x1,int n1);
void main()
{ int x;
int n;
clrscr();
cout<<"Enter the vaue of X and N";
cin>>x>>n;
cout<<”\nThe sum of Series = “<getch();
}
double SUMFIN(int x1,int n1)
{ double sum=0;
int c=0;
for(int i=1;i<=(2*n1);i=i+2)
{ c=c+1;
if(c%2==1)
{ sum=sum+(pow(x1,i))/i;
} else
{ sum=sum-(pow(x1,i))/i;
}
}
return sum;
}
1.e)Give the output of the following program. 3
#include
#include
int g=20;
void func(int &x,int y)
{ x=x-y;
y=x*10;
cout<}
void main( )
{ int g=7;
func(g,::g);
cout<func(::g,g);
cout<}
2000:
1.b) Name the header file, to which following
built in function belong: 2
(i) isupper( ) ( ii)setw()
(iii)exp( ) (iv)strcmp( )
Ans) (i) isupper( ) - ctype.h
(ii)setw( ) - iomanip.h
(iii)exp( ) - math.h
(iv)strcmp( ) - string.h
1.c)Will the following program execute
successfully?If not, state the eason(s) 2
#include
void main( )
{ int s1,s2,num;
s1=s2=0;
for(x=0;x<11 br="" x="">{ cin<if(num>0)s1+=num;else s2=/num;
}
cout<}
Ans: The program will not execute successfully.
Because some syntax errors are there in the
program. They are
(i)cin and cout, stream objects used but
iostream.h header file is not included in the
program.
(ii)x is not declared, it should be declared as int.
(iii)With cin, we should use >> instead of <<.
(iv)The shorthand operator /=, is given wrongly
as =/.
So the corrected program is as follows:
#include
void main( )
{ int s1,s2,num;
s1=s2=0;
for(int x=0;x<11 br="" x="">{ cin>>num;
if(num>0)s1+=num;else s2/=num;
}
cout<}
d)Give the output of the following program
segment(Assuming all required header files are
included in the program): 2
char *NAME=”a ProFiLe”;
for(int x=0;xif(islower(NAME[x]))
NAME[x]=toupper(NAME[x]);

else if(isupper(NAME[x]))
if(x%2!=0)
NAME[x]=tolower(NAME[x-1]);
else
NAME[x]--;
cout<Ans: Output: AOROoIiE
1.e)Write the output of the following program3
#include
int func(int &x,int y=10)
{ if(x%y==0) return ++x;else return y- -;
}
void main( )
{ int p=20,q=23;
q=func(p,q);
cout<
p=func(q);
cout<
q=func(p);
cout<
}
Ans: Output: 2023
1023
1111
f) Write a function seqsum( ) in C++ with two
arguments, double x and int n. The function
should return a value of type double and it
should find the sum of the following series. 4
1+ x/2! + x2/4! + x3/6! + x4/8! + x5/10! + ----+ xn/
(2n)!
#include
#include
#include
double seqsum(int x1,int m1);
void main()
{ int x;
int m;
clrscr();
cout<<"Enter the vaue of X and M";
cin>>x>>m;
cout<<"\nThe sum of the series =
"<getch();
}
double seqsum(int x1,int m1)
{ double sum=1;
for(int i=1;i<=m1;i++)
{ int f=1;
for(int j=1;j<=2*i;j++)
{ f=f*j;
}
sum=sum+pow(x1,i)/f;
}
return sum;
}
1999:
1.a) Why main( ) function is so special. Give
two reasons? 1
Ans: Execution of the program starts and ends
at main( ). The main( ) is the driver function of
the program. If it is not present in a program,
no execution can take place.
1.b) Name the header file of C++ to which
following functions belong. (i)strcat( )
(ii) scanf( ) (iii) getchar( ) (iv)clrscr( )
Ans: (i)strcat( ) - string.h
(ii)scanf( ) - stdio.h
(iii)getchar( ) - stdio.h
(iv)clrscr( ) - conio.h
1.c) Find the syntax error(s), if any, in the
following program:
#include
main( )
{ int x[5],*y,z[5];
for(i=0;i<5 br="" i="">{ x[i]=i;
z[i]=i+3;
y=z;
x=y;
}
}
Ans (i) Line No 5: Undefined symbol ‘i’.
The variable ‘i’ is not declared in the program.
(ii)Line No 10:Assign the value of a pointer to
an integer variable. Ie error in x=y.
1.e) Write the output of the following program.
#include
static int i=100;
void abc( )
{ static int i=8;
cout<<”first =”<}
main( )
{ static int i=2;
abc( );
cout<<”second =”<}
Ans: Output:
First =8second =2
1.f) Write a C++ function that converts a 2-
digit octal number into binary number and prints
the binary equivalent.
#include
#include
void binary(int a)
//member function for conversion
{ int i,b[5]; //integer array 6
for(i=3;i>=1;i--)
{ b[i]=a%2;
a=a/2;
}
for(i=1;i<=3;i++)

cout<}
void main()
{ int n,x,y;
clrscr( );
cout<<"Enter a two digit octal number: ";
cin>>n;
x=n/10;
y=n%10;
binary(x);
binary(y);
getch( );
}
1998 :
1.b)Name the header files, to which the
following built in functions belongs to:
(i)cos( ) (ii)setw( )
(iii)toupper( ) (iv)strcpy( )
Ans: (i) cos( ) - math.h
(ii) setw( ) - iomanip.h
(iii) toupper( ) - ctype.h
(iv) strcpy( ) - string.h
1.c)Find the syntax error(s), if any, in the
following program:
include
void main( )
{ int R; W=90;
while W>60
{ R=W-50;
switch(W)
{ 20:cout<<”Lower Range”<30:cout<<”Middle Range “<40:cout<<”Higher Range”<}
}
}
Ans:
(i) Line 1: It should be,
#include
(ii) Line 4:Variables should be
separated using commas.
It should be int R,W=90;
(iii) Line 5:Test expression should be
in braces. It should be while
(W>60)
(iv) Line 10:It should be case 20;
(v) Line 11:It should be case 30;
(vi) Line 13:It should be case 40;
So the corrected version of the program is as
follows:
#include
void main( )
{ int R, W=90;
while (W>60)
{R=W-50;
switch(W)
{ case 20:cout<<”Lower Range”<case 30:cout<<”Middle Range “<case 40:cout<<”Higher Range”<}
}
}
1.d) Give the output of the following program
segment: char *NAME=”IntRAneT”;
for(int x=0;xif(islower(NAME[x])
NAME[x]=toupper(NAME[x]));
else if(isupper(NAME[x]))
if(x%2==0)
NAME[x]=tolower(NAME[x]);
else
NAME[x]=NAME[x-1];
puts(NAME);
Ans: Output: INTTaNEE
1.f) Write the output of the following program:
#include
void Execute(int &X,int Y=200)
{ int TEMP=X+Y;
X+=TEMP;
if(Y!=200)
cout<}
void main( )
{ int A=50,B=20;
Execute(B);
cout<
Execute(A,B);
cout<
}
1.f) Write a C++ function having two value
parameters X and N with result type float to find
the sum of series given below:
1 + x1/2! + x2/3! + x3/4! + x4/5! + - - - - - - xn/(n+1)!
#include
#include
#include
float sum_series(float X,int N) //function being
declared
{ float sum=0,term;
int fact,f;
sum+=1;
for(int i=1;i<=N;i++)
{ fact=1;
for(f=1;f<=(i+1);f++)
fact*=f;
term=pow(X,i)/fact;
sum+=term;
}
return(sum);
}
void main( )

{ clrscr( );
float x1;
int n1;
cout<<"\nEnter the value of X and N";
cin>>x1>>n1;
cout<<"\nThe Sum of the Series
..."<getch( );
}
Model Paper 1 for 2008-09 Batch:
Q1. (a) What is the difference between Global
Variable and Local Variable? 2
Answer:
Global Variable Local Variable
· It is a variable,
which is declared
outside all the
functions
· It is accessible
throughout the
program
· It is a variable,
which is declared
with in a function
or with in a
compound
statement
· It is accessible only
within a
function/compound
statement in which
it is declared
#include
float NUM=900; //NUM is a global variable
void LOCAL(int T)
{ int Total=0; //Total is a local variable
for (int I=0;ITotal+=I;
cout<}
void main()
{ LOCAL(45);
}
1.b)Write the names of the header files to which
the following belong: 1
(i) strcmp() (ii) fabs()
Answer: (i)string.h (ii) math.h
1(d) Find the output of the following
program: 3
#include
struct PLAY
{ int Score, Bonus;
};
void Calculate(PLAY &P, int N=10)
{ P.Score++;
P.Bonus+=N;
}
void main()
{ PLAY PL={10,15};
Calculate(PL,5);
cout<Calculate(PL);
cout<Calculate(PL,15);
cout<}
Answer:
11:20
12:30
13:45
1.e)Find the output of the following program 2
#include
#include
void Encrypt(char T[])
{ for (int i=0;T[i]!='\0';i+=2)
if (T[i]=='A' || T[i]=='E')
T[i]='#';
else if (islower(T[i]))
T[i]=toupper(T[i]);
else
T[i]='@';
}
void main()
{ char Text[]="SaVE EArtH";
//The two words in the string Textare separated
by single space
Encrypt(Text);
cout<}
Answer: @a@E@E#rTH
1.f)In the following program, if the value of N
given by the user is 15, what maximum and
minimum values the program could possibly
display? 2
#include
#include
void main()
{ int N,Guessme;
randomize();
cin>>N;
Guessme=random(N)+10;
cout<}
Answer:M aximum Value:24 Minimum
Value:10
Model Paper 2 for 2008-09 Batch:
1.b)Write the names of the header files to which
the following belong: 1
(i) frexp() (ii) isalnum()
Answer:
(i) math.h (ii) ctype.h
1.d)Find the output of the following program3
#include

void Changethecontent(int Arr[], int Count)
{
for (int C=1;CArr[C-1]+=Arr[C];
}
void main()
{
int A[ ]={3,4,5},B[ ]={10,20,30,40},C[ ]={900,1200};
Changethecontent(A,3);
Changethecontent(B,4);
Changethecontent(C,2);
for (int L=0;L<3 br="">cout<
cout<for (L=0;L<4 br="">cout<cout<for (L=0;L<2 br="">cout<}
Answer:
7#9#5#
30#50#70#40#
2100#1200#
1.(e)Find the output of the following program:2
#include
struct Game
{ char Magic[20];int Score;
};
void main()
{ Game M={“Tiger”,500};
char *Choice;
Choice=M.Magic;
Choice[4]=’P’;
Choice[2]=’L’;
M.Score+=50;
cout<Game N=M;
N.Magic[0]=’A’;N.Magic[3]=’J’;
N.Score-=120;
cout<}
Answer:
TiLeP550
AiLJP430
1.f)In the following program, if the value of N
given by the user is 20, what maximum and
minimum values the program could possibly
display? 2
#include
#include
void main()
{ int N,Guessnum;
randomize();
cin>>N;
Guessnum=random(N-10)+10;
cout<}
Answer:M aximum Value:19 Minimum
Value:10
Sample Paper 1 for 2009-10:
1.a) What is the difference between Global
Variable and Local Variable? Also, give
a suitable C++ code to illustrate both. 2
Ans) Global Variable:
* It is a variable which is declared outside all
the functions
* It is accessible throughout the program
Local Variable:
* It is a variable which is declared with in a
function or with in a compound statement
* It is accessible only within a function/
compound statement in which it is declared
#include
float NUM=900; //NUM is a global variable
void LOCAL(int T)
{
int Total=0; //Total is a local variable
for (int I=0;ITotal+=I;
cout<}
void main()
{
LOCAL(45);
}
1.b) Which C++ header file(s) will be
essentially required to be included to run
/execute the following C++ code: 1
void main()
{
char Msg[ ]="Sunset Gardens";
for (int I=5;Iputs(Msg);
}
A) (i) string.h (ii) stdio.h
1. d) Find the output of the following program:3
#include
struct GAME
{ int Score, Bonus;
};
void Play(GAME &g, int N=10)
{
g.Score++;
g.Bonus+=N;
}
void main()
{

GAME G={110,50};
Play(G,10);
cout<Play(G);
cout<Play(G,15);
cout<}
A) 111:60 3
112:70
113:85
e) Find the output of the following program: 2
#include
void Secret(char Str[ ])
{
for (int L=0;Str[L]!='\0';L++);
for (int C=0;Cif (Str[C]=='A' || Str[C]=='E')
Str[C]='#';
else
{
char Temp=Str[C];
Str[C]=Str[L-C-1];
Str[L-C-1]=Temp;
}
}
void main()
{
char Message[ ]="ArabSagar";
Secret(Message);
cout<}
A) #agaSbarr
f) In the following program, if the value of
Guess entered by the user is 65, what will be the
expected output(s) from the following options
(i), (ii), (iii) and (iv)? 2
#include
#include
void main()
{
int Guess;
randomize();
cin>>Guess;
for (int I=1;I<=4;I++)
{
New=Guess+random(I);
cout<<(char)New;
}
} (
i) ABBC
(ii) ACBA
(iii) BCDA
(iv) CABD
A)
(i) ABBC
Sample Paper 2 for 2009-10 Batch:
1.(a) What is the difference between Actual
Parameter and Formal Parameters? Also, give a
suitable C++ code to illustrate both. 2
A) Actual Parameter Formal Parameter 2
* It is a parameter, which is used in function
call to send the value from calling environment
* It is a parameter, which is used in function
header, to receive the value
from actual parameter
#include
void Calc(int T) //T is formal parameter
{
cout<<5 br="">}
void main()
{ int A=45;
Calc(A);//A is actual parameter
}
(b) Write the names of the header files to which
the following belong: 1
(i) frexp() (ii) isalnum()
A) (i) math.h (ii) ctype.h
(c) Rewrite the following program after
removing the syntactical errors (if any).
Underline each correction. 2
#include
struct Pixels
{ int Color,Style;}
void ShowPoint(Pixels P)
{ cout<
void main()
{
Pixels Point1=(5,3);
ShowPoint(Point1);
Pixels Point2=Point1;
Color.Point1+=2;
ShowPoint(Point2);
}
Ans:
#include 2
struct Pixels
{
int Color,Style;
};
void ShowPoint(Pixels P)
{
cout<
}
void main()
{
Pixels Point1={5,3};
ShowPoint(Point1);
Pixels Point2=Point1;

Point1.Color+=2;
ShowPoint(Point2);
}
(d) Find the output of the following program: 3
#include
void Changethecontent(int Arr[ ], int Count)
{
for (int C=1;CArr[C-1]+=Arr[C];
}
void main()
{
int A[]={3,4,5},B[]={10,20,30,40},C[]={900,1200};
Changethecontent(A,3);
Changethecontent(B,4);
Changethecontent(C,2);
for (int L=0;L<3 br="" cout="">cout<for (L=0;L<4 br="" cout="">cout<for (L=0;L<2 br="" cout="">}
Ans:
7#9#5#
30#50#70#40#
2100#1200#
(e) Find the output of the following program: 2
#include
struct Game
{c
har Magic[20];int Score;
};
void main()
{
Game M={"Tiger",500};
char *Choice;
Choice=M.Magic;
Choice[4]='P';
Choice[2]='L';
M.Score+=50;
cout<Game N=M;
N.Magic[0]='A';N.Magic[3]='J';
N.Score-=120;
cout<}
A)
TiLeP550
AiLJP430
(f) In the following program, if the value of N
given by the user is 20, what maximum and
minimum values the program could possibly
display? 2
#include
#include
void main()
{i
nt N,Guessnum;
randomize();
cin>>N;
Guessnum=random(N-10)+10;
cout<}
A)
Maximum Value: 19 Minimum Value: 10
Sample Paper 1 for 2011-12:
1.a)Differentiate between the post increment
and pre increment operators. Also, give a
suitable C++ code to illustrate both. 2
Post Increment: ++ is an increment operator to
increment the value of a variable by one. When
used after the operand it is known as post
increment operator.
Pre Increment: When ++ is used before an
operand to increment its value by one, it is
called a pre-increment operator.
Example:
#include
void main( )
{
int NUM=9;
cout<<++NUM; //10 will be displayed
cout<cout<}
b) Which C++ header file(s) will be essentially
required to be included to run/execute the
following C++ code: 1
void main( )
{
int Last=25;
for(int C=9;C<=Last;C++)
cout<}
Ans:
(i)iostream.h (for cout)
(ii) math.h (for sqrt( ))
c) Find the output of the following program:
#include
struct STOCK
{
Int Ino,Qty;
};
void Buy(STOCK &I,int TQ=2)
{I
.Qty+=TQ;
}
void main( )
{
STOCK i[2]

Header files – Important Functions.
From Old Papers
Name the header files that shall be needed for
the following
2009 Outside Delhi:
(i) setw( ) – iomanip.h
(ii) sqrt( ) – math.h
2008 Delhi & OD:
Due to cout, include iostream.h
Due to setw( ), include iomanip.h
2007 Delhi & OD:
Due to strcat( ), include string.h
Due to gets( ), include stdio.h
2006 Delhi:
(i) abs( ) - math.h, stdlib.h
(ii) isupper( ) – ctype.h
2006 Outside Delhi
(i) abs( ) - math.h, stdlib.h
(ii) random( ) - stdlib.h
2005 Delhi:
(i) abs( ) - stdlib.h, math.h
(ii) strcmp( ) - string.h
2005 OD:
(i) puts( ) - stdio.h
(ii) isalnum( ) - ctype.h
2004 :
(i) gets( ) - stdio.h
(ii) strcmp( ) - string.h
(iii) abs( ) - math.h, stdlib.h
(iv) isalnum( ) - ctype.h
2003:
(i) strcmp( ) - string.h
(ii) getc( ) - stdio.h
2002:
(i) get( ) - iostream.h
(ii) open( ) - fstream.h
(iii) abs( ) - math.h, stdlib.h
(iv) strcat( ) - string.h
2001:
(i) getc( ) - stdio.h
(ii) strcat( ) - string.h
2000:
(i) isupper( ) - ctype.h
(ii) setw( ) - iomanip.h
(iii) exp( ) - math.h
(iv) strcmp( ) - string.h
1999:
(i) strcat( ) - string.h
(ii) scanf( ) - stdio.h
(iii) getchar( ) - stdio.h
(iv) clrscr( ) - conio.h
1998:
(i) cos( ) - math.h
(ii) setw( ) - iomanip.h
(iii) toupper( ) - ctype.h
(iv) strcpy( ) - string.h
Model Paper 1:
(i) strcmp( ) – string.h
(ii) fabs( ) – math.h
Model Paper 2:
(i) frexp( ) – math.h
(ii) isalnum( ) – ctype.h
ctype.h (Character functions)
isalnum( ), isalpha( ), isdigit( ), islower( ),
isupper( ), tolower( ), toupper( )
string.h (Generally starts with str)
strcat( ), strcmp( ), strcpy( ), strlent( ), strchr( ),
stricmp( ), strlwr( ), strew( ), strupr( )
iomanip.h
setw( ), setprecision( ), endl, flush( ).
stdlib.h
abs( ), labs( ), free( ), random( ), atof( ), atoi( ),
atol( ),strtol( ), strtod( ), calloc( ),malloc(),
realloc( )
iostream.h
(cout,cin – these are streams available in
iostream.h)
get( ) getline( ) read( ) write( ) put( )
open( ) close( ) flush( )
seekg( ) seekp( ) tellg( ) tellp( )
bad( ) eof( ) fail( ) good( )
clear( )
stdio.h (Many of the functions starting with f)
printf( ) scanf( )
fflush( ) fgetc( ) fgetchar( ) fgets( ) fopen( )
fprintf( ) fputc( ) fputchar( )
fputs( ) fread( ) freopen( ) fscanf( ) fseek( )
fsetpos( ) fwrite( ) ftell( )
fwrite( ) getc( ) getchar( ) gets( ) getw( )
putc( ) putchar( ) puts( ) putw( )
remove( ) rename( )
conio.h
clrscr( ) getch( ) gotoxy( ) cprintf( )
dos.h
sound( ) nosound( ) delay( )
process.h
exit(0)
math.h
acos( ) acosl( ), etc, etc div( ) exp( ) ceil( )
ceill( ) fabs( ) floor( ) fmod( ) log( )
pow( ) modf( ) poly( ) sqrt( )

” If WEALTH is lost, nothing is lost.
If HEALTH is lost, something is lost.
If CHARACTER is lost, everything is lost.”
XII Computer Science
Chapter: C++ Revision Tour
Important Models
1.a) Name the header file to which the
following below: (i) abs( ) (ii)
isupper( ) 1
Ans) (i) abs( ) - math.h, stdlib.h, complex.h
(ii)isupper( ) - ctype.h
2) Name the header file(s) that shall be needed
for successful compilation of the following C++
code. 1
void main( )
{ char String[20];
gets(String);
strcat(String,”CBSE”);
puts(String);
}
Ans) stdio.h string.h
3) In the following program, find the correct
possible output(s) from the options: 2
#include
#include
void main( )
{ randomize( );
char Area[ ][10]
={“NORTH”,”SOUTH”,”EAST”,”WEST”};
int ToGo;
for(int I=0; I<3 br="">{
ToGo=random(2) + 1;
cout<}
}
Ans: Outputs:
(i) SOUTH : EAST : SOUTH :
(ii) NORTH : SOUTH : EAST :
(iii) SOUTH : EAST : WEST :
(iv) SOUTH : EAST : EAST :
4) Find the output of the following program.2
#include
#include
#include
void Change(char Msg[],int Len)
{ for(int Count=0;Count{ if(islower(Msg[Count]))
Msg[Count] = toupper(Msg[Count]);
else if(isupper(Msg[Count]))
Msg[Count] = tolower(Msg[Count]);
else if (isdigit(Msg[Count]))
Msg[Count]=Msg[Count]+1;
else Msg[Count] = ‘*’;
}
}
void main( )
{ char Message[ ]=”2005 Tests ahead”;
int Size=strlen(Message);
Change(Message,Size);
cout<for(int C=0,R=Size – 1; C<=Size/2;C++,R--)
{ char Temp=Message[C];
Message[C]=Message[R];
Message[R]=Temp; }
cout<}
Ans: Output:
3116*tESTS*AHEAD
DAEHA*SSTEt*6113
5) Find the output of the following program 2
#include
void main( )
{ long NUM=1234543;
int F=0,S=0;
do
{ int R=NUM % 10;
if (R %2 != 0)
F += R;
else
S += R;
NUM / = 10;
} while (NUM>0);
cout<}
Ans: Output: 2
6)Write the output of the following program 3
#include
void X(int &A,int &B)
{ A=A+B;
B=A-B;
A=A-B;
}
void main( )
{ int a=4,b=18;
X(a,b);
cout<
}
Ans: Output:
18,4
7) Give the output of the following program:
#include
struct Pixel
{ int C,R;

};
void Display(Pixel P)
{
cout<<”col”<
dl;
}
void main( )
{
Pixel X={40,50},Y,Z;
Z=X;
X.C+=10;
Y=X;
Y.R+=20;
Z.C-=15;
Display(X);
Display(Y);
Display(Z);
}
8) Find the output of the following program: 2
#include
void main( )
{ int U=10,V=20;
for(int I=1;I<=2;I++)
{ cout<<”[1]”<cout<<”[2]”<<++V<<”&”<}
} Ans: Output:
[1]10&15
[2]21&13
[1]11&16
[2]22&14
9) Rewrite the following program after
removing the syntactical error(s), if any.
Underline each correction. 2
#include
const int dividor 5;
void main( )
{ Number = 15;
for(int Count=1;Count=<5 -="3)<br" ount="" umber="">if(Number % dividor = = 0)
cout<cout<else
cout<Ans:
#include
const int dividor= 5;
void main( )
{ int Number = 15;
for(int Count=1;Count<=5;Count++,Number -= 3)
if(Number % dividor = = 0)
{ cout<cout<}
else
cout<}
10) Find the syntax error(s), if any, in the
following program:
#include
main( )
{ int x[5],*y,z[5];
for(i=0;i<5 br="" i="">{ x[i]=i;
z[i]=i+3;
y=z;
x=y;
}
}
Ans (i) Line No 5: Undefined symbol ‘i’.
The variable ‘i’ is not declared in the program.
(ii)Line No 10:Assign the value of a pointer to
an integer variable. Ie error in x=y.
11)Rewrite the following program after
removing the syntactical error(s)if any.
Underline each correction. 2
#include
void main( )
{ First = 10, Second = 20;
Jumpto(First;Second);
Jumpto(Second);
}
void Jumpto(int N1, int N2 = 20)
{ N1=N1+N2;
count<>N2;
}
Ans)#include
void Jumpto(int N1,int N2=20);
//Prototype missing
void main( )
{ int First = 10, Second = 20;
//Data type missing
Jumpto(First,Second);
//Comma to come instead of ;
Jumpto(Second);
}
void Jumpto(int N1, int N2)
{ N1=N1+N2;
cout<//Output operator << required
}
12) Rewrite the following program after
removing the syntactical error(s), if any.
Underline each correction. 2
#include
void main( )
{ struct movie
{ char movie_name[20];
char movie_type;
int ticket_cost=100;
}MOVIE;
gets(movie_name);
gets(movie_type);
}

Ans:#include
#include
void main( )
{ struct movie
{ char movie_name[20];
char movie_type;
int ticket_cost;
//Initialization of variables inside a structure is not allowed.
}MOVIE;
gets(MOVIE.movie_name);
cin>>MOVIE.movie_type;
//A single character cannot be read using gets
}
13)Write definition for a function
SumSequence( ) in C++ with two arguments/
parameters – double X and int n. The function
should return a value of type double and it
should perform sum of the following series.
1/x- 3!/x2 + 5!/x3 – 7!/x4 + 9!/x5 - ------upto n
terms.
Note: The symbol ! represents Factorial of a
number
ie 5!= 1 X 2 X 3 X 4 X 5.
#include
#include
#include
double SumSequence(int x1,int n1);
void main()
{ int x;
int n;
clrscr();
cout<<"Enter the vaue of X and N";
cin>>x>>n;
cout<<”\nThe sum of the series =
“<getch();
}
double SumSequence(int x1,int n1)
{ double sum=0;
int c=0;
for(int i=1;i<=(2*n1);i=i+2)
{ int f=1;
for(int j=1;j<=i;j++)
{ f=f*j;
}
c=c+1;
if(c%2==1)
{ sum=sum+f/(pow(x1,c));
}
else
{ sum=sum-f/(pow(x1,c));
}
}
return sum;
}
Some Important Theorey Questions:
1. Differentiate between call by value and call
by reference with example. (whole concept is
imp)
2. Type Convertion.
3. Diff between various types of errors.
4. Diff between #define and const
5. Typedef keyword
6. Nested Structures.
7. Global variables and local variables.
8. Importance of main().
(Theory question we cannot guess. It can be
on any of the topic covered in XI C++
Revision Tour)
XII Computer Science
TEST - C++ Revision Tour
1. Name the header file to which the following
below: (i) isupper( ) (ii) setw( )
2) Name the header file(s) that shall be needed
for successful compilation of the following C++
code.
void main( )
{ char<< word[ ]=”Exam”;
cout<}
3) In the following program, find the correct
possible output(s) from the options:
#include
#include
const int MAX=20;
void main( )
{ randomize( );
int SCORE=8,Num;
for(int i=2;i<=5;i++)
{ Num=MAX+random(SCORE);
cout<SCORE--;
}
}
(i) 24:24:25:25:
(ii) 24:24:25:24:
(iii) 26:25:24:23:
(iv) 24:24:25:24:23
4) Find the output of the following program
#include
#include
#include
void MyCode(char Msg[],char CH)
{ for (int Cnt=0;Msg[Cnt]!='\0';Cnt++)
{
if (Msg[Cnt]>='B'&&Msg[Cnt]<='G')
Msg[Cnt]=tolower(Msg[Cnt]);
else
if (Msg[Cnt]=='A'|| Msg[Cnt]=='a')
Msg[Cnt]=CH;
else
if (Cnt%2==0)
Msg[Cnt]=toupper(Msg[Cnt]);
else
Msg[Cnt]=Msg[Cnt+2];
}

}
void main ()
{ clrscr();
cout<char MyText[] ="Jawahar Navodaya Vidyalaya";
MyCode(MyText,'@');
cout<<"MY TEXT:"<}
5)Write the output of the following program
#include
void Withdef(int HisNum=22)
{ for(int I=20;I<=HisNum;I+=7)
cout<cout<}
void Control(int &MyNum)
{ MyNum+=8;
Withdef(MyNum);
}
void main()
{ int YourNum=20;
Control(YourNum);
Withdef( );
cout<<”Number=”<}
6) Give the output of the following program:
#include
struct Package
{ int Length,Breadth,Height;
};
void Occupies(Package M)
{ cout<x”;
cout<}
void main( )
{ Package P1={5,10,15},P2,P3;
++P1.Height;
Occupies(P1);
P3=P1;
++P3.Breadth;
P3.Height++;
Occupies(P3);
P2=P3;
P2.Length+=25;
--P2.Length;
Occupies(P2);
}
7. Find the output of the following program
#include
struct Game
{char Magic[20];int Score;
};
void main()
{ Game M={"Tiger",500};
char *Choice;
Choice=M.Magic;
Choice[4]='P';
Choice[2]='L';
M.Score+=50;
cout<Game N=M;
N.Magic[0]='A';N.Magic[3]='J';
N.Score-=120;
cout<}
8) Find the output of the following program:
#include
void main( )
{ int A=7,B=9;
for(int I=10;I<=11;I++)
{ cout<<”Line1”<<++A<<”&”<cout<<”Line2”<}
}
9)Rewrite the following program after removing
the syntactical error(s) if any. Underline each
correction.
include
void main( )
{ First = 10; Second = 20;
Jumpto(First;Second);
Jumpto(Second);
}
void Jumpto(int N1, int N2 = 20)
{ N1=N1+N2;
count<>N2;
}
10) Rewrite the following program after
removing the syntactical error(s), if any.
Underline each correction.
#include
void main( )
{ struct movie
{ char movie_name[20];
char movie_type;
int ticket_cost=100;
}MOVIE;
gets(movie_name);
gets(movie_type); }
11) Write the output for the following program.
#include
#include
int g=20;
void func(int &x,int y)
{ x=x-y;
y=x*10;
cout<}
void main( )
{ int g=7;
func(g,::g);
cout<func(::g,g);
cout<}
12) Write the output of the following program.
#include
static int i=100;

void abc( )
{ static int i=8;
cout<<”first =”<}
main( )
{ static int i=2;
abc( );
cout<<”second =”<}
13.What are Nested Structures? Give an example.
14.Differentiate between call by value and call
by reference.
15. Explain type convertion.