TERM - II EXAMINATION: 2015 -16

COMPUTER SCIENCE (083)

Class : XI SET – B

Max. Marks : 70 Duration: 3 hrs.

(i) All questions are compulsory

(ii) Programming Language: C++

1. Write the programming code for the following: (15)

a)  To display 1-D array in descending order -3-

b)  To check equality of two 2D matrices. -4-

c)  Consider a 1-D array ”a” containing 10 integers.

Develop a program to do the following : -5-

i.  Remove all occurrences of a given integer

ii.  Shift the elements of the array to the right so that used space is available at the left end

iii.  Fill the unused spaces by 0(zero).

d)  Read a string and display number of words, spaces and alphabets present in it. -3-

2. Find the syntax errors(s), if any, underline it and do the corrections (10)

a)  void large(int &a,int b) -2-

int main()

{large(a,b);}

void large(int &a,int &b)

{ if (a>b)

a=-1;

else

b=-1;}

b)  include<iostream.h> -3-

void main()

{

int r;w=90;

while w>60

{r=w-50;

switch(w)

{20: cout<”lower range”<endl;

30: cout<”middle range”<endl;

20: cout<”higher range”<endl;

}}}

c)  #include<stdio.h> -2-

void main()

{ int s1,s2,num;

for(x=0;x<11;x++)

{cin<num;

if(num>0)s1+=num; else s2=/num;

}

cout>s1>s2;}

d)  #include<iostream.h>

static int x 20; -3-

void main()

{ int num[max];

num={20,50,10,30,40};

for(loc=max-1;loc>=0;loc--)

cout>num[loc];

}

3. Answer the following: (15)

a) convert for loop into while loop and if statement into switch case, rewrite the code: -4-

for(j=0;j<noofkids;j++)

{

if kilometer==1000)

totalfare+=500/2;

else

if kilometer==500)

totalfare+=300/2;

else

totalfare+=200/2;

}

b) What are the functions of the following header files: -3-

iomanip.h string.h math.h

c)Define a function convert() to convert Celsius to Fahrenheit temperature. -2-

d)Name the header files(s) that shall be needed for successful compilation of the following code: -1-

void main()

{ char string[25];

gets(string);

strcat(string,”nms”);

puts(string);

}

e)  If the ages of Ram, Sulabh and Ajay are input by the user, write a program to determine the youngest of the three. -2-

f)  Define a function prime() to check a given number is prime or not. -3-

4.  Write the difference between (3 X 3 = 9)

a)  Call by Value and Call by reference

b)  While loop and Do…While loop

c)  Logical error and syntax error. Also give suitable examples of each in c++.

5.  What will be the output of the following program? (5 x 3 =15)

a)  # include<iostream.h>

int a=10;

void func(int &x,int y, int *z)

{ a+=x; Y*=a; *z=a+y;

cout<’\n’<x<’\t’<y<’\t’<*z<endl;

}

void main()

{

void func(int &,int , int *);

int a=40, b=10;

func(::a,a,&b);

cout<’\n’<::a<’\t’<a<’\t’<b<endl;

}

b)  #include<iostream.h>

int recs(int);

void main()

{int r;

for (int i=0;i<5;i++)

{r=recs(i);

cout<”\n”<i<”\t”<r;

} }

int recs(int num)

{if (num==0) return 0;

else

return (num+recs(num-1));

}

c)  #include<iostream.h>

#include<ctype.h>

#include<string.h>

void main()

{char c[]="NMS-DPS 2015 IntRAneT";

for(int i=0;i<strlen(c);i++)

if (isupper(c[i]))

if(i%2==0)

c[i]=tolower(c[i]);

else

if ( islower(c[i]))

c[i]=toupper(c[i])-2;

else c[i]='@';

cout<"Text= "<c<endl;}

d)  #include<iostream.h>

#include<conio.h>

void sumfn(int last)

{int sum=0;

static int sum2=0;

for(int i=last;i>0;i-=10) sum+=i;

sum2+=sum;

cout<sum<””<sum2<endl;

}

void main()

{

for(int i=10;i<60;i+=10)sumfn(i);

}

e) # include<iostream.h>

int execute(int &b,int c=10)

{ if(b%c==0) return ++b;

else

return c--;

}

void main()

{int m=20,n=23;

n=execute(m ,n);

cout<m<n<endl;

m=execute(n );

cout<m<n<endl;

n=execute(m );

cout<m<n<endl;

}

