You are on page 1of 10

CLASS XI

FIRST TERM EXAMINATION 2013-2014


SUBJECT : COMPUTER SCIENCE
SET B2(SOLUTIONS)
Time allowed: 3 hours

Max Marks : 70

General Instructions :

This paper consists of 10 pages


There are 5 questions in this paper
Attempt all questions neatly and in order
Programming language : C++

Q1a) Name the header files are required for the following :
i)
cin
ii)
setw( )
iii)
sqrt( )
iv)
exit( )
Ans . i)
ii)
iii)
iv)

cin
setw( )
sqrt( )
exit( )

(2)

iostream.h
iomanip.h
math.h
process.h

b) Explain the difference between break and continue with the help of an
example.
(3)
Ans.

Break statement is used to terminate the smallest enclosing while, dowhile, for or switch statement while continue statement forces the next
iteration of the loop to take place, skipping any code in between.
for(int i=1;i<=10;i++)
{
If (i%3 ==0)
break;
else
cout<<i<<endl;
}
It will give the output as :
1
2
for(int i=1;i<=10;i++)
{
If (i%3 ==0)
continue;
else
cout<<i<<endl;
}
It will give the output as :
1
1

2
4
5
7
8
10
c)

What is meant by type casting and type promotion? Explain with an


example of each.
(3)

Ans.

The implicit conversion of one data type to another one by the compiler is
called type promotion. E.g if one operand is int and other is float, int
operand is converted to float by the compiler.
Example :
int a;
float b, c;
c= a / b
The explicit type conversion is done by the programmer. It is also called
type casting.
Example :
int a,b;
(float) a+b/2;

d)

Differentiate between a while loop and a do- while loop with example. (2)

Ans.

In while loop, the test-expression is evaluated before entering into a loop


whereas in a do-while loop, the test-expression is evaluated before exiting
from the loop. Do_while loop is executed at least one even if the test
condition evaluates to false even for the very first time.

e)

Ans:

How many bytes are reserved for the following data types:
i)
float
ii)
char

(2)

float occupies 4 bytes and char occupies 1 bytes

f)

What is the significance of default clause in switch statement? Is it


necessary to include default statement in switch statement?
(2)

Ans.

Default clause is executed when no match is found. No, it is not necessary


to include default statement in switch statement.

g)
Ans:

What is the difference between a and \a?

(2)

a represents a single character that occupies 1 byte in memory whereas


\a represents an escape sequence .
2

h) Differentiate between the statements: ++a; and a++;. Also, give a


suitable C++ code to illustrate the difference.
(2)
Ans. Post-increment operator is placed after the operand e.g., in a++. It differs
from Pre-increment operator in the sense that the value of the operand is
first used in the expression (for evaluation) and then the operands value is
incremented by 1 (i.e., follows Use-then-Change rule).
Pre-increment operator is placed before the operand e.g., in ++a. it differs
from the Post-0ncrement operator in the sense that the value of the
operand is first incremented by 1 and then used in the expression for
evaluation (i.e., follows Change-then-Use rule)
For example,
:
int value1 = 20, value2 = 20, sum1 = 0, sum2 = 0 ;
sum1 = sum1 + ++value1 ;
//change-then-use
sum2 = sum2 + value2++ ;
//Use-then-change
cout << sum1 << value1 << endl ;
cout << sum2 << value2 << endl ;
The output produced by above code will be :
21 21
// because of chabge-then-use
20 21
// because of use-then-change
i)
Ans.

Explain the role of sizeof operator with the help of an example.

(2)

The sizeof operator is used to determine the no. of bytes of memory


occupies by a variable or data type
Example : cout<< sizeof(float);
will give an output 4
char ch; sizeof ch;
will give an output 1

Q2 Find the syntax errors in the following code and write the corrected code
underlining the corrections made.
a) #include(iostream.h)
(3)
void main( )
int x; y;
cin>>x;
for (y= =0; y<10, y++)
if x= =y
cout<<y+x;
else
cout<<y;
Ans.

#include<iostream.h>
3

void main( )
{
int x, y;
cin>>x;
for (y =0; y<10 ; y++)
if (x= =y)
cout<<y+x;
else
cout<<y;
}
b) #include<<iostream.h>>
void main( );
{
const int max=0;
int a,b;
cin>>a>>b;
if(a>b) max=a;
for(x; x<max; x++)cout>>x;
}
Ans.

#include<iostream.h>
void main( ) ;
{
const int max=0;
int a,b;
cin>>a>>b;
if(a>b) max=a;
for(int x = 1; x<max; x++)cout<<x;
}
c) #include<iostream.h>
int main
{
int j=99,a;
float u=10.0;
cin(a);
while a<=j
{
a+=10
u=/a;
}
}

Ans.

(3)

1)
2)

(3)

Instead of main, it should be main( )


It should be void main( ) or return statement should be there
4

3)
4)
5)
6)
Q3.

Instead of cin(a), it should be cin>>a


Instead of u=/a, it should be u/=a
The condition of while should be put in parenthesis i.e (a<=j)
The semicolon after a+=10 is missing.

Find the output of the following code (assuming all the necessary files are
included.)

