CBSE eBooks

CBSE Guess > eBooks > Class XII > Computer Science By . Mr. MRK

Chapter – 1 C++ Revision Tour

Previous Index Next

2004 Annual Paper:

1.b. Write the names of the header files to which the following belong: (i) gets( ) (ii) strcmp( ) (iii)abs( ) (iv)isalnum( )

Ans:

  1. gets( ) - stdio.h
  2. strcmp( ) - string.h
  3. abs( ) - math.h, stdlib.h,complex.h
  4. isalnum( ) - ctype.h

1.e. What will be the output of the following program

#include<iostream.h>
void main( )
{
int var1=5,var2=10;
for(int i=1,i<=2;i++)
{
cout<<var1++<<’\t’<< - - var2<<endl;
cout<<var2- -<<’\t’<<+ + var1<<endl;
}
}

Ans: Output:

5

9

9 7
7 7
7 9

1. 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!/x + 5!/x – 7!/x + 9!/x - ------upto n terms. 2 3 4 5
Note: The symbol ! represents Factorial of a number ie 5!= 1 X 2 X 3 X 4 X 5.

#include<iostream.h>
#include<math.h>

#include<conio.h>
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 = “<<SumSequence(x,n);
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;
}

2003 Annual Paper:

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<iostream.h>
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<iostream.h>
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<<endl;break;
<<P[I]+Q;
}
}

Ans:

# include<iostream.h>
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<<endl; break;
case 1:
case 2: cout<<P[I]+Q;
}
}

1.e. Write the output of the following program:

#include<iostream.h>
int Execute(int M)
{
if(M%3==0) return M*3;
else return M+10;
}
void Output(int B=2)
{
for(int T=0;T<B;T++)
cout<<Execute(T)<<”*”;
cout<<endl;
}
void main( )
Output(4);
Output( );
Output(3);
}

Ans:

 

Previous Index Next