6. Define the following with suitable example: (3 x 2 =6)

a) Pinter b) Array c) Reference

TERM - II EXAMINATION: 2015 -16 (Answer Key)

COMPUTER SCIENCE (083)

Class : XI SET – A

Max. Marks : 70 Duration: 3 hrs.

(i) All questions are compulsory

(ii) Programming Language: C++

1.  Write the difference between (3 X 3 = 9)

d)  Call by Value and Call by reference

Ans: In call by value method, the called function creates its own copies of the original values sent to it. Any changes, that are made, occur on the called function’s copy of values and are not reflected back to the calling function.

In call by reference method, the called function accesses and works with the original values using their references. Any changes, that changes, that occurs, take place on the original values and are reflected back to the calling code.

Example :

void fun(int x, int &y)

{

x=x+2;

y=y+2;

cout<x<y<endl; // 5 6

}

void main()

{ int a=3, b=4;

fun(a,b);

cout<a<b<endl; //3 6, 6 bcs reference

}

e)  While loop and Do…While loop

Ans : While loop checks for the condition first. so it may not even enter into the loop, if the condition is false.

do while loop, execute the statements in the loop first before checks for the condition. At least one iteration takes places, even if the condition is false.

Example :

int a=0
do{
cout<"i will run once";
}
while(a==2);
output is i will run once .
while(a==2)
{
cout<"i will run once";
}
output is nothing , loop the program did not enter the loop .

f)  Logical error and syntax error. Also give suitable examples of each in c++.

Ans : Syntax errors occur when a program does not conform to the grammar of a programming language, and the compiler cannot compile the source file. Logic errors occur when a program does not do what the programmer expects it to do.

Syntax errors are usually easy to fix because the compiler will tell you where the error occurs and you simply fix the syntax error. For example you may miss a semicolon or a curly bracket where it’s supposed to be. Simply locate those errors and fix them.

(1/2 mark for definition , ½ for example )

2.  What will be the output of the following program? (5 x 3 =15)

e)  # include<iostream.h>

int a=10;

void func(int &x,int y, int *z)

{ a+=x;

Y*=a;

*z=a+y;

cout<’\n’<x<’\t’<y<’\t’<*z<endl;

}

void main()

{

void func(int &,int , int *);

int a=40, b=10;

func(::a,a,&b);

cout<’\n’<::a<’\t’<a<’\t’<b<endl;

}

Ans : 40 800 820

20 40 820

f)  #include<iostream.h>

int recs(int);

void main()

{

Int r;

For (int i=0;i<5;i++)

{

R=recs(i);

Cout<”\n”<i<”\t”<r;

}

}

Int recs(int num)

{

If (num==0)

Return 0;

Else

Return (num+recs(num-1));

}

Ans :

0  0

1  1

2  3

3  6

4  10

g)  #include<iostream.h>

#include<ctype.h>

#include<string.h>

void main()

{char c[]="NMS-DPS 2015 IntRAneT";

for(int i=0;i<strlen(c);i++)

if (isupper(c[i]))

if(i%2==0)

c[i]=tolower(c[i]);

else

if ( islower(c[i]))

c[i]=toupper(c[i])-2;

else c[i]='@';

cout<"Text= "<c<endl;}

Ans: Text= n@s-d@s 2015 @ntr@net

h)  #include<iostream.h>

#include<conio.h>

void sumfn(int last)

{

int sum=0;

static int sum2=0;

for(int i=last;i>0;i-=10) sum+=i;

sum2+=sum;

cout<sum<””<sum2<endl;

}

void main()

{

for(int i=10;i<60;i+=10)sumfn(i);

}

Ans:

10 10

30 40

60 100

100 200

150 350

e) # include<iostream.h>

int execute(int &b,int c=10)

{ if(b%c==0) return ++b;

else

return c--;

}

void main()

{

int m=20,n=23;

n=execute(m ,n);

cout<m<n<endl;

m=execute(n );

cout<m<n<endl;

n=execute(m );

cout<m<n<endl;

}

Ans :

20 23

10 23

11 11

(2 for correct output, 1 marks for processing, any two output data is wrong -1/2)

3. Define the following with suitable example: (3 x 2 =6)

a) Pinter b) Array c) Reference

Ans :

a)  Pointer Variables (or Pointers)