a) #include<iostream.h>
void main( )
{
int i,j,k,x=0;
for(i=0;i<3;++i)
for(j=0;j<i;j++)
{ switch(i+j-1)
{
case -1:
case 0:
x+=1;
break;
case 1:
case 2:
case 3:
x+=3;
default:
x+=3;
}
cout<<x<< ;
}
cout<<"\n "<<x;
}
Ans.
b)

Ans.

(4)

1 7 13
13
int x=25, y=10;
cout<<++x<<@<<y++<<@<<--y<<endl;
cout<<x++<<@<<--y<<@<<x++<<endl;

(3)

26@9@9
27@9@26

c) #include<iostream.h>

(4)

void main( )
{ for ( int a = 3 ; a< 10 ; a++ )
{ if ( a==4)
a+=5;
5

for ( int b = 4 ; b > 0 ; b--)


{ cout << b * a << @ ;
}
cout<< endl;
}
}
Ans

12@9@6@ 3@
36@27@18@ 9

Q4.

Convert the following codes as directed.


a) for to do . While loop
for ( int x=1; x<=n; x++)
for ( int y=1; y<=x; y++)
{ sum+ = (x+y);
cout<<sum<<endl;
}

Ans :

int x=1, y;
do
{ y=1;
do
{ sum+ = (x+y);
cout<<sum<<endl;
y++;
} while (y<=x);
x++;
} while (x<=n);
b)

Ans.

(2)

if. else to switch case


int num,val;
cin>>num;
if (num= =5)
{
val=num*25-20;
cout<<num+val;
}
else
if (num= =10)
{
val=num*20-15;
cout<<val-num;
}
int num,val;
cin>>num;
switch (num)
6

(2)

{
case 5:
case 10:

val=num*25-20;
cout<<num+val;
break;
val=num*20-15;
cout<<val-num;
break;

}
c)

Ans.

do while loop to for loop


int n=0,x=0;
do{
if ( (n%2 ==0)
{
x=x+n ;
cout<<x;
}
n++;
} while (n<10);

(2)

int x=0;
for ( int n=0 ; n < 10 ; n++)
{
if ( (n%2 ==0)
{
x=x+n;
cout<<x;
}
}

Q5 a) Given the following two expressions :


(2)
i)
x = 10;
ii)
x == 10;
How are these two different? What will be the final value of x after
execution of the two statements if the initial value of x is 15?
Ans

b)

i) In x= 10;, the value 10 is assigned to variable. In ii) x == 10; we are


comparing the value of x with 10. After execution of the first statement the
value of x becomes 10 while second statement will give the result false.
Construct logical expressions to represent the following conditions :
i) fare is greater than or equal to 115 but less than 125
ii) fees is in the range 4000-5000 or grade = A

Ans

i) if ( ( fare>=115 ) && ( fare<125 ) )


7

(2)

ii) if (( fees >4000 ) && ( fees < 5000 ) || (grade == A ))


c)

Write a program that reads the value of an integer n and then display the
following patterns till n number of lines. For e.g. If n=4 then the output
should be:
(4)

4
Ans.

d)

Ans.

3
3

2
2
2

1
1
1
1

2
2
2

3
3

#include <iostream.h>
void main( )
{
int i,j,n;
cout <<Enter the no of lines to be printed \n;
cin>>n;
for (i = 1; i <= n; i ++)
{
cout<<\n;
for (j = 1; j <= n-i ; j ++)
cout<< ;
for (k = i; k >= 1 ; k --)
cout<<j;
for (l = 1; k >= i ; l++)
cout<<j;
}
}
Write a program that reads two integer numbers n & m and then prints
every integer between 1 and n divisible by m. Also report whether the
number that is divisible by m is even or odd.
(4)
#include <iostream.h>
void main( )
{
int n, m;
cout <<Enter the value of n & m;
cin>>n>>m;
for (int i=1;i<=n; i++)
{
if (i%m= = 0)
{
cout<<i<<\t;
if (i%2)
cout<<It is a odd number;
else
cout<<It is an even number;
}
}
8

}
e)

Write a program that reads a given number and prints whether it is prime
or not. The program should continue as long as user wants.
(4)

Ans.

#include <iostream.h>
void main( )
{
char choice;
int n, prime=1;
do
{
cout <<Enter the value of n;
cin>>n;
for (int i=2;i<=n/2; i++)
{
if (n%j= = 0)
{
prime=0;
break;
}
}
if(prime= =1)
cout<<It is not a prime number;
else
cout<<It is a prime number;
cout<<Do u want to continue;
cin>>choice;
}while (choice = = Y || choice = =y);
}

f)
Ans.

Write a program that reads a number, n and prints all armstrong numbers
from 1 to n.
(4)
#include <iostream.h>
void main( )
{
int n, num, digit, sum=0;
cout<<Enter the number;
cin>>n;
for(int i=1;i<=n;i++)
{
num=n;
sum=0;
do
{
digit = n%10;
sum = sum + pow(digit,3);
n = n /10;
9

} while (n >0);
if (num = = sum)
cout<< i;
}
}
g)

Write a program that reads a number n and find the sum of Fibonacci
series upto n terms.
(4)

Ans.

#include <iostream.h>
void main( )
{
int n, first=0, second=1, third, sum=0;
cout<<Enter the number;
cin>>n;
sum = first + second;
for ( int i =3; i<=n ; ++i)
{
third = first + second;
sum = sum + third;
first = second;
second = third;
}
cout<< Sum of Fibonacci series up to n is <<sum;
}

10

You might also like