A pointer variable (or pointer in short) is basically the same as the other variables, which can store a piece of data. Unlike normal variable which stores a value (such as an int, a double, a char), a pointer stores a memory address.

The syntax of declaring a pointer is to place a * in front of the name.

Example: int *p1, *p2, i; // p1 and p2 are int pointers. i is an int

char *name; // name is character pointer

b)  an array, also known as a vector or list (for one-dimensional arrays) or a matrix (for two-dimensional arrays), is one of the simplest data structures. Arrays hold a series of data elements, usually of the same size and data type.

Example int a[20];

c)  Pointers via the Address-Of Operator (&)

The address-of operator (&) operates on a variable, and returns the address of the variable. For example, if number is an int variable, &number returns the address of the variable number.

Example 1:

Example 2:

int A = 5;
int& rA = A;// whatever will change in rA it will reflect to A and vice versa

(1 mark defining , 1 mark example)

4. Write the programming code for the following: (15)

e)  To display 1-D array in descending order -3-

ans :

void main()

{

int a[20],i,n;

cin>n;

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

for(j=0;j<n;j++)

if(a[j]<a[i])

{

b=a[i];

a[i]=a[j];

a[j]=b;

}

cout<"\n the array in descending order"<endl;

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

cout<a[i]<'\t'; }

(1 ½ for for loop, 1 ½ for swapping for descending order)

f)  To check equality of two 2D matrices. -4-

Ans : #include<iostream.h>

#include<conio.h>

void main()

{clrscr();

int a[5][5],b[5][5],i,j,k,r1,c1,r2,c2,flag=0;

cout<"\n Equality of 2 2D Arrays";

cout<"\n Enter size of Array 1(max 5x5)";

cin>r1>c1;

cout<"\n Enter size of Array 2(max 5x5)";

cin>r2>c2;

if(r1==r2||c1==c2) // check of marix can be add or subtract

{cout<"\n Enter Array 1 ";

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

for(j=0;j<c1;j++)

cin>a[i][j];

cout<"\n Enter Array 2 ";

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

for(j=0;j<c2;j++)

cin>b[i][j];

cout<"\n Your Array 1 is";

for(i=0;i<r1;i++){cout<'\n';

for(j=0;j<c1;j++)

cout<a[i][j]<'\t';}

cout<"\n Your Array 2 is";

for(i=0;i<r2;i++){cout<'\n';

for(j=0;j<c2;j++)

cout<b[i][j]<'\t';}

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

{

for(j=0;j<c1;j++)

if(a[i][j]==b[k][j])

flag=1; }

if(flag==1)

cout<"\n Both MArices are equal";

getch();

}

g)  Consider a 1-D array ”a” containing 10 integers. Develop a program to do the following : -5-

iv.  Remove all occurrences of a given integer

v.  Shift the elements of the array to the right so that used space is available at the left end

vi.  Fill the unused spaces by 0(zero).

Ans: #include<iostream.h>

void main()

{

int a[10],i,j,pos,num;

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

cin>a[i];

cout<”\n read which number to be remove”;

cin>num;

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

{ if(a[i]==num)

{

pos=i;

j=pos;

for(;j>=0;j--)

a[j]=a[j-1];

a[0]=0;

}

}

if(i>=10)

{cout<”\n sorry !!! the element was not found”;

exit(0);

}

cout<”\n the array is”;

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

cout<a[i]<endl;

}

(1 ½ for reading of an array, 2 marks for equalizing, ½ for other statements)

h)  Read a string and display number of words, spaces and alphabets present in it. -3-

Ans :

n=strlen(str)

for( int i=0;i<n;i++)

{ if(str[i]==” “)

s++;

if(isaplha(str[i])

a++; }

cout<”number of spaces - “<s;

cout<”number of words = “ < s+1;

cout<”number of alphabets = “ < a;

(1 mark for for loop, 1 mark for checking , 1 mark for display)

5. Find the syntax errors(s), if any, underline it and do the corrections (10)

g)  void large(int &a,int b) -2-

int main()

{large(a,b);

}

void large(int &a,int &b)

{ if (a>b)

a=-1;

else

b=-1;

}

Ans: error1: void large(int &a,int b) correction: void large(int &a,int &b);

Error 2: void large(int &a,int &b) correction: void large(int &a,int b)

Error 3: large(a,b); correction : int a,b;

Error 4: int main() correction : return 0;

(1/2 for each error and correction)

h)  include<iostream.h> -3-

void main()

{

int r;w=90;

while w>60

{

r=w-50;

switch(w)

{

20: cout<”lower range”<endl;

30: cout<”middle range”<endl;

20: cout<”higher range”<endl;

}}}

Ans: error1: include<iostream.h> correction: #include<iostream.h>

Error 2: r;w=90 correction: r,w=90;

Error 3: while w>60 correction : while (w>60)

Error 4: 20: cout<”lower range”<endl;

correction : case 20: cout<”lower range”<endl;

Error 5: 30: cout<”lower range”<endl;

correction : case 30: cout<”lower range”<endl;

Error 6: 40: cout<”lower range”<endl;

correction : case 40: cout<”lower range”<endl;

(1/2 for each error and correction)

i)  #include<stdio.h> -2-

void main()

{ int s1,s2,num;

for(x=0;x<11;x++)

{

cin<num;

if(num>0)s1+=num; else s2=/num;

}

cout>s1>s2;

}

Ans: Error 1: declaration correction: int x;

Error 2: cin<num; correction : cin>num;

Error 3: cout>s1>s2; correction : cout<s1<s2;

(1/2 for each error and correction)

j)  #include<iostream.h>

static int x 20; -3-

void main()

{ int num[max];

num={20,50,10,30,40};

for(loc=max-1;loc>=0;loc--)

cout>num[loc];

}

Ans: Error 1: static int x 20; correction: static int max =20;

Error 2: int num[max];

num={20,50,10,30,40}; correction : int num[max] ={20,50,10,30,40};

( 1 1/2 for each error and correction)

6. Answer the following: (15)

a) convert for loop into while loop and if statement into switch case, rewrite the code: -4-

for(j=0;j<noofkids;j++)

{

if kilometer==1000)

totalfare+=500/2;

else

if kilometer==500)

totalfare+=300/2;

else

totalfare+=200/2;

}

Ans: j=0;while(j<noofkids)

{

switch(kilometer)

{

case 1000:

totalfare+=500/2;

case 500:

totalfare+=300/2;

default:

totalfare+=200/2;

}

j++;

}

(2 mark for while loop, 2 mark for switch case)

b) What are the functions of the following header files: -3-

iomanip.h

string.h

math.h

Ans: iomanip.h: it declars the c++ streams, I/O manipulations and contains macros for creating parameterized manipulators.

String.h: several string manipulations are routine in it

Math.h : several mathematical calculation and conversions are routine in it.

(1mark each for correct answer)

c)Define a function convert() to convert Celsius to Fahrenheit temperature. -2-

Ans : void convert()

{

float a,b;

cout<”please enter the celsius value\n”;

cin>a;

b=((a*9)/5)+32;

cout<”The temperature in Fahrenheit is “<b;

getch();

}

(1/2 for function, 1 for equation, ½ for other statemetns)

d)Name the header files(s) that shall be needed for successful compilation of the following code: -1-

void main()

{ char string[25];

gets(string);

strcat(string,”nms”);

puts(string);

}

Ans: iostream.h, string.h. stdio.h

(1/2 mark each- no marks for iostream.h)

k)  If the ages of Ram, Sulabh and Ajay are input by the user, write a program to determine the youngest of the three. -2-

Ans : int main()

{ int ram_age,sulabh_age,ajay_age;

cout<"Enter Ram age:";

cinram_age;

cout<"Enter Sulabh age:";

cinsulabh_age;

cout<"Enter Ajay age:";

cinajay_age;

if (ram_agesulabh_age & ram_ageajay_age)

cout<"Ram is youngest";

else if(sulabh_ageram_age & sulabh_ageajay_age)

cout<"Sulabh is youngest";

else

cout<"Ajay is youngest";

return 0;

}

( 1 mark for reading data, 2marks for if statements)

l)  Define a function prime() to check a given number is prime or not. -3-

Ans : int prime()

{

int n;

bool flag=false;

cout"Enter any number : ";

cinn;

for(int i=2;in;i++)

{

if(n%i==0)

{

flag=true;

break;

}

}

if(flag==false & n1)

cout"Number is prime";

else

cout"Number is not prime";

return 0;

}

( 1/2 mark for reading data, 1 for for loopto check the number for prime , 1mark for if statement which displasy the result)

Page 12 of 12